-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDigCtrl.py
72 lines (62 loc) · 1.86 KB
/
DigCtrl.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
'''
from time import sleep
try:
import serial
#from pyfirmata import Arduino, util
except:
import pip
pip.main(['install', 'serial'])
import serial
from msvcrt import getch
'''
import serial
##==============================================================================
ser = None
##==============================================================================
##======Write Serial Command to arduino============
def SerialWrite(command):
ser.write(command)
ser.flushInput()
##====================================
##======Use this============
def digCtrl(command):
if command == 1:
cmd = 'a'.encode('utf-8')
SerialWrite(cmd)
elif command == 2:
cmd = 'b'.encode('utf-8')
SerialWrite(cmd)
cmd = 'c'.encode('utf-8')
SerialWrite(cmd)
##====================================
##======= Main ================
def arduinoInit():
ser = serial.Serial("/dev/ttyACM0", 38400, timeout=2) # Establish the connection on a specific port
print("Connecting to Arduino.....")
for i in range (1,10):
rv=ser.readline()
print("Loading...")
#Debug print (rv) # Read the newest output from the Arduino
print (rv.decode("utf-8"))
ser.flushInput()
#sleep(1) # Delay for one tenth of a secon
Str=rv.decode("utf-8")
print(Str[0:5])
if Str[0:5]=="Ready":
print("Get Arduino Ready !")
break
print("==================================")
##------------------------------------------------------
'''
##counter = 65 # "A"
##ser.write(chr(counter).encode('utf-8')) # Convert the decimal number to ASCII then send it to the Arduino
cmd="Key in the Command".encode("utf-8")
SerialWrite(cmd)
DigCtrl(1)
print(1)
#sleep(10)
DigCtrl(2)
print(2)
#sleep(15)
ser.close()
'''