Python中的串行通信(3.xx) [英] Serial comms in Python (3.xx)

查看:108
本文介绍了Python中的串行通信(3.xx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






我正在尝试读取一些串行数据(a在这个阶段的字符串)在Python中,我可以发送一个字符串(你好世界!),现在我正在尝试读取一个字符串。



[edit]已添加Python tag - OriginalGriff [/ edit]



我尝试过:



在这个阶段:

Hi,

I am trying to read some serial data (a string at this stage) in Python and I can send a string (" hello world! ") and am now trying to read a string.

[edit]Added Python tag - OriginalGriff[/edit]

What I have tried:

at this stage:

res =ser.readlines()
print(res)



之前:


previously:

res =ser.read()
print(res)



产生发送内容的第一个字符,我在想循环直到整个字符串被读取...


which produces the first character of what is sent, I was thinking looping until the whole string is read...

stringlength = len ser.res
for i in range(stringlenght)



刚试过以下内容:


Just tried the below:

res =ser.read(5)
print(res)

首先,你必须知道要读取多少个字符,它还会产生一个领先的b

The issue with this is firstly you have to know how many characters are to be read and also it produces a leading b

Quote:

b'hello'

我猜这意味着二进制编码,'标签可能会导致问题我假设有一种方法来压制这个?另外进一步摆弄导致试图循环,直到它看到/n...

I'm guessing that means binary coded and the ' tags could cause issues I am assuming there is a way to suppress this? Also further fiddling has lead to trying to loop until it see /n...

推荐答案

参见简短介绍 - pySerial 3.4文档 [ ^ ]和 pySerial API - pySerial 3.4文档 [ ^ ]。



Serial.read(size = 1)读取指定的字节数,默认为1。这就是为什么你只得到一个角色。



readline()读取直到收到新行字符。当另一方没有响应新的行终止答案时,不能使用此功能。它还需要在打开端口时指定超时。



readlines()调用 readline()直到出现 EOF 。但是这种情况永远不会发生在串行端口上,以便在超时时间后函数终止。



串行通信的一般问题是在没有固定大小的情况下检测包的结尾。



使用字符串的常用方法是使用新行字符或字符串中没有出现的字符,如空字节作为包指示符的结尾。



二进制数据可以带有包含数据长度的固定大小的标头作为前缀。因此,接收器知道他有多少数据字节。接收器首先读取头部字节,然后读取剩余的有效负载字节。



结论:

您必须知道使用的数据格式(称为协议)或在实现两个通信方时定义它。
See Short introduction — pySerial 3.4 documentation[^] and pySerial API — pySerial 3.4 documentation[^].

Serial.read(size=1) reads the specified number of bytes which is one by default. That is why you get only one character.

readline() reads until a new line character is received. This function can't be used when the other side is not responding with new line terminated answers. It requires also to specify a timeout when openening the port.

readlines() calls readline() until an EOF occurs. But such never happens with serial ports so that the function terminates when the timeout has elapsed.

It is a general problem of serial communication to detect the end of a "package" when those does not have a fixed size.

The common method when using strings is using the new line character or a character not occuring in strings like a null byte as end of package indicator.

Binary data can be prefixed with a header of fixed size containing the length of the data. So the receiver knows how many data bytes he has to expect. A receiver reads than the header bytes first and the remaining payload bytes afterwards.

Conclusion:
You have to know the used data format (called protocol) or define it when implementing both communication sides.


有点摆弄,这实际上是我想要的东西(让我脱离了洞我是的):

A bit of fiddling and this does pretty much what I want (got me out of the hole I was in):
#Will send and read from Com1 13/08/2018  

import serial
import time

#Set up Serial Port 1 to send and print out com port 
ser = serial.Serial(0)
ser.baudrate = 9600
ser.parity = serial.PARITY_NONE
ser.BYTESIZES = serial.EIGHTBITS
ser.stopbits = serial.STOPBITS_ONE
print(ser.portstr)
ser.close()


def Send():
    ser.open()
    #write text hello world to serial port
    print('Type a string:')
    text = input()
    ser.write(text.encode())
    ser.close()

def Receive():
    #open serial port and read characters from it
    ser.open()
    print('Receiving')
    res =ser.readline()
    res = res[:-2]
    print(res)
    ser.close()


while True:
    usrInput = input("Choose A: to receive, B: to Send")
    if usrInput == "A":
            Receive()
    if usrInput == "B": 
            Send()



感谢Ben Barker!


With thanks to Ben Barker!


这篇关于Python中的串行通信(3.xx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆