zoukankan      html  css  js  c++  java
  • Python 串口通讯

    摘要:

    pyserial module: https://github.com/tbusf/pyserial

    Python使用pyserial进行串口通信:http://blog.csdn.net/log1100/article/details/54380325

    串口通讯的python模块——pySerial :http://blog.csdn.net/dainiao01/article/details/5885122

    Parameters for the Serial class

    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 created
    baudrate=9600,          # baud rate
    bytesize=EIGHTBITS,     # number of databits
    parity=PARITY_NONE,     # enable parity checking
    stopbits=STOPBITS_ONE,  # number of stopbits
    timeout=None,           # set a timeout value, None for waiting forever
    xonxoff=0,              # enable software flow control
    rtscts=0,               # enable RTS/CTS flow control
    interCharTimeout=None   # Inter-character timeout, None to disable
    )

    Methods of Serial instances

    open()                  # open port
    close()                 # close port immediately
    setBaudrate(baudrate)   # change baud rate on an open port
    inWaiting()             # return the number of chars in the receive buffer
    read(size=1)            # read "size" characters
    write(s)                # write the string s to the port
    flushInput()            # flush input buffer, discarding all it's contents
    flushOutput()           # flush output buffer, abort output
    sendBreak()             # send break condition
    setRTS(level=1)         # set RTS line to specified logic level
    setDTR(level=1)         # set DTR line to specified logic level
    getCTS()                # return the state of the CTS line
    getDSR()                # return the state of the DSR line
    getRI()                 # return the state of the RI line
    getCD()                 # return the state of the CD line

    Attributes of Serial instances
    readonly

    portstr                 # device name
    BAUDRATES               # list of valid baudrates
    BYTESIZES               # list of valid byte sizes
    PARITIES                # list of valid parities
    STOPBITS                # list of valid stop bit widths

    New values can be assigned to the following attributes, the port will be reconfigured, even if it’s opened at that time:(即使是打开的情况下也会重新配置???liub)

    port                    # port name/number as set by the user
    baudrate                # current baud rate setting
    bytesize                # byte size in bits
    parity                  # parity setting
    stopbits                # stop bit with (1,2)
    timeout                 # timeout setting
    xonxoff                 # if Xon/Xoff flow control is enabled
    rtscts                  # if hardware flow control is enabled

    居然还有这么多好东西,看看下面:

    TCP/IP – serial bridge

    This program opens a TCP/IP port. When a connection is made to that port (e.g. with telnet) it forwards all data to the serial port and vice versa.

    This example only exports a raw socket connection. The next example below gives the client much more control over the remote serial port.

    • The serial port settings are set on the command line when starting the program.
    • There is no possibility to change settings from remote.
    • All data is passed through as-is.

    打开一个python shell

    import serial导入模块

    然后就可以用了

    ser = serial.Serial(0) 是打开第一个串口

    print ser.portstr 能看到第一个串口的标识,windows下是COM1

    ser.write(“hello") 就是往串口里面写数据

    ser.close() 就是关闭ser表示的串口

    ser.open() 会打开这个串口

    ser = serial.Serial('COM1', 115200) 来设置波特率,当然还有专门的函数

    data = ser.read()可以读一个字符

    data = ser.read(20) 是读20个字符

    data = ser.readline() 是读一行,以/n结束,要是没有/n就一直读,阻塞。

    data = ser.readlines()和ser.xreadlines()都需要设置超时时间

    ser.baudrate = 9600 设置波特率

    ser 来查看当前串口的状态

    ser.isOpen() 看看这个串口是否已经被打开

    import pyserial
    t=serial.Serial()
        t.port = 3
        t.baudrate = 115200
        t.open()
    t.write(chr(0x03))    #向串口输入ctrl+c
  • 相关阅读:
    yolo_to_onnx ValueError: need more tan 1 value to unpack
    yolo_to_onnx killed
    C++ 实现二维矩阵的加减乘等运算
    Leetcode 1013. Partition Array Into Three Parts With Equal Sum
    Leetcode 1014. Best Sightseeing Pair
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 219. Contains Duplicate II
    Leetcode 890. Find and Replace Pattern
    Leetcode 965. Univalued Binary Tree
    Leetcode 700. Search in a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/liuyang92/p/7482499.html
Copyright © 2011-2022 走看看