简体   繁体   中英

Communication between Arduino and Python

I need to use my laptop talk with the Arduino(send a command to control state machine to make a simple vending machine) everything looks fine when I tried to put the command in the serial monitor but when I send the command via the python is not working. look like the state not change as I expect.

Here is the python code:

def query():
   cmd = "0"
   dev = serial.Serial("COM3", 9600,timeout=5)
   time.sleep(2)
   dev.write(cmd.encode("utf-8"))
   print("Pyhton: Query                  ",dev.readline())
   #time.sleep(1)
   #print(dev.readline())

def up():
   cmd = "1"
   dev = serial.Serial("COM3", 9600)
   time.sleep(2)
   dev.write(cmd.encode("utf-8"))
   print("Pyhton: up                     ",dev.readline())
   #time.sleep(1)
   #print(dev.readline())

def pickup():
   cmd = "2"
   dev = serial.Serial("COM3", 9600)
   time.sleep(2)
   dev.write(cmd.encode("utf-8"))
   print("Pyhton: pickup                  ",dev.readline())
   #time.sleep(1)
   #print(dev.readline())


def ship():
   cmd = "3"
   dev = serial.Serial("COM3", 9600)
   time.sleep(2)
   dev.write(cmd.encode("utf-8"))
   print("Pyhton: ship                 ",dev.readline())
   #time.sleep(1)
   #print(dev.readline())


def origin():
   cmd = "4"
   dev = serial.Serial("COM3", 9600)
   time.sleep(2)
   dev.write(cmd.encode("utf-8"))
   print("Pyhton: origin               ",dev.readline())
   #time.sleep(1)
   #print(dev.readline())


tokenDict = {
   "001":query,
   "002":up,
   "003":ship,
   "004":pickup,
   "005":origin
   }

lineList = [line.rstrip('\n') for line in open("cmd.txt")]

for line in lineList:
   time.sleep(1)
   functionToCall = tokenDict[line]
   functionToCall()

Here is the Arduino code:

int cmd = 0;
enum {IDLEx, UP, PICKUP, SHIP, ORIGIN};
uint8_t STATE = IDLEx;

void setup() {
  Serial.begin(9600);
  //Serial.flush();
  pinMode(LED_BUILTIN, OUTPUT);
}

void readSerialData() {
  if (Serial.available()>0) {
    delay(300);
    //cmd = Serial.readStringUntil('\n');
    cmd = Serial.read();
    process_serial(cmd, &STATE);
    //test(cmd);
  }
}

  void process_serial(int data, uint8_t *state)
  {
    switch (*state)

    {
      case IDLEx: {
          if (data == '0') {
            *state = IDLEx;
            Serial.println("Arduino: IDLE");

          } else if (data == '1' ) { //go to up state
            *state = UP;
            //Serial.println("Arduino: Up");
          } else if (data == '2') {
            *state = PICKUP;
            //Serial.println("Arduino: PICKUP");
          } else if (data == '3') {
            //Serial.println("Arduino: SHIP");
            *state = SHIP;
          } else if (data == '4') {
            //Serial.println("Arduino: ORIGIN");
            *state = ORIGIN;
          } 
          break;
        }
      case UP: {
          Serial.println("Arduino: UP");
          delay(1000);
          *state = PICKUP;
          break;
        }
      case PICKUP: {
          Serial.println("Arduino: PICKUP");
          delay(1000);
          *state = SHIP;
          break;
        }
      case SHIP: {
          Serial.println("Arduino: SHIP");
          delay(1000);
          *state = ORIGIN;
          break;
        }
      case ORIGIN: {
          Serial.println("Arduino: ORIGIN");
          delay(1000);
          *state = IDLEx;
        }
        break;
    }
  }
  void loop() {
    //Serial.println(STATE);
    readSerialData();
  }

I read the command from the text file something like

001
001
002
001
001
003
001
004
001
005

Here is the output I expect:

Pyhton: Query                   b'Arduino: IDLE\r\n'
Pyhton: Query                   b'Arduino: IDLE\r\n'
Pyhton: up                      b'Arduino: Up\r\n'
Pyhton: Query                   b'Arduino: Up\r\n'
Pyhton: Query                   b'Arduino: Up\r\n'
Pyhton: ship                  b'Arduino: SHIP\r\n'
Pyhton: Query                   b'Arduino: SHIP\r\n'
Pyhton: pickup                   b'Arduino: PICKUP\r\n'
Pyhton: Query                   b'Arduino: PICKUP\r\n'
Pyhton: origin                b'Arduino: ORIGIN\r\n'

My issue is when I send the data to Arduino seems like Arduino not change the State(Swith case as the code) please note that I use the Arduino nano 328 and python 3.x Can anyone suggest to me? How I can solve this issue?

In each one of your functions you have this line:

dev = serial.Serial("COM3", 9600)

That line opens the serial port which resets the Arduino board. To avoid this you should open that port once at the beginning of your program and then just keep using that same instance of the serial port each time instead of recreating it in each function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM