通过串口从arduino发送数据到python

时间:2015-10-07 11:46:26

标签: python arduino arduino-ide

我编写了一个代码来从Arduino读取数据并检查数字输入高或登录到python程序但我遇到了困难。程序正常通过串行发送和接收数据,但问题出现在数字读取引脚上。它不是读取引脚,如果高,也发送错误的高低信息。我的代码如下:

代表Arduino:

int inPin30 = 30;
int inPin31 = 31;
int inPin32 = 32;
int inPin33 = 33;
int inPin34 = 34;
int inPin35 = 35;
int inPin36 = 36;
int inPin37 = 37;
int inPin38 = 38;


int outPin2 = 2;
int outPin3 = 3;
int outPin4 = 4;
int outPin5 = 5;
int outPin6 = 6;
int outPin7 = 7;
int outPin8 = 8;
int outPin9 = 9;
int outPin10 = 10;

int in0 = 0;
int in1 = 0;
int in2 = 0;
int in3 = 0;
int in4 = 0;
int in5 = 0;
int in6 = 0;
int in7 = 0;
int in8 = 0;
String str = "";


String inputString = "";         // a string to hold incoming data
boolean stringComplete = 0; // whether the string is complete
boolean stringComplete1 = 0;
void setup() {
    // input pin 
     pinMode(inPin30, INPUT);
     pinMode(inPin31, INPUT);
     pinMode(inPin32, INPUT);
     pinMode(inPin33, INPUT);
     pinMode(inPin34, INPUT);
     pinMode(inPin35, INPUT);
     pinMode(inPin36, INPUT);
     pinMode(inPin37, INPUT);
     pinMode(inPin38, INPUT);
    // output pin 
     pinMode(outPin2, OUTPUT);
     pinMode(outPin3, OUTPUT);
     pinMode(outPin4, OUTPUT);
     pinMode(outPin5, OUTPUT);
     pinMode(outPin6, OUTPUT);
     pinMode(outPin7, OUTPUT);
     pinMode(outPin8, OUTPUT);
     pinMode(outPin9, OUTPUT);
     pinMode(outPin10,OUTPUT);

  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
   //call the function
  // print the string when a newline arrives:

  serialEvent();
  if (stringComplete1==1) {

    // pin 2 high low
    if (inputString[0]=='1')
    {
    digitalWrite(outPin2, HIGH);

    }

     if (inputString[0]=='0')
    {
    digitalWrite(outPin2, LOW);

    }

    // pin 3
       if (inputString[1]=='1')
    {
    digitalWrite(outPin3, HIGH);

    }

     if (inputString[1]=='0')
    {
    digitalWrite(outPin3, LOW);

    }
    // pin 4
        if (inputString[2]=='1')
    {
    digitalWrite(outPin4, HIGH);

    }

     if (inputString[2]=='0')
    {
    digitalWrite(outPin4, LOW);

    }
    // pin 5
        if (inputString[3]=='1')
    {
    digitalWrite(outPin5, HIGH);

    }

     if (inputString[3]=='0')
    {
    digitalWrite(outPin5, LOW);

    }
    // pin 6
        if (inputString[4]=='1')
    {
    digitalWrite(outPin6, HIGH);

    }

     if (inputString[4]=='0')
    {
    digitalWrite(outPin6, LOW);

    }
    // pin 7

        if (inputString[5]=='1')
    {
    digitalWrite(outPin7, HIGH);

    }

     if (inputString[5]=='0')
    {
    digitalWrite(outPin7, LOW);

    }
    // pin 8

        if (inputString[6]=='1')
    {
    digitalWrite(outPin8, HIGH);

    }

     if (inputString[6]=='0')
    {
    digitalWrite(outPin8, LOW);

    }
    // pin 9

        if (inputString[7]=='1')
    {
    digitalWrite(outPin9, HIGH);

    }

     if (inputString[7]=='0')
    {
    digitalWrite(outPin9, LOW);

    }
    // pin 10
        if (inputString[8]=='1')
    {
    digitalWrite(outPin10, HIGH);

    }

     if (inputString[8]=='0')
    {
    digitalWrite(outPin10, LOW);

    }






    Serial.print(inputString);
    delay(1000);
    // clear the string:
    inputString = "";

  }


   if (stringComplete==0) {
        in0= digitalRead(inPin30);
    in1= digitalRead(inPin31);
    in2= digitalRead(inPin32);
    in3= digitalRead(inPin33);
    in4= digitalRead(inPin34);
    in5= digitalRead(inPin35);
    in6= digitalRead(inPin36);
    in7= digitalRead(inPin37);
    in8= digitalRead(inPin38);


     String str=String(in0)+String(in1)+String(in2)+String(in3)+String(in4)+String(in5)+String(in6)+String(in7)+String(in8)+'\n';
     Serial.print(str);
       //if (str[9] == '\n') {
      stringComplete = 1;
    //}
     delay(1000);

    }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete1 = 1;
      stringComplete = 0;
    }
  }
}

python代码:

import serial, time
arduino = serial.Serial('COM3', 9600, timeout=.1)
arduino.setDTR(level=False)
time.sleep(0.5)
arduino.flushInput()
arduino.setDTR()
time.sleep(0.5)
x =0
if x== 0:
    data = arduino.readline()
    print data
    time.sleep(3)
    x= 1
while True:
    arduino.write("111111111\n")
    data = arduino.readline()
    print data
    time.sleep(3)
    data1 = arduino.readline()
    print data1
    time.sleep(3)

0 个答案:

没有答案