简体   繁体   English

无法将字节从 Python serial.write() 发送到 Arduino

[英]Unable to send byte from Python serial.write() to Arduino

I wrote a python script that sends and receive data to and from an Arduino.我编写了一个 python 脚本,该脚本向 Arduino 发送和接收数据。 I can receive data without problems, but when I send data it seems like the Arduino is receiving only garbage.我可以毫无问题地接收数据,但是当我发送数据时,Arduino 似乎只接收垃圾。 The Arduino is connected to a linux pc via USB and the pc is running Python 3.8. Arduino 通过 USB 连接到 linux 电脑,电脑正在运行 ZA7F5F35426B92741137231B56382。 Since the original code contains a lot of unrelated stuff and may be distracting, I prepared the simplest possible sketch to demontrate the problem:由于原始代码包含许多不相关的内容并且可能会分散注意力,因此我准备了最简单的草图来演示该问题:

Python code: Python代码:

#! /usr/bin/env python

import serial
import time

if __name__ == '__main__':
    ser = serial.Serial("/dev/ttyACM0", 9600, timeout=0)
    time.sleep(5)   # Wait for Arduino to reset and init serial
    ser.write(67)

Arduino sketch Arduino 草图

const byte LED_PIN = 13;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  if (Serial.available() > 0) {      
    byte b = Serial.read();
    if (b == 67) {
         digitalWrite(LED_PIN, HIGH);
         delay(500);
         digitalWrite(LED_PIN, LOW);
    }
  }
}

This code flashes the onboard LED when receives a byte with value 67 (uppercase C).此代码在接收到值为 67(大写 C)的字节时使板载 LED 闪烁。 This works with the Arduino Serial Monitor, but not when running the Python script.这适用于 Arduino 串行监视器,但不适用于运行 Python 脚本时。 I feel like my problem may be related to this question , but in that case the focus was on the user not considering the case of an empty serial input buffer, while I believe the problem is actually in the sent data.我觉得我的问题可能与这个问题有关,但在那种情况下,重点是用户不考虑空串行输入缓冲区的情况,而我认为问题实际上出在发送的数据中。 This is why I decided to leave out the receiving part and simplify the example using only the onboard led.这就是为什么我决定省略接收部分并仅使用板载 LED 简化示例的原因。

Update更新

I made it work changing the last line in the python script to:我将 python 脚本中的最后一行更改为:

    ser.write(bytes([67]))

(note the [] added around the integer value). (注意在 integer 值周围添加的 [])。

Anyone can explain why this syntax produces the correct result?任何人都可以解释为什么这种语法会产生正确的结果? It seems like I'm passing a single entry array to the function bytes().似乎我正在将单个条目数组传递给 function 字节()。

Pardon my poor skills in Pyton, I know the question is probably basic.请原谅我在 Pyton 的技能很差,我知道这个问题可能是基本的。

It's really simple;这真的很简单; the methods you tried that worked all conform to the specification for the pyserial library.您尝试的所有方法都符合 pyserial 库的规范。

Per the Pyserial documentation:根据 Pyserial 文档:

write(data)
Parameters: data – Data to send.
Returns:    Number of bytes written.
Return type:    int
Raises: SerialTimeoutException – In case a write timeout is configured for the port and the time is exceeded.
Write the bytes data to the port. This should be of type bytes (or compatible such as bytearray or memoryview). Unicode strings must be encoded (e.g. 'hello'.encode('utf-8').

This is why the b'C style worked as well as the bytes call.这就是为什么b'C样式和bytes调用一样有效的原因。

Attribution: Pyserial Page归因: Pyserial 页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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