最简单的接收然后打印代码如下:
1
2
3
4
5
6
7
8
9
10
|
import serial ser = serial.Serial( 'com4' , 115200 ) data = '' while 1 : while ser.inWaiting() > 0 : data + = ser.read( 1 ) if data ! = '': print data data = '' |
扫描那些串口存在的:
#!/usr/bin/env python # -*- coding: utf-8 -* import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) if len(port_list) <= 0: print "The Serial port can't find!" else: port_list_0 =list(port_list[0]) port_serial = port_list_0[0] ser = serial.Serial(port_serial,115200,timeout = 520) print "serial:",ser.name
在结合一下就是下面代码:
#!/usr/bin/env python # -*- coding: utf-8 -* import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) if len(port_list) <= 0: print "The Serial port can't find!" else: port_list_0 =list(port_list[0]) port_serial = port_list_0[0] ser = serial.Serial(port_serial,115200,timeout = 520) print "Link...",ser.name data = '' while 1 : while ser.inWaiting() > 0: data += ser.read(1) if data != '': print data data=''