Created
January 29, 2019 03:16
-
-
Save wujiang/04f878a29d14f5e8a94321b368cb4dad to your computer and use it in GitHub Desktop.
ulb16_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from canlib import canlib, Frame | |
from canlib.canlib import ChannelData | |
import time | |
import binascii | |
def setUpChannel(channel=0, | |
openFlags=canlib.canOPEN_ACCEPT_VIRTUAL, | |
bitrate=canlib.canBITRATE_500K, | |
bitrateFlags=canlib.canDRIVER_NORMAL): | |
ch = canlib.openChannel(channel, openFlags) | |
print("Using channel: %s, EAN: %s" % (ChannelData(channel).device_name, | |
ChannelData(channel).card_upc_no)) | |
ch.setBusOutputControl(bitrateFlags) | |
ch.setBusParams(bitrate) | |
ch.busOn() | |
return ch | |
def tearDownChannel(ch): | |
ch.busOff() | |
ch.close() | |
print("canlib version:", canlib.dllversion()) | |
template = """ | |
---------- | |
{} {} {} {} {} | |
{} {} | |
{} {} | |
{} {} | |
{} {} {} {} {} | |
$$$$$$$ | |
""" | |
ch0 = setUpChannel(channel=0) | |
import binascii | |
import functools | |
import math | |
def hexint(b): | |
if not b: | |
return 0 | |
return b[1] * 256 + b[0] | |
def frame_data(b): | |
if not b: | |
return 0 | |
cal = lambda x, y: y * 256 + x | |
if len(b) == 8: | |
return "{}-{}, {}, {}, {}".format(b[0], b[1], cal(b[2], b[3]), | |
cal(b[4], b[5]), cal(b[6], b[7])) | |
else: | |
return b | |
frames = [ | |
[0x1832, []], # restore factory | |
[0x2d32, [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00]], # stop auto measure | |
[0x051e32, [0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x061e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x071e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x081e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x091e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x0a1e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x0b1e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x0c1e32, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x1732, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]], | |
[0x1932, []], # power cycle | |
[0x2d32, [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00]], # stop auto measure | |
] | |
for id_, data in frames: | |
frame = Frame(id_=id_, data=data, flags=canlib.canMSG_EXT) | |
ch0.writeWait(frame, 10) | |
get_frame = ch0.read(timeout=2000) | |
print(get_frame) | |
distances = [0] * 16 | |
indexes = [] | |
while True: | |
try: | |
f = Frame(id_=0x2932, data=[0x01]) | |
ch0.writeWait(f, 10) | |
while True: | |
frame = ch0.read(timeout=2000) | |
if frame.id == 0x2932: | |
indexes = [] | |
[ | |
indexes.append(i) for i in range(8) | |
if (frame.data[0] >> i) & 0x01 | |
] | |
[ | |
indexes.append(i + 8) for i in range(8) | |
if (frame.data[1] >> i) & 0x01 | |
] | |
for i, idx in enumerate(indexes[:3]): | |
distances[idx] = ( | |
frame.data[i * 2 + 3] << 8) + frame.data[i * 2 + 2] | |
elif frame.id == 0x012932: | |
if not indexes: | |
continue | |
else: | |
for i, idx in enumerate(indexes[3:]): | |
distances[idx] = ( | |
frame.data[i * 2 + 1] << 8) + frame.data[i * 2] | |
if idx not in (8, 9, 10, 11): | |
continue | |
elif frame.id == 0xff2932: | |
indexes = [] | |
break | |
print(template.format( | |
distances[12], distances[11], distances[10], distances[9], | |
distances[8], distances[13], distances[7], distances[14], | |
distances[6], distances[15], distances[5], distances[0], | |
distances[1], distances[2], distances[3], distances[4])) | |
except (canlib.canNoMsg) as ex: | |
print(ex) | |
except (canlib.canError) as ex: | |
print(ex) | |
tearDownChannel(ch0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment