-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsv3_usb.py
121 lines (93 loc) · 2.97 KB
/
gsv3_usb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import serial
import struct
import platform
class MeasurementConverter:
def convertValue(self, bytes):
pass
class ForceMeasurementConverterKG(MeasurementConverter):
def __init__(self, F_n, S_n, u_e):
self.F_n = F_n
self.S_n = S_n
self.u_e = u_e
def convertValue(self, value):
A = struct.unpack('>H', value)[0]
# return (A - 0x8000) * (self.F_n / self.S_n) * (self.u_e / 0x8000)
return self.F_n / self.S_n * ((A - 0x8000) / 0x8000) * self.u_e * 2
class GSV3USB:
def __init__(self, com_port, baudrate=38400):
com_path = f'/dev/ttyUSB{com_port}' if platform.system(
) == 'Linux' else f'COM{com_port}'
print(f'Using COM: {com_path}')
self.sensor = serial.Serial(com_path, baudrate)
self.converter = ForceMeasurementConverterKG(500, 2, 1.05)
def get_all(self, profile=0):
self.sensor.write(struct.pack('bb', 9, profile))
def save_all(self, profile=2):
self.sensor.write(struct.pack('bb', 10, profile))
def start_transmission(self):
self.sensor.write(b'\x23')
def stop_transmission(self):
self.sensor.write(b'\x24')
def set_zero(self):
self.sensor.write(b'\x0C')
def set_offset(self):
self.sensor.write(b'\x0E')
def set_bipolar(self):
self.sensor.write(b'\x14')
def set_unipolar(self):
self.sensor.write(b'\x15')
def get_serial_nr(self):
self.stop_transmission()
self.sensor.write(b'\x1F')
ret = self.sensor.read(8)
self.start_transmission()
return ret
def set_mode(self, text=False, max=False, log=False, window=False):
x = 0
if(text):
x = x | 0b00010
if(max):
x = x | 0b00100
if(log):
x = x | 0b01000
if(window):
x = x | 0b10000
self.sensor.write(struct.pack('bb', 0x26, x))
def get_mode(self):
self.stop_transmission()
self.sensor.write(b'\x27')
ret = self.sensor.read(1)
self.start_transmission()
return ret
def get_firmware_version(self):
self.stop_transmission()
self.sensor.write(b'\x27')
ret = self.sensor.read(2)
self.start_transmission()
return ret
def get_special_mode(self):
self.stop_transmission()
self.sensor.write(b'\x89')
ret = self.sensor.read(2)
self.start_transmission()
return ret
def set_special_mode(self):
pass
def read_value(self):
self.sensor.read_until(b'\xA5')
read_val = self.sensor.read(2)
return self.converter.convertValue(read_val)
def clear_maximum(self):
self.sensor.write(b'\x3C')
def clear_buffer(self):
self.sensor.write(b'\x25')
def main():
dev = GSV3USB(10)
try:
while True:
print(dev.read_value())
except KeyboardInterrupt:
print("Exiting")
return
if __name__ == "__main__":
main()