pyserial read.line()不打印
我无法打印写入串行端口的任何内容。它不显示或只输出: '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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将字节大小传递给
readline()
。这对我有用。例如:pass byte size to
readline()
. This worked for me. E.g.: