标签:字符 个数 pow html sop ctr targe span time()
版权声明:本文为博主原创文章,欢迎转载,并请注明出处。联系方式:460356155@qq.com
在上一篇随笔中采用VSPD、ModbusTool模拟串口、MODBUS TCP设备进行Python采集软件开发写了模拟ER2200的程序,后来进行了完善:数据进行随机变化,打印输出进行了完善。
# -*- coding:utf-8 -*- u"""FIBERPRO偏振消光比测试仪ER2200模拟程序""" __author__ = ‘zhengbiqing 460356155@qq.com‘ import binascii import signal import sys from random import random import time import serial ser = serial.Serial(‘COM4‘, 115200, timeout=1) print ‘Welcome to ‘, __doc__, ‘,‘, ‘device is %s‘ % (‘Ready‘ if ser.isOpen() else ‘Error‘) print ‘Author is ‘, __author__ # ctrl+c处理函数 def signal_handler(signal, frame): ser.close() print ‘You pressed Ctrl+C! ‘, ‘device is %s‘ % (‘Closed‘ if not ser.isOpen() else ‘Error‘) print ‘Goodbye!‘ sys.exit(0) # 程序是死循环,通过ctrl+c退出,为了在退出时关闭串口,捕获该信号 signal.signal(signal.SIGINT, signal_handler) while True: data = ser.read(7) if len(data): # 调试打印,b2a_hex(data)是把字符串data转换为十六进制数 now = time.localtime(time.time()) HMS = time.strftime(‘%H:%M:%S‘, now) print ‘%s <- %s(%r)‘ % (HMS, binascii.b2a_hex(data), data) if data == ‘read?\r\n‘: # 三个数字分别表示被测光的消光比,偏振角度,和光功率 classtalk = 19.35 + round(random(), 2) angle = 53.47 + round(random(), 2) power = -5.17 + round(random(), 2) sendstr = str(classtalk) + ‘,‘ + str(angle) + ‘,‘ + str(power) + ‘\r‘ now = time.localtime(time.time()) HMS = time.strftime(‘%H:%M:%S‘, now) print ‘%s -> %s‘ % (HMS, sendstr) ser.write(sendstr)
模拟FIBERPRO偏振消光比测试仪ER2200的Python程序
标签:字符 个数 pow html sop ctr targe span time()
原文地址:http://www.cnblogs.com/zhengbiqing/p/6917241.html