In the python built-in class library, there is no module for serial port operations, and additional reference is required.
The following two modules must be referenced in Python 2:
1. pyserial((pyserial-2.5.win32.exe) 2. pywin32 (http://sourceforge.net/projects/pywin32)
Only one module needs to be referenced in Python 3.
The pyserial((pyserial-py3k-2.5.win32.exe) module contains the serialwin32.py file.
Start reading data from hexdata.txt to the serial port.
_ Author __ = ' Zhangps '
Import Serial
From Time Import Sleep
SerialPort = serial. serial ( " com3 " , 115200, timeout = 1)
readhandle = open ( "hex data.txt " , " r " , encoding = " utf8 " )
Try:
ForFilelineInReadhandle. readlines ():
Writedata = bytes. fromhex (fileline)
SerialPort. Write (writedata)
Sleep (0.05)
ExceptEnvironmenterror as ERR:
Print(ERR)
Finally:
Readhandle. Close ()
SerialPort. Close ()
Sleep in seconds
The hexdata. TX format is as follows:
7E 7E 08 02 14 EE 0d 01 1A F0 16
7E 7E 09 02 14 32 F3 01 17 80 00 48
7E 7E 0b 02 14 99 0d 01 15 fa 0f C3 0C A7