代码拉取完成,页面将自动刷新
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-
# Author: Bary Chen
import struct
import serial
# CONSTANTS
VERSION = 13 # this is the version of the messaging protocol
TARGET_DEVICE = 2
HOST_DEVICE = 0
# COMMANDS TO THE LED CONTROLLER
GET_STATUS = 1
LOAD_PULSE_TRAIN = 2
RUN_CONTINUOUS = 3
STOP_PULSE_TRAIN = 4
STOP_CONTINUOUS = 5
GET_PULSE_DATA = 6
LOAD_CALIBRATION = 7
SEND_CALIBRATION = 8
GET_DEVICE_DATA = 9
CONFIGURE_CONTINUOUS = 10
# COMMANDS FROM THE LED CONTROLLER
STATUS_RESPONSE = 128
ERROR_RESPONSE = 129
PULSE_TRAIN_LOADED = 130
PULSE_TRAIN_DATA = 131
STARTED_CONTINUOUS = 132
STOPPED_CONTINUOUS = 133
CALIBRATION_LOADED = 134
CALIBRATION_DATA = 135
PULSE_TRAIN_STOPPED = 136
DEVICE_DATA = 137
CONFIGURED_CONTINUOUS = 138
LOAD_PULSE_TRAIN_TEST_ONCE = 126
LOAD_PULSE_TRAIN_TEST = 127
ASCII_MESSAGE = 255
# ERROR CODES FROM LED CONTROLLER
LOST_SYNC = 1
WRONG_SEQUENCE_NUMBER = 2
WRONG_VERSION = 3
WRONG_DEVICE_ID = 4
UNKNOWN_COMMAND = 5
BAD_CRC = 6
PULSE_TRAIN_IS_RUNNING = 7
BAD_EEPROM_CRC = 8
CONTINUOUS_NOT_ON = 9
EXTERNALLY_HALTED = 10
LED_FAULT = 11
GOOD_CRC = 100
HERE = 101
# Calculate CRC
CRCTable = [
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
]
# Error code to error string hash
errorCode2String = {
1: 'Lost message Synchronisation',
2: 'Wrong sequence number received',
3: 'Wrong version',
4: 'Wrong device ID',
5: 'Unknown command',
6: 'Bad CRC',
7: 'Pulse Train Running, upload rejected',
8: 'Bad CRC on checking EEPROM',
9: 'Attempt to stop continuous while not on',
10: 'The external halt from the host is active',
11: 'LED Fault',
100: 'Good CRC', # used for debugging
101: ' HERE!' # used for debugging
}
# Pulse status code to string hash
statusCode2String = {
2: 'PULSE_TRAIN_RUNNING',
3: 'IN_PULSE',
4: 'ARMED',
5: 'UN_ARMED',
6: 'HALTED by the host',
7: 'STOPPED by a message',
8: 'GLOBAL FAULT asserted'
}
# Continuous Status code to string
continuousStatus2String = {
0: 'CONTINUOUS_OFF', # continuous LED(s) is(are) off
1: 'CONTINUOUS_ON', # continuous LED(s) is(are) on
2: 'CONTINUOUS_FAULTED',
}
eepromCRC2String = {
0: 'NOK',
1: 'OK',
}
anodeV2String = {
0: 'No Voltage',
1: 'Voltage Present',
}
externalHalt2String = {
1: 'Halt Inactive (Enabled)',
0: 'Halt Active (Disabled)'
}
global_fault2String = {
0: 'No Error',
1: 'LED Fault',
2: 'External Halt Exerted',
3: 'Disabled by Controller',
4: 'Over current or Photo-Integrator Trip',
5: 'Undetermined Error'
}
def compute_crc(message):
crc = 0xBEEF
for i in message:
crc = ((crc >> 8) ^ CRCTable[(crc ^ i) & 0xff])
return 0xffff - crc
def send_message(message_data, command, ser, logger, message_no: int):
try:
message = struct.pack('!HBLBBB', 0xDEAD, message_no, 3 + len(message_data),
VERSION, TARGET_DEVICE, command) + message_data
message += struct.pack('!H', compute_crc(message))
if ser and ser.is_open:
ser.flushInput() # 清缓冲
ser.write(message)
else:
if logger:
logger.critical(f'Serial is not opened')
except Exception as e:
logger.critical(e)
def read_data(ser: serial.Serial, logger, message_no: int):
if ser and ser.is_open:
try:
in_message = False
wait_num = ser.inWaiting()
if wait_num > 0:
data_buffer = ser.read(wait_num)
payload_length = 0
sync = 0
response_message_no = 0
command = 0
device_id = 0
if in_message is False:
if len(data_buffer) >= 10:
sync, response_message_no, payload_length, version, device_id, command = \
struct.unpack('!HBLBBB', data_buffer[0:10])
in_message = True
logger.info(f'Light Source received command: {command}')
if in_message is True: # we are in a message so see if we are at the end
if len(data_buffer) >= 7 + payload_length + 2: # we have the complete message
calculated_crc = compute_crc(data_buffer[0: 3 + 4 + payload_length])
message_crc = struct.unpack('!H', data_buffer[7 + payload_length:7 + payload_length + 2])[0]
if message_crc == calculated_crc:
if sync != 0xDEAD:
logger.critical(f'Wrong Sync character: {sync}, should be oxDEAD')
return False
elif message_no != response_message_no and command != ASCII_MESSAGE:
logger.critical(f'Wrong Response message number, '
f'message no {message_no}, response message no {response_message_no}')
return False
elif device_id != HOST_DEVICE:
logger.critical('Wrong Device number')
return False
logger.info(f'Light Source Get Command: {command}')
if command == DEVICE_DATA:
hw_version, sw_major_version, sw_minor_version, protocol_version, eeprom_crc = \
struct.unpack('!BBBBB', data_buffer[10:15])
if protocol_version == VERSION:
return True
else:
logger.critical(f'Get wrong protocol version: {protocol_version}')
return False
elif command == ERROR_RESPONSE:
error_num = struct.unpack('!B', data_buffer[10:11])[0]
logger.critical('Got Error %d from the LED Controller: %s' % (
error_num, errorCode2String[error_num]))
return False
elif command == PULSE_TRAIN_LOADED:
num_pulses = struct.unpack('!B', data_buffer[10:11])[0]
logger.debug('Pulse train of %d pulses loaded' % num_pulses)
return True
elif command == STARTED_CONTINUOUS:
logger.debug('Started continuous mode')
return True
elif command == STOPPED_CONTINUOUS:
logger.debug('Continuous mode stopped')
return True
elif command == PULSE_TRAIN_STOPPED:
logger.debug('Pulse Train Stopped')
return True
elif command == CONFIGURED_CONTINUOUS:
logger.debug('Continuous configured')
return True
else:
logger.critical(f'Get CRC error')
else:
# keep reading
logger.info('Response message error')
return False
else:
logger.debug('No response data in serial')
except Exception as e:
logger.critical(e)
else:
logger.critical('Serial is not opened')
def get_device_data(ser, logger, message_no: int):
send_message(b'', GET_DEVICE_DATA, ser, logger, message_no)
def run_continuous(module: int, ser, logger, message_no: int):
message = struct.pack('!B', module)
send_message(message, RUN_CONTINUOUS, ser, logger, message_no)
if logger:
logger.debug(f'Run continuous mode on module code: {module}')
def stop_continuous(ser, logger, message_no: int):
send_message(b'', STOP_CONTINUOUS, ser, logger, message_no)
if logger:
logger.debug('Stop continuous mode')
def stop_pulse_train(ser, logger, message_no: int):
send_message(b'', STOP_PULSE_TRAIN, ser, logger, message_no)
if logger:
logger.debug('Stop pulse train')
def load_pulse_train(pulse_train: list, ser, logger, message_no: int, trigger=False, run_once=False):
message = b''
for led, duration, module in pulse_train:
message += struct.pack('!BHB', led, duration, module)
if trigger and not run_once:
send_message(message, LOAD_PULSE_TRAIN_TEST, ser, logger, message_no)
elif trigger and run_once:
send_message(message, LOAD_PULSE_TRAIN_TEST_ONCE, ser, logger, message_no)
else:
send_message(message, LOAD_PULSE_TRAIN, ser, logger, message_no)
def configure_continuous(duration: int, fps: int, ser, logger, message_no: int):
period = 1000 // fps
if duration > period * 1000: # duration单位微秒, period单位毫秒
if logger:
logger.warnning(f'Duration should be smaller than period, '
f'duration: {duration} us, period: {period} ms')
message = struct.pack('!HH', duration, period)
send_message(message, CONFIGURE_CONTINUOUS, ser, logger, message_no)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。