Hello
I’m sending data to python from my Arduino and I’m having some difficulties. My program on the python side is not working and I’m curious if the string I’m sending is in the correct format. (Arduino 1.0 and Python 2.7.2 and pySerial 2.5)
Here is the Arduino code:
void setup()
{
Serial.begin (115200);
}
void loop()
{
Serial.println("{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}");
}
This should be interpreted by python as a regular dictionary with strings as keys and values.
Below is the Python code:
import serial
usbport ='/dev/tty.usbserial-A600af58'
ser = serial.Serial(usbport, 115200, timeout=1)
RawData=ser.readline()
for i in range (10):
RawData=ser.readline()
print RawData
print RawData['p2']
I get an error when trying to read one of the dictionary keys.
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>>
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
{'p1':'1010','p2':'756','p3':'395','p4':'10','p5':'897','p6':'1024'}
Traceback (most recent call last):
File "/Users/mdsmac/Dropbox/MiniMe/Software/Currently version in use/Sens_Input_Dict0618.py", line 11, in <module>
print RawData['p2']
TypeError: string indices must be integers, not str
>>>
Is Arduino sending what Python reads as a string or is it something else?
Thanks