• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python操作串口的代码片段

python 水墨上仙 1267次浏览

为了使用python操作串口,首先需要下载相关模块: 1. pyserial http://pyserial.wiki.sourceforge.net/pySerial) 2. pywin32 (http://sourceforge.net/projects/pywin32/)

#coding=gb18030  
  
import sys,threading,time  
import serial  
import binascii,encodings  
import re  
import socket  
from struct import * 
class ComThread:   
    def __init__(self, Port=0):   
        self.l_serial = None
        self.alive = False
        self.waitEnd = None
        self.port = Port  
  
    def waiting(self):   
        if not self.waitEnd is None:   
            self.waitEnd.wait()  
  
    def SetStopEvent(self):   
        if not self.waitEnd is None:   
            self.waitEnd.set()  
        self.alive = False
        self.stop()  
  
    def start(self):   
        self.l_serial = serial.Serial();   
        self.l_serial.port = self.port  
        self.l_serial.baudrate = 9600
        self.l_serial.timeout = 2
        self.l_serial.open()  
        if self.l_serial.isOpen():  
            self.waitEnd = threading.Event()  
            self.alive = True
            self.thread_read = None
            self.thread_read = threading.Thread(target=self.FirstReader)  
            self.thread_read.setDaemon(1)  
            self.thread_read.start()  
            return True
        else:   
            return False
  
    def FirstReader(self):   
        while self.alive:   
            # 接收间隔  
            time.sleep(0.1)  
            try:   
                data = '' 
                n = self.l_serial.inWaiting()  
                if n:   
                    data = data + self.l_serial.read(n)  
                    for l in xrange(len(data)):  
                        print '%02X' % ord(data[l]),  
  
                    # 发送数据  
                    snddata = '' 
                    snddata += chr(97)  
                    tt = 0x12345678
                    snddata += pack('i', tt)  
                    snddata += chr(0x64)  
                    self.l_serial.write(snddata)  
                # 判断结束  
                if len(data) > 0 and ord(data[len(data)-1])==0x45:   
                    #pass  
                    break
                   
            except Exception, ex:   
                print str(ex)  
  
        self.waitEnd.set();   
        self.alive = False
  
    def stop(self):   
        self.alive = False
        self.thread_read.join()  
        if self.l_serial.isOpen():   
            self.l_serial.close()  
    
#测试用部分   
if __name__ == '__main__':   
    rt = ComThread()  
    try:   
        if rt.start():   
            rt.waiting()  
            rt.stop()  
        else:   
            pass;               
    except Exception,se:   
        print str(se)  
      
    if rt.alive:   
        rt.stop()  
  
    print '' 
    print 'End OK .'
    del rt 


喜欢 (0)
加载中……