简体   繁体   English

使用pyserial从arduino读取整数

[英]Reading integer from arduino using pyserial

I am sending integer value from arduino and reading it in Python using pyserial The arduino code is:我正在从 arduino 发送整数值并使用 pyserial 在 Python 中读取它 arduino 代码是:

Serial.write(integer)

And the pyserial is: pyserial 是:

ser=serial.Serial ('com3',9600,timeout =1)
X=ser.read(1)
print(X)

But it doesn't print anything except blank spaces Does anyone knows how to read this integer passed from arduino in Python?但它除了空格之外不打印任何东西有谁知道如何在 Python 中读取从 arduino 传递的这个整数?

You probably need to use a start bit.您可能需要使用一个起始位。

The problem might be that the arduino has already written the integer by the time pyserial is running?问题可能是在 pyserial 运行时 arduino 已经写入了整数?

So write a character from pyserial to arduino to signal start like所以写一个字符从 pyserial 到 arduino 来表示开始

    ser=serial.Serial ('com3',9600,timeout =1)
    ser.write(b'S')
    X=ser.read(1)
    print(X)

And write the integer from the arduino once you get this start bit.一旦你得到这个起始位,就从 arduino 中写入整数。

That is the not the right way to read an Integer from Arduino.这不是从 Arduino 读取Integer的正确方法。 Integer is a 32-bit type while your serial port would be set to EIGHTBITS (Both in pyserial and Arduino. Correct me if I'm wrong) in byte size, therefore you have to write the Character version of the Integer from Arduino while transmitting it through a Serial Port because a Character takes only EIGHTBITS in size which is also the convenient way to do the stuff that you need to, very easily. Integer是 32 位类型,而您的串行端口将设置为字节大小的EIGHTBITS (在 pyserial 和 Arduino 中。如果我错了,请纠正我) ,因此您必须在传输时从 Arduino 编写IntegerCharacter版本它通过串行端口,因为一个Character大小只需要8 位,这也是很容易地完成你需要的事情的便捷方式。

Long story short, convert your Integer into a String or a Character Array before transmitting.长话短说,在传输之前将您的Integer转换为StringCharacter数组。 (Chances are there are inbuilt functions available for the conversion). (可能有可用于转换的内置函数)。

On a side note here is the correct python code that you'd prefer to use:在旁注中,这里是您更喜欢使用的正确 python 代码:

ser = serial.Serial(
        port='COM3',
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS
    )
    #RxTx
    ser.isOpen()
while 1:
    out = ''
    while ser.inWaiting() > 0:
        out += ser.read(1)
    if out != '':
        print ">>Received String: %s" % out

An easy program which I tested:我测试的一个简单的程序:

Arduino:阿杜诺:

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  int f1=123;
  // print out the value you read:
  Serial.println(f1);
  delay(1000);    
}

Python:蟒蛇:

import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 'COM5'

ser.open()
while True:
  h1=ser.readline() 
  if h1:
    g3=int(h1); #if you want to convert to float you can use "float" instead of "int"
    g3=g3+5;
    print(g3)

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

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