python 对串口读写心得

import serial
import time
import re
import os
from time import sleep


class useSerialPort:
def __init__(self,port,bauterate):
#print "Load useSerialPort module"
self.port      = port
self.bauterate = bauterate
self.ser = serial.Serial(port,bauterate)
self.ser.timeout = 0.01
#print self.port


def readcom(self):
#'we can deal with the UART output'
clear_time = 0
while 1:
sleep(0.05)
reading = self.ser.readall()
if len(reading) >0:
LOG = self.ser.port + ":" + reading
print LOG
self.__conservelog(LOG)
clear_time = 0
else:
clear_time = clear_time + 1


if clear_time > 10:
break


#support input from Console
def write_serial_con(self):
while 1:
command = raw_input() 
for character in command:
self.ser.write(character)
self.ser.write('\r')
    
def close_serial(self):
self.ser.close()


def write_serial(self,command):
for character in command:
self.ser.write(character)
self.ser.write('\r')
self.readcom()


def __conservelog(self,read_data):
with open('testlog','a+') as writelog:
writelog.write(time.strftime('%Y-%m-%d-%H:%M:%S',time.localtime(time.time()))+': '+read_data+'\r')

你可能感兴趣的:(python 对串口读写心得)