Pyserial没有正确读取串行输入并引发错误

时间:2015-02-23 15:25:49

标签: python arduino pyserial

我正在研究一个使用pyserial从arduino读取串行输入的代码。输入字符串应该类似于" 2786,7,258.00,10,20,5,0,2000,4809.14,N,1131.70,E"但是pyserial给出的是" 286,9,246.00,10,20,0,2000,4809.74,N,131.90,E"几秒钟后,程序出现错误

 Traceback (most recent call last):
    File "/home/saurabh/Desktop/testreception.py", line 14, in <module>
    main()
    File "/home/saurabh/Desktop/testreception.py", line 9, in main
      x.append(ser.readline())
    File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 460,  in read
    raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
`SerialException: device reports readiness to read but returned no data (device disconnected?)

我的代码:

import serial

def main():
    a = 1
    x = []
    i = 0
    ser = serial.Serial('/dev/ttyACM0', 9600)
    while(a):
        x.append(ser.readline())
        print(x[i])
        i = i+1

if __name__=="__main__":
    main()

arduino代码是:

 int TEAM_ID = 2786;
 int h = 00;
 int m = 00;
 int s = 00;
 int MISSION_TIME = s*1 + m*100 + h*10000;
 float ALT_SENSOR = 300;
 int OUTSIDE_TEMP = 10;
 int INSIDE_TEMP = 20;
 int VOLTAGE = 12;
 int FSW_STATE = 0;
 int ROTOR_RATE = 2000;
 float LATTITUDE = 4807.038;
 char LAT_CHAR = 'N';
 float LONGITUDE = 01131.0000;
 char LON_CHAR = 'E';
 int i = 1, j = 1;
 void intialize()
 {
  h = 00;
  m = 00;
  s = 00;
  MISSION_TIME = 0;
  ALT_SENSOR = 300;
  OUTSIDE_TEMP = 10;
  INSIDE_TEMP = 20;
  VOLTAGE = 12;
  FSW_STATE = 0;
  LATTITUDE = 4807.038;
  LONGITUDE = 01131.0000;
  i = 1; j = 1;
 } 
 void setup(){
  Serial.begin(9600);
 }
 void loop()
 {
  Serial.print(TEAM_ID); Serial.print(',');
  Serial.print(MISSION_TIME); Serial.print(',');
  Serial.print(ALT_SENSOR); Serial.print(',');
  Serial.print(OUTSIDE_TEMP); Serial.print(',');
  Serial.print(INSIDE_TEMP); Serial.print(',');
  Serial.print(VOLTAGE); Serial.print(',');
  Serial.print(FSW_STATE); Serial.print(',');
  Serial.print(ROTOR_RATE); Serial.print(',');
  Serial.print(LATTITUDE); Serial.print(',');
  Serial.print(LAT_CHAR); Serial.print(',');
  Serial.print(LONGITUDE); Serial.print(',');
  Serial.print(LON_CHAR); 
  Serial.print('\n');
  delay(1000);
  MISSION_TIME += i;
  j += 1;
  if(s>59)
 {
  m=m+1;
  s += 1;
 }
  if(m>60)
 {
  h=h+1;
 }
  if(j==20)
 {
  FSW_STATE = 3;
 }
  if(j==30)
 {
  FSW_STATE = 4;
 }
  if(j==40)
{
 FSW_STATE = 5;
}
 if(j==50)
{
 FSW_STATE = 6;
}
 OUTSIDE_TEMP += 0.3;
 INSIDE_TEMP += 0.2;
 VOLTAGE -= 0.2;
 ALT_SENSOR -= 6;  
 s += 1;
 LATTITUDE += 0.3;
 LONGITUDE += 0.1;
 if(ALT_SENSOR == 0)
{
 intialize(); 
}
 loop;
}

0 个答案:

没有答案