標籤:字元 個數 pow html sop ctr targe span time()
著作權聲明:本文為博主原創文章,歡迎轉載,並請註明出處。連絡方式:[email protected]
在上一篇隨筆中採用VSPD、ModbusTool類比串口、MODBUS TCP裝置進行Python採集軟體開發寫了類比ER2200的程式,後來進行了完善:資料進行隨機變化,列印輸出進行了完善。
# -*- coding:utf-8 -*-u"""FIBERPRO偏振消光比測試儀ER2200類比程式"""__author__ = ‘zhengbiqing [email protected]‘import binasciiimport signalimport sysfrom random import randomimport timeimport serialser = 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程式