Skip to content

Commit 75627e7

Browse files
committed
add m5310a.py
1 parent 29f428a commit 75627e7

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

07.sensors/M5310A/M5310A.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from machine import UART
2+
import utime
3+
from machine import Pin
4+
5+
class M5310_A():
6+
def send_cmd(self,cmd):
7+
self.uart.write(cmd + '\r\n')
8+
def cheak_ack(self,buf, ack):
9+
if ack in buf:
10+
return True
11+
else:
12+
return False
13+
def __init__(self):
14+
self.rst = Pin(18, Pin.OUT)
15+
self.uart = UART(2)
16+
self.uart.init(9600, bits=8, parity=None, stop=1)
17+
self.RecvMax = 128
18+
self.RecvBuf = bytearray()
19+
self.cmd = ['AT', 'AT+CIMI', 'AT+COPS=1,2,\"46000\"', 'AT+CSQ', 'AT+CEREG?', 'AT+CGATT?',
20+
'AT+NSOCR="DGRAM",17,0,1', 'AT+NSOCFG=0', 'AT+NSOCFG=0,0,0']
21+
self.cmd_len = len(self.cmd)
22+
self.cmd_index = 0
23+
self.set_up()
24+
def set_up(self):
25+
self.rst.value(1)
26+
utime.sleep_ms(200)
27+
self.rst.value(0)
28+
self.send_cmd(self.cmd[self.cmd_index])
29+
send_time = utime.ticks_ms()
30+
while True:
31+
recvlen = self.uart.any()
32+
if recvlen > 0:
33+
buffer = self.uart.read(recvlen)
34+
for data in buffer:
35+
self.RecvBuf.append(data)
36+
if self.cmd[self.cmd_index] == 'AT' or self.cmd[self.cmd_index] == 'AT+CIMI' \
37+
or self.cmd[self.cmd_index] == 'AT+COPS=1,2,\"46000\"' or self.cmd[self.cmd_index] ==\
38+
'AT+NSOCFG=0' or self.cmd[self.cmd_index] == 'AT+NSOCFG=0,0,0':
39+
if self.cheak_ack(self.RecvBuf, b'\r\nOK\r\n'):
40+
print('CHEAK_OK')
41+
print(self.RecvBuf)
42+
self.RecvBuf = bytearray()
43+
self.cmd_index += 1
44+
if self.cmd_index >= self.cmd_len:
45+
break
46+
else:
47+
self.send_cmd(self.cmd[self.cmd_index])
48+
send_time = utime.ticks_ms()
49+
elif self.cmd[self.cmd_index] == 'AT+CSQ':
50+
if self.cheak_ack(self.RecvBuf, b'\r\n+CSQ:'):
51+
print('CHEAK_OK')
52+
print(self.RecvBuf)
53+
strbuf = bytes(self.RecvBuf)
54+
print(strbuf)
55+
num = strbuf.find(b':')
56+
csq = int(strbuf[num + 1:num + 3])
57+
if csq > 12 and csq < 99:
58+
self.RecvBuf = bytearray()
59+
self.cmd_index += 1
60+
if self.cmd_index >= self.cmd_len:
61+
break
62+
else:
63+
self.send_cmd(self.cmd[self.cmd_index])
64+
send_time = utime.ticks_ms()
65+
elif self.cmd[self.cmd_index] == 'AT+CGATT?':
66+
if self.cheak_ack(self.RecvBuf, b'+CGATT:1'):
67+
print('CHEAK_OK')
68+
print(self.RecvBuf)
69+
self.RecvBuf = bytearray()
70+
self.cmd_index += 1
71+
if self.cmd_index >= self.cmd_len:
72+
break
73+
else:
74+
self.send_cmd(self.cmd[self.cmd_index])
75+
send_time = utime.ticks_ms()
76+
elif self.cmd[self.cmd_index] == 'AT+CEREG?':
77+
if self.cheak_ack(self.RecvBuf, b'+CEREG:0'):
78+
print('CHEAK_OK')
79+
print(self.RecvBuf)
80+
81+
strbuf = bytes(self.RecvBuf)
82+
print(strbuf)
83+
num = strbuf.find(b'G:')
84+
reg = int(strbuf[num + 4:num + 5])
85+
if reg == 1 or reg == 5:
86+
self.RecvBuf = bytearray()
87+
self.cmd_index += 1
88+
if self.cmd_index >= self.cmd_len:
89+
break
90+
else:
91+
self.send_cmd(self.cmd[self.cmd_index])
92+
send_time = utime.ticks_ms()
93+
elif self.cmd[self.cmd_index] == 'AT+NSOCR="DGRAM",17,0,1':
94+
if self.cheak_ack(self.RecvBuf, b'\r\nOK\r\n'):
95+
print('CHEAK_OK')
96+
print(self.RecvBuf)
97+
strbuf = bytes(self.RecvBuf)
98+
print(strbuf)
99+
num = strbuf.find(b'\r\nOK\r\n')
100+
reg = int(strbuf[num - 3]) - 48
101+
print(reg)
102+
if reg >= 0 and reg <= 6:
103+
self.RecvBuf = bytearray()
104+
self.cmd_index += 1
105+
if self.cmd_index >= self.cmd_len:
106+
break
107+
else:
108+
self.send_cmd(self.cmd[self.cmd_index])
109+
send_time = utime.ticks_ms()
110+
if (len(self.RecvBuf) > self.RecvMax):
111+
self.RecvBuf = bytearray()
112+
print('OUT LINE')
113+
self.send_cmd(self.cmd[self.cmd_index])
114+
print('resend:', self.cmd[self.cmd_index])
115+
send_time = utime.ticks_ms()
116+
now_time = utime.ticks_ms()
117+
if send_time + 500 < now_time:
118+
self.RecvBuf = bytearray()
119+
self.send_cmd(self.cmd[self.cmd_index])
120+
print('resend:', self.cmd[self.cmd_index])
121+
send_time = utime.ticks_ms()
122+
def send_data(self,data):
123+
data='AT+NSOST=0,zwidas.top,8888,,\"%s\"'%data
124+
self.send_cmd(data)
125+
126+
def unit_test():
127+
ts=M5310_A()
128+
ts.send_data('hello')
129+
while True:
130+
recvlen = ts.uart.any()
131+
if recvlen > 0:
132+
buffer = ts.uart.read(recvlen)
133+
for data in buffer:
134+
ts.RecvBuf.append(data)
135+
print(ts.RecvBuf)
136+
if ts.cheak_ack(ts.RecvBuf, b'+NSONMI'):
137+
ts.RecvBuf=bytearray()
138+
ts.send_cmd('AT+NSORF=0,100')
139+
140+
if __name__=='__main__':
141+
unit_test()

0 commit comments

Comments
 (0)