-
Notifications
You must be signed in to change notification settings - Fork 0
/
BS_UDOO_SERVER.py
124 lines (80 loc) · 2.64 KB
/
BS_UDOO_SERVER.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
122
123
124
import serial,time,re
import platform
if platform.system()=="Windows":
UdooPortName = "COM10"
elif platform.system()=="Linux":
UdooPortName = "/dev/ttymxc3"
else:
print "Unsupported platform"
baud = 9600
PacketStore = "PS.txt"
ImageFile = "abc.jpg"
Udoo = serial.Serial()
Udoo.port = UdooPortName
Udoo.baudrate = baud
Udoo.timeout=5
Udoo.open()
print "Udoo operating on",Udoo.name
# commands must be of the format BS+xxxxxxx with 7 'x'es as characters (can't be numbers)
COMMANDS = ["BS+PULLIMG",
"BS+STOREPT"]
def PULLIMG():
try:
import GP
imgName = GP.Capture()
imgName = imgName.strip()
f = open(imgName,'rb')
imgData = f.read()
cmd = "BS+PUSHIMG=" + str(f.tell()) + "\r\n" #f.tell() gives the current position of file pointer and as have
# read all the bytes using .read() it give the size of the file.
print "Sending CMD:",cmd
f.close()
time.sleep(0.5) #wait for a little time to let the basestation ready to receive the command BS+PUSH
Udoo.write(cmd)
print "Image Load Complete....Pushing Image in 1s."
time.sleep(1) # wait for a little time to let the basestation be ready to receive the Image
Udoo.write(imgData)
print "IMAGE sent."
except Exception as e:
print "Unexpected error:",e
def STOREPT(param):
try:
print "Waiting for PACKET....."
f = open(PacketStore,'a')
estimated_timeout = (param / (baud/10)) * 2 # in 1s we receive 960 bytes at 9600 baud , so we set a timeout double the time required as a safety
Udoo.timeout = estimated_timeout
packet = Udoo.readline()
f.write(packet)
f.close()
print "PACKET received successfully."
except Exception as e:
print "Unexpected error:",e
def serve():
while True:
try:
time.sleep(0.5)
Udoo.timeout = None
Udoo.flushInput()
print "Waiting for REMOTE Command...."
line = Udoo.readline()
line = line.strip() #remove \r\n
print "Rcvd REMOTE command >",line
#line = "BS+STOREPT=99"
#line = "BS+PASSIMG"
cmd_format = "BS\+(\w+)=?(\d*)"
cmdSearch = re.search(cmd_format,line)
if cmdSearch == None:
print "Command Format Error. Rcvd Cmd >",line
continue
fun = cmdSearch.group(1) # extract ABCDEFG from BS+ABCDEF=100
param = cmdSearch.group(2) # extract 100 from BS+ABCDEFG=100
print "function:",fun,"parameter:",param
if "BS+"+fun in COMMANDS:
print fun+"("+param+")"
eval(fun+"("+param+")")
except Exception as e:
print "Unexpected error:",e
print "Closing Udoo Serial port..."
Udoo.close()
if __name__ == "__main__":
serve()