Python serial

Posted njuptlwh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ‘\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()

 

以上是关于Python serial的主要内容,如果未能解决你的问题,请参考以下文章

python 有用的Python代码片段

Python 向 Postman 请求代码片段

Py serial readline在Python3中不起作用

Python 2.7:serial.serialutil.SerialException:端口已打开。如何关闭这个端口?

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档