您的位置:

python模拟串口(python控制串口设备)

本文目录一览:

如何用python写个串口通信的程序?

打开串口后启动一个线程来监听串口数据的进入,有数据时,就做数据的处理。

用python写串口通信程序的示例:

#coding=gb18030

import sys,threading,time;

import serial;

import binascii,encodings;

import re;

import socket;

class ReadThread:

def __init__(self, Output=None, Port=0, Log=None, i_FirstMethod=True):

self.l_serial = None;

self.alive = False;

self.waitEnd = None;

self.bFirstMethod = i_FirstMethod;

self.sendport = '';

self.log = Log;

self.output = Output;

self.port = Port;

self.re_num = None;

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.re_num = re.compile('\d');

try:

if not self.output is None:

self.output.WriteTex;

if not self.log is None:

self.log.info;

self.l_serial.open();

except Exception, ex:

if self.l_serial.isOpen():

self.l_serial.close();

self.l_serial = None;

if not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.error(u'%s' % ex);

return False;

if self.l_serial.isOpen():

if not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.info;

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:

if not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.info;

return False;

def InitHead(self):

try:

time.sleep(3);

if not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.info;

self.l_serial.flushInput();

self.l_serial.write('\x11');

data1 = self.l_serial.read(1024);

except ValueError,ex:

if not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.error(u'%s' % ex);

self.SetStopEvent();

return;

if not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.info;

self.output.WriteText(u'===================================\r\n');

def SendData(self, i_msg):

lmsg = '';

isOK = False;

if isinstance(i_msg, unicode):

lmsg = i_msg.encode('gb18030');

lmsg = i_msg;

pass

except Exception, ex:

pass;

return isOK;

def FirstReader(self):

data1 = '';

isQuanJiao = True;

isFirstMethod = True;

isEnd = True;

readCount = 0;

saveCount = 0;

RepPos = 0;

#read Head Infor content

self.InitHead();

while self.alive:

try:

data = '';

n = self.l_serial.inWaiting();

if n:

data = data + self.l_serial.read(n);

#print binascii.b2a_hex(data),

for l in xrange(len(data)):

if ord(data[l])==0x8E:

isQuanJiao = True;

continue;

if ord(data[l])==0x8F:

isQuanJiao = False;

continue;

if ord(data[l]) == 0x80 or ord(data[l]) == 0x00:

if len(data1)10:

if not self.re_num.search(data1,1) is None:

saveCount = saveCount + 1;

if RepPos==0:

RepPos = self.output.GetInsertionPoint();

self.output.Remove(RepPos,self.output.GetLastPosition());

self.SendData(data1);

data1 = '';

continue;

except Exception, ex:

if not self.log is None:

self.log.error(u'%s' % 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 not self.output is None:

self.output.WriteText;

if not self.log is None:

self.log.info;

def printHex(self, s):

s1 = binascii.b2a_hex(s);

print s1;

if __name__ == '__main__':

rt = ReadThread();

f = open("sendport.cfg", "r")

rt.sendport = f.read()

f.close()

try:

if rt.start():

rt.waiting();

rt.stop();

else:

pass;

except Exception,se:

print str(se);

if rt.alive:

rt.stop();

print 'End OK .';

del rt;

如何用python实现串口通信

Python非常适合写一些测试的脚本,如快速的串口通信测试等。如果使用VC++ QT开发,可能用时较多,使用python,如果掌握使用方法,可以直接读写测试,配合设备或是串口助手,很快验证与实现。

Python有没有现成的串口API直接调用呢?经过实践验证,需要安装一个叫 Pyserial的组件即可。这个可以在github上下载。

在windows 7 64bit 上可以使用吗?当然可以使用,我安装的python3.5为64位的。把下载后的文件,其中有一个serial的文件夹,拷贝到python35安装路径, C:\Python35\Lib\site-packages\serial

网上可以搜一下windows的安装包,安装完也是:C:\Python35\Lib\site-packages\serial ,可以用最新的版本,替换即可。

测试的方法:在python IDE里测试:

import serial

这里如果报错,是python版本与pyserial版本没有配合好。如果正常,不返回,即可以导入serial模块。

ser=serial.Serial("COM5",115200)

这里为COM5,115200的波特率。如果打不开,请检查安装环境。

ser.write('hello,serial test'.encode())

17

发送测试(如果返回字节数,说明返回成功),这里需要转换一个编码为字节。

以上测试,可以使用现在的设备或是串口助手,如安装Virtual Serial Port Driver 7.2 虚拟串口软件,设置一对串口,进行自发自收的测试。

print(ser.readline())

b'abcdefg\r\n'

这里是串口接收,有接收的超时。设备或是串口助手发送一个字符串,以回车换行结束,这里就可以收到打印出来。

也可以用ser.read(),这里只接收一个字符来实现。

上面已经实现了基本的串口操作。

关闭串口为:

ser.close()

如果使用python,一般写个py文件,就像windows bat 批处理一样,这是python强大的地方。如果写一个py脚本呢?其实只要把上面的命令,一条条写下来,就是一个脚本,测试如下:

import serialser=serial.Serial("COM5",115200,timeout=0.5)for i in range(0,100-1):ser.write('hello\r\n'.encode())print(ser.readline());ser.close()

如何用串口工具模拟器向python发送数据

串口模块的波特率比较特别,找了几个串口工具都不支持。。。所以,干脆用python自己来写了,其实已经好奇好久了,别人的工具各种不顺手。

需要pyserial的支持,兼容各种平台,不需要新编译二进制文件。

先贴一个定时发送的代码:

import serial

import time

ser = serial.Serial('/dev/ttyUSB0', 250000, timeout=1)

print ser.isOpen()

words="gggggggggggggggg"

while (1):

print "send 256x\""+words+"\" to remotes"

startTime = time.time()

times = 256

while (times):

times -= 1

s = ser.write(words)

endTime = time.time()

print "use time: "+str(endTime-startTime)

print ""

time.sleep(5)

ser.close()

Python如何进行多串口通信?一个串口控制电机 一个串口采集数据?

下载 pyserial包

def OpenCom(self,*args): #设置端口和波特率 selComPort =‘com2’ #波特率 selBaudRate =9600 #奇偶校验 selParity = 'N' try: if(not self.mySerial): self.mySerial = serial.Serial(port=selComPort, baudrate=selBaudRate,bytesize=8,parity=selParity,stopbits=1,timeout=5) else: if(self.mySerial.isOpen()): self.mySerial.close() self.mySerial = serial.Serial(port=selComPort, baudrate=selBaudRate, bytesize=8, parity=selParity, stopbits=1, timeout=5) self.lblInfo['text'] = '打开成功!' except Exception as ex: self.lblInfo['text'] = '打开失败!'

#使用com口发送modbus协议给终端设备。

def btnEmId_Click(self):

barray = bytearray([0x05, 0x03, 0xA#, 0x54, 0x00, 0x08])

vOldEmId = self.txbOldEmId.get()

vNewEmId = self.txbNewEmId.get()

barray[0] = int(vOldEmId)

barray[5] = int(vNewEmId)

#crc校验

strInput = utils.crc16_append(barray)

print(barray)

n = self.mySerial.write(barray)

if(n 0):

str = self.mySerial.readall()

self.lblInfo['text'] = 'success!'

# for s in str:

# print (hex(s))

else:

self.lblInfo['text'] = 'error!'