pyserial read.line()不打印

发布于 2025-01-18 14:02:35 字数 2262 浏览 1 评论 0原文

我无法打印写入串行端口的任何内容。它不显示或只输出: 'b '

我已经尝试了很多不同的东西,但无法读取和打印它。

from doctest import IGNORE_EXCEPTION_DETAIL
from logging import exception
import serial
import serial.tools.list_ports
# Here is the link to the documentation I referenced: https://docs.python.org/3/library/multiprocessing.html
from multiprocessing import Process
import time
import io


def sendData(data, port, baud):
time.sleep(0)
with serial.Serial(port=port, baudrate=baud, timeout=0) as ser:
ser.reset_input_buffer()
ser.reset_output_buffer()
ser.flushInput()
ser.flushOutput()
data += "\n"
print()
print("Data that was sent: ", data)
ser.write(data.encode())
ser.close()


def receiveData(port, baud):
time.sleep(0)
with serial.Serial(port=port, baudrate=baud, timeout=0) as ser:
ser.reset_input_buffer()
ser.reset_output_buffer()
ser.flushInput()
ser.flushOutput()
line = ser.readline().strip()
print()
print("Data that was received:", line.decode())
ser.close()


def main():

if __name__ == "__main__":
spObject = serial.tools.list_ports.comports()
serialPorts = []
print()
for i in spObject:
serialPorts.append(i.device)
print('Available ports:')
for i in range(len(serialPorts)):
print('%i. %s' % (i+1, serialPorts[i]))
selectedPort = int(input('Please select a port to send data to: '))
while selectedPort-1 not in range(len(serialPorts)):
print('Invalid input')
selectedPort = input('Please select a port: ')
tty = serialPorts[selectedPort-1]
data = input(
'Please enter the data you want to send over the serial port: ')
print()

spObject2 = serial.tools.list_ports.comports()
serialPorts2 = []
for i in spObject2:
serialPorts2.append(i.device)
print('Available ports:')
for i in range(len(serialPorts2)):
print('%i. %s' % (i+1, serialPorts2[i]))
selectedPort2 = int(input('Please select a port to listen on: '))
while selectedPort2-1 not in range(len(serialPorts2)):
print('Invalid input')
selectedPort2 = input('Please select a port: ')

lport = serialPorts2[selectedPort2-1]

p1 = Process(target=sendData(data, tty, 115200))
p2 = Process(target=receiveData(lport, 115200))

p2.start()
p1.start()

p2.join()
p1.join()
print()
print("Done!")
print()


main()

我尝试使用“utf-8”、“ascii”等读取行解码,但没有任何效果。我是否忘记添加一些内容?我也尝试过 readlines()、read() 等。数据是否发送到端口?

I am having trouble to print whatever I write to the serial port. It doesn't display or only outputs: 'b '

I have tried so many different things, but cannot get it read and print.

from doctest import IGNORE_EXCEPTION_DETAIL
from logging import exception
import serial
import serial.tools.list_ports
# Here is the link to the documentation I referenced: https://docs.python.org/3/library/multiprocessing.html
from multiprocessing import Process
import time
import io


def sendData(data, port, baud):
time.sleep(0)
with serial.Serial(port=port, baudrate=baud, timeout=0) as ser:
ser.reset_input_buffer()
ser.reset_output_buffer()
ser.flushInput()
ser.flushOutput()
data += "\n"
print()
print("Data that was sent: ", data)
ser.write(data.encode())
ser.close()


def receiveData(port, baud):
time.sleep(0)
with serial.Serial(port=port, baudrate=baud, timeout=0) as ser:
ser.reset_input_buffer()
ser.reset_output_buffer()
ser.flushInput()
ser.flushOutput()
line = ser.readline().strip()
print()
print("Data that was received:", line.decode())
ser.close()


def main():

if __name__ == "__main__":
spObject = serial.tools.list_ports.comports()
serialPorts = []
print()
for i in spObject:
serialPorts.append(i.device)
print('Available ports:')
for i in range(len(serialPorts)):
print('%i. %s' % (i+1, serialPorts[i]))
selectedPort = int(input('Please select a port to send data to: '))
while selectedPort-1 not in range(len(serialPorts)):
print('Invalid input')
selectedPort = input('Please select a port: ')
tty = serialPorts[selectedPort-1]
data = input(
'Please enter the data you want to send over the serial port: ')
print()

spObject2 = serial.tools.list_ports.comports()
serialPorts2 = []
for i in spObject2:
serialPorts2.append(i.device)
print('Available ports:')
for i in range(len(serialPorts2)):
print('%i. %s' % (i+1, serialPorts2[i]))
selectedPort2 = int(input('Please select a port to listen on: '))
while selectedPort2-1 not in range(len(serialPorts2)):
print('Invalid input')
selectedPort2 = input('Please select a port: ')

lport = serialPorts2[selectedPort2-1]

p1 = Process(target=sendData(data, tty, 115200))
p2 = Process(target=receiveData(lport, 115200))

p2.start()
p1.start()

p2.join()
p1.join()
print()
print("Done!")
print()


main()

I tried read line decode with “utf-8”, “ascii”, etc. Nothing has worked. Am I forgetting to include something? I have also tried readlines(), read(), etc. Is the data even sending to the port?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生池水覆流年 2025-01-25 14:02:35

将字节大小传递给readline()。这对我有用。例如:

line = ser.readline(2)  #receive two bytes

pass byte size to readline(). This worked for me. E.g.:

line = ser.readline(2)  #receive two bytes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文