-
Notifications
You must be signed in to change notification settings - Fork 0
/
HID2.py
34 lines (28 loc) · 1.03 KB
/
HID2.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
#coding:utf-8
import serial
from Smooth import SpeedMeter
import time
# 速度計を管理する
class HID2:
def __init__(self, device):
# timeoutを設定することで通信エラーを防止する
try:
self.ser = serial.Serial(device, timeout=0.1, inter_byte_timeout=0.1, baudrate=19200)
except serial.serialutil.SerialException:
print('正常にシリアルポートを開けませんでした。')
raise
def setMeter(self, speed):
speed_out = SpeedMeter.getValue(speed)
self._sendMeter(speed_out)
def _sendMeter(self, speed_out):
self._send(f's{speed_out}EOF\n'.encode('ascii'))
def _send(self, text):
try:
self.ser.write(text)
except serial.serialutil.SerialException:
print('シリアル書き込みエラー')
if __name__ == '__main__':
hid2 = HID2('/dev/de15_meter')
while True:
speed_out = int(input('speed: '))
hid2._send(f's{speed_out}EOF\n'.encode('ascii'))