forked from fclef978/MG4Py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.py
86 lines (71 loc) · 2.28 KB
/
arduino.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
# Arduino通信クラス
# TL 鈴木宏和
# 2017-08-26
# 2017-08-26
import serial
from time import sleep
class ArduinoOpenError(Exception):
def __str__(self):
return ('Arduino Open Failed')
class Arduino:
"""
Arduinoとの通信をやってくれるクラスです。
シリアル通信のクラスであるserial(モジュールはpySerial)はbyte型や
byteArray型でしかやり取りできないので、このクラス内のread()やwrite()で
エンコード・デコードしてます。
"""
def __init__(self):
try:
self.ser = serial.Serial('/dev/ttyACM0', 115200)
self.port = '/dev/ttyACM0'
except:
self.ser = serial.Serial('/dev/ttyACM1', 115200)
self.port = '/dev/ttyACM1'
finally:
pass
self.flush()
print("Arduino Opened {0}".format(self.port))
def __del__(self):
# cmd = [99, 0, 0]
# self.send(cmd)
self.ser.close()
def flush(self):
self.ser.reset_input_buffer()
def send(self, cmd_data):
print("Send {0}".format(cmd_data))
ser_data = self.encode(cmd_data)
for i in range(12):
self.write(ser_data[i])
def receive(self):
cnt = 0
ser_data = []
# 50ループ(だいたい50ミリ秒)でタイムアウト
for i in range(50):
if self.ser.in_waiting >= 12:
ser_data.append(self.read())
if ser_data[0] == ':':
for i in range(1, 12):
ser_data.append(self.read())
ser_data = ''.join(ser_data)
cmd_data = self.decode(ser_data)
print("Receive {0}".format(cmd_data))
return cmd_data
sleep(0.001)
return False
def write(self, byte_data):
tmp = byte_data.encode('ASCII')
print(tmp)
self.ser.write(tmp)
return
def read(self):
tmp = self.ser.read().decode('utf-8')
return tmp
@staticmethod
def encode(cmd):
return ':{cmd[0]}{cmd[1]:05d}{cmd[2]:05d}'.format(cmd = cmd)
@staticmethod
def decode(ser):
a = ser[1]
b = ser[2:7]
c = ser[7:12]
return [a, int(b), int(c)]