因为有个需要用有源RFID搞资产管理的项目,需要用python读取读卡器的串口内容。于是装了pyserial模块,用了下很方便,整理下常用功能
一、
为了使用python操作串口,首先需要下载相关模块:
1. pyserial (http://pyserial.wiki.sourceforge.net/pySerial)
2. pywin32 (http://sourceforge.net/projects/pywin32/)
2,十六进制显示
十六进制显示的实质是把接收到的字符诸葛转换成其对应的ASCII码,然后将ASCII码值再转换成十六进制数显示出来,这样就可以显示特殊字符了。
在这里定义了一个函数,如hexShow(argv),代码如下:
3,十六进制发送
十六进制发送实质是发送十六进制格式的字符串,如'\xaa','\x0b'。重点在于怎么样把一个字符串转换成十六进制的格式,有两个误区:
1)'\x'+'aa'是不可以,涉及到转义符反斜杠
2)'\\x'+'aa'和r'\x'+'aa'也不可以,这样的打印结果虽然是\xaa,但赋给变量的值却是'\\xaa'
这里用到decode函数,
需要注意一点,如果字符串list的长度为奇数,则decode会报错,可以按照实际情况,用字符串的切片操作,在字符串的开头或结尾加一个'0'
假如在串口助手以十六进制发送字符串"abc",那么你在python中则这样操作“self.l_serial.write(”\x61\x62\x63") ”
当然,还有另外一个方法:
同样可以达到相同目的。
那么,串口方面的就整理完了
>>> import serial >>> ser = serial.Serial(0) # open first serial port >>> print ser.portstr # check which port was really used >>> ser.write("hello") # write a string >>> ser.close() # close portOpen named port at "19200,8,N,1", 1s timeout
>>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1) >>> x = ser.read() # read one byte >>> s = ser.read(10) # read up to ten bytes (timeout) >>> line = ser.readline() # read a '\n' terminated line >>> ser.close()Open second port at "38400,8,E,1", non blocking HW handshaking
>>> ser = serial.Serial(1, 38400, timeout=0, ... parity=serial.PARITY_EVEN, rtscts=1) >>> s = ser.read(100) # read up to one hundred bytes ... # or as much is in the bufferGet a Serial instance and configure/open it later
>>> ser = serial.Serial() >>> ser.baudrate = 19200 >>> ser.port = 0 >>> ser Serial<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() FalseBe carefully when using "readline". Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that "readlines" only works with a timeout. "readlines" depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.
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 )The port is immediately opened on object creation, if a port is given. It is not opened if port is None.
timeout=None # wait forever timeout=0 # non-blocking mode (return immediately on read) timeout=x # set timeout to x seconds (float allowed)
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
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 widthsNew values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time:
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
serial.SerialException
serial.PARITY_NONE serial.PARITY_EVEN serial.PARITY_ODDstopbits:
serial.STOPBITS_ONE serial.STOPBITS_TWObytesize:
serial.FIVEBITS serial.SIXBITS serial.SEVENBITS serial.EIGHTBITS