Python 3 serial port operations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.