pyserial

Python串口异步通信

风格不统一 提交于 2020-02-08 17:57:34
Python串口异步通信(串口接收中断) 串口是计算机上一种非常通用的设备通信协议。pyserial模块封装了python对串口的访问,为多平台的使用提供了统一的接口。 安装库 Python要使用串口功能需要导入这两个库: pyserial (基本串口功能) pip3 install pyserial pyserial-asyncio (实现异步功能需要这个库) pip3 install pyserial - asyncio 获取串口名称 使用serial.tools.list_ports.comports()函数会返回一个ListPortInfo类型的列表 import serial import serial . tools . list_ports class serial_class : def get_port ( self ) : self . port_list = serial . tools . list_ports . comports ( ) return self . port_list serial_port = serial_class ( ) port = serial_port . get_port ( ) for i in range ( 0 , len ( port ) ) : print ( tuple ( [ port [ i ] .

pyserial can't write the complete bytes to serial port (hex data)

人盡茶涼 提交于 2020-01-25 10:50:06
问题 encode('utf-16') will write the hexadecimal to serial port, but it is sending only 5 bit's on serial port. port = 'COM4' baud = 9600 ser = serial.Serial(port, baud, timeout=1) ser.reset_input_buffer() ser.isOpen() command = '4c 31 32 33' ser.write(bytearray(str(command), 'utf-16')) ser.close() ----------------------------------------- a = ser.readline().decode('utf-16') s = ''.join([chr(int(x, 16)) for x in a.split()]) Output: L Expected output: L123 来源: https://stackoverflow.com/questions

Can't write multiple times to same port (pyserial)

烈酒焚心 提交于 2020-01-23 12:38:37
问题 Solved: dsrdtr=True shouldn't have been used for software, only on the hardware being used Hi I'm trying to write telegrams to serial port and can send one successfully. If I send more than one nothing happens. The script has to be closed at which point the first telegram is successfully received. The manufacturer suggests a break of 50ms between telegrams, even with breaks >5s it still fails. s = serial.Serial( port='COM3', baudrate=9600, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS

Can't write multiple times to same port (pyserial)

浪尽此生 提交于 2020-01-23 12:37:06
问题 Solved: dsrdtr=True shouldn't have been used for software, only on the hardware being used Hi I'm trying to write telegrams to serial port and can send one successfully. If I send more than one nothing happens. The script has to be closed at which point the first telegram is successfully received. The manufacturer suggests a break of 50ms between telegrams, even with breaks >5s it still fails. s = serial.Serial( port='COM3', baudrate=9600, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS

unsupported operand type(s) for %: 'bytes' and 'str'

非 Y 不嫁゛ 提交于 2020-01-17 04:25:11
问题 I receive the error in the following line: command = input("please type command.example open 1") #call the serial_connection() function ser.write(b"%d\r\n"%command) Essentially I want the input written by the user and parse it into ser.write, without asking for the input and directly putting the string into ser.write such as: ser.write(b'close1\r\n') worked fine, the problem only occurred when i try to use the result of the input as a string to include in ser.write A bit more of the code: ser

safe and fast method for communicating a mix list of integers and booleans between pyserial and Arduino

北慕城南 提交于 2020-01-16 09:05:14
问题 I want to send and receive a mix list of integers and booleans between pyserial and an Arduino. I have figured the Arduino to pyserial out, well partly: Arduino code: ... bool var1 = ...; int var2 = ...; ... void setup() { ... Serial.begin(9600); ... } void loop() { ... // here I send the data in CSV format Serial.print(var1); Serial.print(", "); Serial.print(var2); Serial.print(", "); ... Serial.println(var_n); ... } and the pyserial side: import serial serPort = serial.Serial("COM7")

serial port getting engaged after reading single command in python

浪尽此生 提交于 2020-01-16 08:41:25
问题 I am making a connection with python using pyserial with a UART port. As I send the command using serial.write my output is received but my serial port does not break connection. As I need to send a second command also to receive the output. Please guide me on this. I have also used ser.close still I am not able to close the port. import serial ser = serial.Serial( port='COM5', \ baudrate=9600, \ parity=serial.PARITY_NONE, \ stopbits=serial.STOPBITS_ONE, \ bytesize=serial.EIGHTBITS, \ timeout

serial port getting engaged after reading single command in python

混江龙づ霸主 提交于 2020-01-16 08:41:11
问题 I am making a connection with python using pyserial with a UART port. As I send the command using serial.write my output is received but my serial port does not break connection. As I need to send a second command also to receive the output. Please guide me on this. I have also used ser.close still I am not able to close the port. import serial ser = serial.Serial( port='COM5', \ baudrate=9600, \ parity=serial.PARITY_NONE, \ stopbits=serial.STOPBITS_ONE, \ bytesize=serial.EIGHTBITS, \ timeout

python的串口通信(pyserial)

我们两清 提交于 2020-01-14 07:56:11
python的串口通信(pyserial) 1 使用用硬件 树莓派,CH340串口转USB两个,PC 2 环境安装 树莓派我使用的是自带系统,环境都已经配好了,PC这边我安装了的ubunt子系统,有python环境,那么就需要安装pyserial库和CH340的驱动 sudo apt-get update sudo apt install python-pip pip install --upgrade pip pip install pyserial 可以使用命令查看本机的串口 python -m serial.tools.list_ports python3 -m serial.tools.list_ports PC的话将会列举一堆,需要取设备管理器中找到对应的COM口号,例如我串口是COM8,则是用的/dev/ttyS5 树莓派的话将会显示两个,/dev/ttyAMA0是GPIO处的串口,/dev/ttyUSB0则是我插入的串口转USB模块 pi@raspberrypi:~ $ python -m serial.tools.list_ports /dev/ttyAMA0 /dev/ttyUSB0 3 收发测试 PC lissettecarlr@lissettecarlr ~ % python Python 2.7.12 (default, Nov 12 2018, 14:36

using serial port in python3 asyncio

♀尐吖头ヾ 提交于 2020-01-13 08:18:31
问题 i'm trying and, so far, failing to use python asyncio to access a serial port. i'd really appreciate any tips on using the new python async framework on a simple fd. Cheers! James 回答1: It's other way using FD import asyncio import serial s = serial.Serial('/dev/pts/13', 9600) def test_serial(): ''' read a line and print. ''' text = "" msg = s.read().decode() while (msg != '\n'): text += msg msg = s.read().decode() print(text) loop.call_soon(s.write, "ok\n".encode()) loop = asyncio.get_event