简体   繁体   English

Arduino串口通信问题

[英]Arduino serial communication issue

I'm trying to write a simple program to read serial data from Arduino.我正在尝试编写一个简单的程序来从 Arduino 读取串行数据。 In the Arduino serial monitor window, everything works fine.在 Arduino 串行监视器窗口中,一切正常。 In the Python console, each number is on a separate line.在 Python 控制台中,每个数字都在单独的一行上。 In Pycharm, it just shows b' ' .在 Pycharm 中,它只显示b' ' I don't know where the problem is.我不知道问题出在哪里。

Arduino Serial Monitor : Arduino 串行监视器

1234567890

1234567890

1234567890

1234567890

1234567890

1234567890

1234567890

1234567890

1234567890

1234567890

Python 3 console : Python 3 控制台

1

2

3

4

5

6

7

8

9

0

Pycharm IDE :开发环境

b' '

b' '

b' '

b' '

b' '

b' '

b' '

b' '

b' '

b' '

Here is the Python 3 code I am using:这是我正在使用的 Python 3 代码:

import serial
from time import sleep

Ser = serial.Serial("COM3", 9600, timeout=0)
Counter = 1

while Counter <= 10:
    data = Ser.readline()
    print(data)
    sleep(1)
    Counter += 1
Ser.close()

Arduino code:阿杜诺代码:

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    Serial.println(1234567890);
    delay(1000);
}

Perhaps a side effect of timeout=0 .也许是timeout=0的副作用。 I would try this:我会试试这个:

import serial

Ser = serial.Serial("COM3", 9600, timeout=1)
data = Ser.readline()
print(data)
Ser.close()

Try using the AMD module !!尝试使用AMD模块!!

Link to the documentation: https://pypi.org/project/AMD/文档链接: https ://pypi.org/project/AMD/

AMD is a powerful Data-Science module built especially for data extraction and communication with Arduino . AMD是一个功能强大的数据科学模块,专为数据提取和与 Arduino 的通信而构建 This module automatically filters all escape sequence characters and returns you a piece of data or a list of data from the Arduino!该模块自动过滤所有转义序列字符,并从 Arduino 返回给您一条数据或数据列表

Install via pip: pip install AMD通过 pip 安装: pip install AMD

Documentation for ardata function in the link: https://github.com/SayadPervez/AMD-SEPERATE-DOCUMENTATION/blob/master/ardata().md链接中 ardata 函数的文档: https ://github.com/SayadPervez/AMD-SEPERATE-DOCUMENTATION/blob/master/ardata().md

Your entire python code can be replaced with the below two lines!!您的整个 python 代码可以替换为以下两行!!

from AMD import *
data = ardata(3,lines=10)

Alternatively, you can also use the below modified line to get more functionality或者,您也可以使用下面的修改行来获得更多功能

data = ardata('COM3',lines=10,squeeze=False,numeric=True)

The first parameter is the COM port .一个参数COM 端口 It can either be a string or an Integer.它可以是字符串或整数。 lines represent the number of lines of data to be read from the serial monitor. lines表示要从串行监视器读取的数据行数 squeeze parameter specifies if data has to be compressed. squeeze参数指定是否必须压缩数据。 numeric specifies if the expected data is of numeric type(integers or floats). numeric指定预期数据是否为数字类型(整数或浮点数)。 However for your requirement the first two lines of code are enough since the rest are set by default!然而,对于您的要求,前两行代码就足够了,因为其余代码都是默认设置的!

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

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