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:

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 '
' or '
'
 8 cmd_list = ['val?
','temp?
']
 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) == '
'):
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()
原文地址:https://www.cnblogs.com/njuptlwh/p/7903869.html