python 串口接收


最簡單的接收然后打印代碼如下:

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=''

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM