Python serial
- __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None)
- Parameters:
- port – Device name or None.
- baudrate (int) – Baud rate such as 9600 or 115200 etc.
- bytesize – Number of data bits. Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
- parity – Enable parity checking. Possible values: PARITY_NONE, PARITY_EVEN, PARITY_ODD PARITY_MARK, PARITY_SPACE
- stopbits – Number of stop bits. Possible values: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
- timeout (float) – Set a read timeout value.
- xonxoff (bool) – Enable software flow control.
- rtscts (bool) – Enable hardware (RTS/CTS) flow control.
- dsrdtr (bool) – Enable hardware (DSR/DTR) flow control.
- write_timeout (float) – Set a write timeout value.
- inter_byte_timeout (float) – Inter-character timeout, None to disable (default).
Raises:
- ValueError – Will be raised when parameter are out of range, e.g. baud rate, data bits.
- SerialException – In case the device can not be found or can not be configured.
1 import serial 2 import time 3 4 5 # initialize the serial ports 6 t = serial.Serial('com9',9600,timeout = 0.1) 7 #write commond list, commond should be ended with '\n' or '\r' 8 cmd_list = ['val?\r','temp?\r'] 9 a = '' 10 11 #t.close() # close port 12 #t.open() # open port 13 14 print t.portstr #print port number 15 for idx2 in cmd_list: 16 n = t.write(idx2) 17 #print n # 18 #s1 = t.readall() 19 s1 = t.readline() 20 #s1 = t.readlines() 21 print s1 22 23 #t = serial.Serial('com8',9600,timeout = 0.1) 24 #for idx1 in cmd_list: 25 # t.write(idx1) 26 # start = time.clock() 27 # while(True): 28 # s = t.read(1) 29 # a = a + s 30 # if(str(s) == '\r'): 31 # break 32 # now = time.clock() 33 # if(now - start) > 2: 34 # print 'timeout' 35 # break 36 # print a 37 # a = '' 38 39 t.close()