-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsendreceivesigfox.py
executable file
·123 lines (103 loc) · 2.8 KB
/
sendreceivesigfox.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
#!/usr/bin/python
## @package rpisigfox
# This script allow the control of the rpisigfox expansion board for Raspberry Pi.
#
# V1.0 allow only to send regular message on the SigFox Network.
# syntax is :
# sendsigfox MESSAGE
# where MESSAGE is an HEXA string encoded. Can be 2 to 24 characters representing 1 to 12 bytes.
# Example : sendsigfox 00AA55BF to send the 4 bytes 0x00 0xAA 0x55 0xBF
#
import time
import serial
import sys
import re
from time import sleep
SOH = chr(0x01)
STX = chr(0x02)
EOT = chr(0x04)
ACK = chr(0x06)
NAK = chr(0x15)
CAN = chr(0x18)
CRC = chr(0x43)
def getc(size, timeout=1):
return ser.read(size)
def putc(data, timeout=1):
ser.write(data)
sleep(0.001) # give device time to prepare new buffer and start sending it
def WaitFor(ser, success, failure, timeOut):
return ReceiveUntil(ser, success, failure, timeOut) != ''
def ReceiveUntil(ser, success, failure, timeOut):
iterCount = timeOut / 0.1
ser.timeout = 0.1
currentMsg = ''
while iterCount >= 0 and success not in currentMsg and failure not in currentMsg :
sleep(0.1)
while ser.inWaiting() > 0 :
c = ser.read()
currentMsg += c
iterCount -= 1
if success in currentMsg :
return currentMsg
elif failure in currentMsg :
print 'Failure (' + currentMsg.replace('\r\n', '') + ')'
else :
print 'Receive timeout (' + currentMsg.replace('\r\n', '') + ')'
return ''
print 'Sending SigFox Message...'
# allow serial port choice from parameter - default is /dev/ttyAMA0
portName = '/dev/ttyAMA0'
if len(sys.argv) == 3:
portName = sys.argv[2]
print 'Serial port : ' + portName
ser = serial.Serial(
port=portName,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
if ser.isOpen() : # on some platforms the serial port needs to be closed first
ser.close()
try:
ser.open()
except serial.SerialException as e:
sys.stderr.write("Could not open serial port {}: {}\n".format(ser.name, e))
sys.exit(1)
ser.write('AT\r')
if WaitFor(ser, 'OK', 'ERROR', 3) :
print('SigFox Modem OK')
else:
print('SigFox Modem Init Error')
ser.close()
exit()
ser.write('ATE0\r')
if WaitFor(ser, 'OK', 'ERROR', 3) :
print('SigFox Modem echo OFF')
else:
print('SigFox Modem Configuration Error')
ser.close()
exit()
ser.write("AT$SF={0},2,1\r".format(sys.argv[1]))
print('Sending ...')
if WaitFor(ser, 'OK', 'ERROR', 25) :
print('Message sent')
else:
print('Error sending message')
ser.close()
exit()
if WaitFor(ser, 'BEGIN', 'ERROR', 25) :
print('Waiting for answer')
else:
print('Error waiting for answer')
ser.close()
exit()
rxData = ReceiveUntil(ser, 'END', 'ERROR', 25)
if rxData != '' :
print('Answer received')
else:
print('Error receiving answer')
ser.close()
exit()
print re.sub(r'\+RX=([0-9af ]{2,})\+RX END', r'\1', rxData.replace('\r\n', ''))
ser.close()