- It actually does something useful. (WOW!)
- Has basic error checking inplace, should help eliminate string index errors
- Has a basic packet structure in place, 959595 indicates that data is coming in.
First of all, this program will read the data in from the Sharp IR range sensor I am using as a semi random number generator. You will notice on the output it looks like this:
Output:
C&
C is 69 or something in ASCII(too lazy to check), which is a range that this sensor could be producing. In order to verify it is accurate, I replaced the actual sensor input on the ard with "343" and ran it, the output was W. Go ahead, hit alt + 343. W is the result, which verifies the output.
Also, please note in the code below, in the function, that I am using basic error checking to avoid errors. This one was implemented because I was getting a pretty constant string index error after a few seconds because it would not have any data and thus nothing to access(I think :P). Anyways, this basically runs a length check on the string and then verifys that the index that is about to be called is actually there, if not it breaks out of the while loop and goes around again. I am no professional coder, just a hobbyist, but I feel that these kinds of steps are crucial to a solid program. I am sure there are cleaner ways to do it, and I will look for them and try to think them up, but this is working for now.
You will see that I used 959595 for the initial "header" part of the data, and then only a & as the end in the function. The 959595 was used because it should be bigger then any of my sensor readings, and I used & because it was a random variable.
Anyways! Below is the code, as always.. feel free to use it for anything you want, no credit needed at all. Just enjoy it and hopefully learn some. Oh, that reminds me, this is all done in strings really. It is just easier in python and ard to use strings all around then dealing with conversions at the moment.
import serialimport timeSensors1 = 0r = 0y = 0# Setup area, starts serial, gives some info, then sleeps for about 2 seconds.ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=0) # Pretty straightforward, opens that device up for serial.print "Current port location:" # Informationalprint ser.portstr # Prints basic port informationtime.sleep(1.75)def Sensor_Read(): # This is the function that reads the sensor in.i = 0j = 0r = 0while i == 0:r = ser.read(2)if(j >= len(r)): break # Makes sure I am not refrencing an index that is out of rangeif(r[j] == '&'): i = 1 # Reads until I find a &j = j+1return rwhile y == 0:sensors1 = ser.read(6)if(ser.inWaiting() > 0): # Don't run this until something is in the bufferif(len(sensors1) > 0):if sensors1 == "959595": # This is arbitrary, I just told the arduino to write that as a 'header'print "Output:"print Sensor_Read()time.sleep(.1)else:ser.flushInput() # If the numbers aren't write, wipe the buffer and retryser.close()
Now is the arduino code, a few quick notes. It is a very compact code base, because the ard is by far the weaker of the two systems for processing power and memory. Also, I commented out the build notice because it is not terribly relevant right now.
int InputSer = 0;int potPin = 7;int AnalogIn = 0;void setup(){Serial.begin(19200); //Rather straightforward, 9600 baud rate for this serial connection.//Serial.println("Adruino Python serial system. Ard Client. Build #4");}void loop() {AnalogIn = analogRead(potPin);Serial.println("959595");Serial.write(AnalogIn); // This will send the intiger on the same line as the &, & carries a /n(line term) on it.Serial.write('&'); // Signals end of variabledelay(100);}
I really wish there was a way to make that code look cleaner, but just copy it to a wordpad(or whatever) and view it there. This is currently working on the Arduino mega and CentOS5 with Python 2.6(I believe). Also you need the PySerial module installed. Google it, if you still need help just email me or comment here.
Thanks all! Enjoy!
No comments:
Post a Comment