Package: jrt-laser-distance-sensor
JRT Laser Distance Sensor Python LibraryThis library is a Python library for the JRT laser distance sensor for laser devices from Chengdu JRT Meter Technology Company, Ltd.This library has been tested with the following sensors:U81B (https://www.jrt-measure.com/industrial-laser-distance-sensor/55121160.html)The author of this library has no affiliation with Chengdu JRT and this library is not endorsed by Chengdu JRT.InstallationUse the package managerpipto install jrt-laser-distance-sensor.pipinstalljrt_laser_distance_sensorAfter installation, you should be able to test the library with the following command:jrt-laser-distance-sensor<serial-port-name>Serial port naming conventions vary by platform (e.g. /dev/ttyUSB0 on Linux, COM1 on Windows, /dev/tty.usb-12345 on OS X).All measurements from the library are reported in millimeters.UsageA simple example of using the library is as follows, with the sensor connected to /dev/ttyUSB0:fromjrt_laser_distance_sensorimportJrtSerialsensor=JrtSerial('/dev/ttyUSB0')print(sensor.one_shot_measurement())You can also do continuous measurements:fromjrt_laser_distance_sensorimportJrtSerialsensor=JrtSerial('/dev/ttyUSB0')sensor.start_continuous_measurement()whileTrue:print(sensor.read_measurement())This is block of code demonstrates most of the useful features of the library:importsysimporttimefrombinasciiimporthexlifyfromjrt_laser_distance_sensorimportJrtSerialdefmain():iflen(sys.argv)>1:port=sys.argv[1]else:print(f'usage:{sys.argv[0]}<port>')sys.exit(1)jrt=JrtSerial(port)hw_version=hexlify(jrt.read_hw_version().to_bytes(2,'big',signed=True))print(f'hw version:{hw_version}')sw_version=hexlify(jrt.read_sw_version().to_bytes(2,'big',signed=True))print(f'sw version:{sw_version}')input_voltage=hexlify(jrt.read_input_voltage().to_bytes(2,'big',signed=True))print(f'input voltage:{input_voltage}')print(f"reading status:{jrt.status_to_text(jrt.read_status())}")print("testing laser on / off")print('laser',jrt.set_laser(True))print('laser should be on (sleeping 2 seconds)')time.sleep(2)print('laser',jrt.set_laser(False))print('laser should be off (sleeping 2 seconds)')time.sleep(2)print('testing continuous measurement in auto mode (10 readings)')jrt.start_continuous_measurement('auto')x=0whilex<10:print(f"{x+1}:{jrt.read_measurement()}(signal quality:{jrt.last_signal_quality})")time.sleep(0.1)x+=1jrt.stop_continuous_measurement()print('continuous measurement test finished')time.sleep(0.5)print('test one shot measurement')print('auto',jrt.one_shot_measurement('auto'))print('slow',jrt.one_shot_measurement('slow'))print('fast',jrt.one_shot_measurement('fast'))print('turning laser off')print('laser',jrt.set_laser(False))It should produce output similar to the following:hw version: b'8301'
sw version: b'1b08'
input voltage: b'3303'
reading status: No error
testing laser on / off
laser 1
laser should be on (sleeping 2 seconds)
laser 0
laser should be off (sleeping 2 seconds)
testing continuous measurement in auto mode (10 readings)
1: 271 (signal quality: 98)
2: 271 (signal quality: 97)
3: 270 (signal quality: 108)
4: 270 (signal quality: 110)
5: 270 (signal quality: 83)
6: 270 (signal quality: 81)
7: 270 (signal quality: 87)
8: 271 (signal quality: 83)
9: 270 (signal quality: 87)
10: 270 (signal quality: 103)
continuous measurement test finished
test one shot measurement
auto 271
slow 271
fast 270
turning laser off
laser 0