簡體   English   中英

Arduino 和 Python 之間的通信

[英]Communication between Arduino and Python

我需要使用我的筆記本電腦與 Arduino 對話(發送命令來控制 state 機器以制作簡單的自動售貨機)當我嘗試將命令放入串行監視器時一切看起來都很好但是當我通過 python 發送命令時不是在職的。 看起來 state 沒有像我預期的那樣改變。

這是 python 代碼:

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()

這是 Arduino 代碼:

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();
  }

我從文本文件中讀取了類似的命令

001
001
002
001
001
003
001
004
001
005

這是我期望的 output:

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? 我該如何解決這個問題?

在您的每個功能中,您都有這一行:

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

該行打開了重置 Arduino 板的串行端口。 為避免這種情況,您應該在程序開始時打開該端口一次,然后每次都繼續使用相同的串行端口實例,而不是在每個 function 中重新創建它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM