Python/Pyserial: reading incoming information from port

寵の児 提交于 2019-12-06 15:37:07

you can do it this way to test it. Firstly create a pair of ports in manage ports

First port: COM199 Second Port: COM188

Click Add Pair

On one console/script do the below steps:

>>> import serial
>>> ser = serial.Serial('COM196', 9600,timeout=None, parity=serial.PARITY_NONE, stopbits=serial.S
BITS_ONE, bytesize=serial.EIGHTBITS)
>>> print ser.portstr
COM196
>>> x = ser.read(5) # It will be waiting till it receives data
>>> print x
hello

On the other console, perform below steps:

>>> import serial
>>> s = serial.Serial('COM188')
>>> s.write("hello")
5L

you can test it this way (or) by creating python programs for each of the ports

x = ser.write("hello")
print x

You writing this as sended. It's not recieved info. Probably it writes the lenght of string you have sended. First you need to have a client side script which will response your sended information.

And you have to use something like this on it.

...     x = ser.read()          # read one byte
...     s = ser.read(10)        # read up to ten bytes (timeout)
...     line = ser.readline()   # read a '\n' terminated line
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!