-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBS_ARDUINO_SIMULATOR.py
151 lines (91 loc) · 2.98 KB
/
BS_ARDUINO_SIMULATOR.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import serial,re,time
port = "COM22"
baud = 9600
import sys
if len(sys.argv)>1:
port = sys.argv[1]
Arduino = serial.Serial(port,baud,timeout=100)
def rcvImage(ImageFile="rcv.jpg"):
try:
Arduino.write("BS+PULLIMG\r\n")
while True:
rcvCmd = Arduino.readline()
rcvCmd = rcvCmd.strip() #remove CR LF
rcvCmdFormat = "BS\+PUSHIMG=(\d+)"
rcvCmdValidity = re.search(rcvCmdFormat,rcvCmd)
if rcvCmdValidity==None:
print "Invalid command from Satellite."
print "Expected format BS+PUSHIMG=(\d+) , but got:",rcvCmd
#return False
else:
break
length2read = rcvCmdValidity.group(1)
if not length2read.isdigit():
print "Excepted length2read to be a number."
print "length2read received:",length2read
return False
length2read = int(length2read) # convert to interger
print "Waiting for image from SATELLITE. Ready to write to",ImageFile
print "Size (in bytes) of Image:",length2read
buffersize = 2048 # note this must be less than 4096
Arduino.timeout = (buffersize / (baud/10)) * 3
f = open(ImageFile,'wb')
no_of_bytes_written = 0
for i in xrange(int(length2read/buffersize)):
imgData = Arduino.read(buffersize)
f.write(imgData)
no_of_bytes_written += buffersize
print "Received bytes = ",no_of_bytes_written
# for the remainder of bytes
imgData = Arduino.read(length2read%buffersize)
f.write(imgData)
no_of_bytes_written += length2read%buffersize
print "Received bytes = ",no_of_bytes_written
#f.close()
print "Image received successfully."
return True
except Exception as e:
print "Unexpected error:",e
return False
finally:
f.close()
print "Please wait... Returning to data capture mode..."
#time.sleep(7)
#Arduino.reset_input_buffer()
def sendPacket(packet="HELLO WORLD"): # CAUTION : packet cannot contain a line feed LF ('\n')
try:
cmd = "BS+STOREPT=" + str(len(packet)) + "\r\n"
print "Sending command :",cmd
Arduino.write(cmd)
Arduino.write(packet + "\r\n")
print "Packet sent successfully."
except Exception as e:
print "Unexpected error :",e
if __name__ == "__main__":
#sendPacket()
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server = ('localhost',9999)
s.sendto("hello",server)
data,server = s.recvfrom(10)
if data=="ack":
print "Server connected....."
else:
print "Server not found....."
exit()
while True:
try:
SATdata = Arduino.readline()
print SATdata
s.sendto(SATdata,server)
with open("log.txt", 'a') as outfile:
outfile.write(SATdata)
except KeyboardInterrupt:
import datetime
rcvFname = datetime.datetime.now().strftime("%b_%d_%Y_%H-%M-%S.jpg")
rcvImage(rcvFname)
import subprocess
subprocess.check_output("cp " + rcvFname + " rcv.jpg -f",shell=1)
time.sleep(7)
#Arduino.reset_input_buffer()
Arduino.flushInput()