1. 程式人生 > >模擬FIBERPRO偏振消光比測試儀ER2200的Python程序

模擬FIBERPRO偏振消光比測試儀ER2200的Python程序

span int ctrl targe nal 版權 隨筆 too spa

版權聲明:本文為博主原創文章,歡迎轉載,並請註明出處。聯系方式:[email protected]

在上一篇隨筆中采用VSPD、ModbusTool模擬串口、MODBUS TCP設備進行Python采集軟件開發寫了模擬ER2200的程序,後來進行了完善:數據進行隨機變化,打印輸出進行了完善。

# -*- coding:utf-8 -*-

u"""FIBERPRO偏振消光比測試儀ER2200模擬程序"""

__author__ = zhengbiqing [email protected]

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程序