hello im not very experienced with python so sorry for any bad code
i am running ubuntu 8.10 and using pyserial
i have made a script that reads the serial input from an arduino if you are not familier with an arduino here is there website
http://www.arduino.cc/
for some reason if i try to do anything to the var raw_read[1] i get an error that says "list index out of range" but if i just print raw_read i get so why cant i use the [1] and is there a better way to separate data from serial. i am getting a couple of vars sent but i dont know how to separate them correctly. right now i am using a character in between them and using split() to put them into a list. is there any way i should send the data in ,as in format

#!/usr/bin/env python
import serial
import time
class arduino:
	data1=0
	data2=0
	def __init__(self, _baud):
		self.serial = serial.Serial("/dev/ttyUSB0", _baud)
		
	def update(self):
		self.raw = self.serial.readline()
		self.raw_read = self.raw.replace("\r\n", "")
		self.raw_read = self.raw_read.split("<!>")
		data1 = self.raw_read[0]
		data2 = self.raw_read[1]

foo = arduino(9600)

while 1:
	foo.update()
	time.sleep(.050)

Recommended Answers

All 3 Replies

Nice project!
Why don't you at least test-print self.raw and self.raw_read to see what you got.

Computers are faster than serial ports, so you are probably getting several reads (and processing) the same data more than once, and reading and trying to process the null between each piece of data. For serial reads you want to do something like this pseudo code:

port_read = False
data = read_the_port()
while True:
    if port_read:
        ## print the 'Null' to see what it contains and what
        ## length the program will see it as
        print "'Null' found  " , len(data), data
        if len(data.strip()) < 2:        ## may have to adjust the "2"
            port_read = False     ## Null found
    if not port_read:
        process_the_data( data )
        port_read = True      ## don't process again until the 
                              ## next piece of data, i.e. Null is found
    data = read_the_port()

This all depends on what and how data is being sent to the serial port of course.

I just read this again and it probably should be more like the following (which seems clumsy), but like I said earlier, you'll have to work out the details.

port_read = True
data = read_the_port()
while True:
    if port_read:
        ## print the 'Null' to see what it contains and what
        ## length the program will see it as
        print "'Null' found  " , len(data), data
        while len(data.strip()) > 2:        ## (same data) may have to adjust the "2"
            data = read_the_port()    
        port_read = False     ## Null found
    if not port_read:
        if len(data) > 2:
            process_the_data( data )
            port_read = True      ## don't process again until the 
                                  ## next piece of data, i.e. Null is found
        data = read_the_port()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.