Using Python to debug the serial port

Source: Internet
Author: User

Serial port module baud rate is very special, find a few serial tools are not supported ... So, simply use Python to write their own, in fact, have been curious for a long time, the tools of others are not handy.

Requires pyserial support, compatible with a variety of platforms, and does not require new binaries to be compiled.

First, put a timed send code:

ImportSerialImportTimeser= Serial. Serial ('/dev/ttyusb0', 250000, Timeout=1)PrintSer.isopen () words='\x72\x02\x02\x52\x02\x12\x02\x02\x42\x02\x62\02' while(1):        Print "Send"+words+"To remotes"s=Ser.write (words) time.sleep (5) Ser.close ()

Then there are some other ways:

1. Use serial number to open the serial port: ser = serial. Serial (0). But, how to determine the serial number???

2. Look at the name of the serial port, Aha, using the 1 method to open the serial port, you can produce the name of the serial port: print Ser.portstr

3. Precedent an entity, then open:

>>> ser = serial. Serial ()>>> ser.baudrate = 19200>>> Ser.port = 0>>> serserial<id =0XA81C10, open=false> (port='COM1', baudrate=19200, bytesize=8, parity=' N ', Stopbits=1, Timeout=none, xonxoff=0, rtscts=0)>>> ser.open ()>>> Ser.isopen () True>>> ser.close ()>>> ser.isopen () False

4. Centralized way to read data

>>> ser = serial. Serial ('/dev/ttys1', 19200, timeout=1)>>> x = Ser.read ()          #  read one byte>>> s = ser.read (Ten)        #  read up to ten bytes (timeout)>>&G T line = Ser.readline ()   #  read a '/n ' terminatedline >>> ser.close ()

In which, if just serial debugging, direct Ser.read (1000), this will read the value of the direct printing to the screen.

5. All parameters

Ser =Serial. Serial (Port=none,#Number of device, numbering starts at#zero. If everything fails, the user#can specify a device string, note#That this isn ' t portable anymore#if no port is specified an unconfigured#An closed serial port object is createdbaudrate=9600,#baud RateBytesize=eightbits,#Number of databitsParity=parity_none,#Enable parity checkingStopbits=stopbits_one,#Number of StopBitsTimeout=none,#set a timeout value, None for waiting ForeverXonxoff=0,#enable software flow controlRtscts=0,#Enable Rts/cts flow controlInterchartimeout=none#inter-character Timeout, None to disable)

6. Exception:serial. Serialexception

Using Python to debug the serial port

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.