22import utime
33from machine import Pin
44
5+
56class 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
137 def __init__ (self ):
148 self .rst = Pin (18 , Pin .OUT )
159 self .uart = UART (2 )
1610 self .uart .init (9600 , bits = 8 , parity = None , stop = 1 )
11+ self .cmd = ['idle' , 'AT' , 'AT+CIMI' , 'AT+COPS=1,2,\" 46000\" ' , 'AT+CSQ' , 'AT+CEREG?' , 'AT+CGATT?' ,
12+ 'AT+NSOCR="DGRAM",17,0,1' , 'AT+NSOCFG=0' , 'AT+NSOCFG=0,0,0' , 'end' ]
1713 self .RecvMax = 128
1814 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' ]
2115 self .cmd_len = len (self .cmd )
2216 self .cmd_index = 0
17+ self .retrans_times = 0
18+ self .send_time = 0
2319 self .set_up ()
24- def set_up (self ):
20+
21+ def send_cmd (self , cmd ):
22+ self .uart .write (cmd + '\r \n ' )
23+
24+ def cheak_ack (self , buf , ack ):
25+ if ack in buf :
26+ return True
27+ else :
28+ return False
29+
30+ def reset (self ):
2531 self .rst .value (1 )
2632 utime .sleep_ms (200 )
2733 self .rst .value (0 )
34+
35+ def is_recv (self ):
36+ recvlen = self .uart .any ()
37+ if recvlen > 0 :
38+ buffer = self .uart .read (recvlen )
39+ for data in buffer :
40+ self .RecvBuf .append (data )
41+ return True
42+ else :
43+ return False
44+
45+ def transform (self ):
46+ self .RecvBuf = bytearray ()
47+ self .retrans_times = 0
48+ self .cmd_index += 1
2849 self .send_cmd (self .cmd [self .cmd_index ])
29- send_time = utime .ticks_ms ()
50+ self .send_time = utime .ticks_ms ()
51+
52+ def is_out (self ):
53+ if (len (self .RecvBuf ) > self .RecvMax ):
54+ self .RecvBuf = bytearray ()
55+ print ('out line' )
56+ self .send_cmd (self .cmd [self .cmd_index ])
57+ self .retrans_times += 1
58+ print ('resend:' , self .cmd [self .cmd_index ])
59+ self .send_time = utime .ticks_ms ()
60+
61+ def set_up (self ):
3062 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' :
63+ if self .cmd [ self . cmd_index ] == 'idle' :
64+ print ( 'reset' )
65+ self .reset ( )
66+ self . transform ()
67+ elif self . cmd [ self . cmd_index ] == 'AT' or self .cmd [ self . cmd_index ] == 'AT+CIMI' \
68+ or self .cmd [self .cmd_index ] == 'AT+COPS=1,2, \" 46000 \" ' or self .cmd [self .cmd_index ] == \
69+ 'AT+NSOCFG=0' or self .cmd [self .cmd_index ] == 'AT+NSOCFG=0,0,0' :
70+ if self .is_recv () :
3971 if self .cheak_ack (self .RecvBuf , b'\r \n OK\r \n ' ):
40- print ('CHEAK_OK ' )
72+ print ('cheak ok ' )
4173 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' :
74+ self .transform ()
75+ else :
76+ self .is_out ()
77+
78+ elif self .cmd [self .cmd_index ] == 'AT+CSQ' :
79+ if self .is_recv ():
5080 if self .cheak_ack (self .RecvBuf , b'\r \n +CSQ:' ):
5181 print ('CHEAK_OK' )
5282 print (self .RecvBuf )
@@ -55,42 +85,32 @@ def set_up(self):
5585 num = strbuf .find (b':' )
5686 csq = int (strbuf [num + 1 :num + 3 ])
5787 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?' :
88+ self .transform ()
89+ else :
90+ self .out_line ()
91+ elif self .cmd [self .cmd_index ] == 'AT+CEREG?' :
92+ if self .is_recv ():
7793 if self .cheak_ack (self .RecvBuf , b'+CEREG:0' ):
7894 print ('CHEAK_OK' )
7995 print (self .RecvBuf )
80-
8196 strbuf = bytes (self .RecvBuf )
8297 print (strbuf )
8398 num = strbuf .find (b'G:' )
8499 reg = int (strbuf [num + 4 :num + 5 ])
85100 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' :
101+ self .transform ()
102+ else :
103+ self .out_line ()
104+ elif self .cmd [self .cmd_index ] == 'AT+CGATT?' :
105+ if self .is_recv ():
106+ if self .cheak_ack (self .RecvBuf , b'+CGATT:1' ):
107+ print ('cheak ok' )
108+ print (self .RecvBuf )
109+ self .transform ()
110+ else :
111+ self .out_line ()
112+ elif self .cmd [self .cmd_index ] == 'AT+NSOCR="DGRAM",17,0,1' :
113+ if self .is_recv ():
94114 if self .cheak_ack (self .RecvBuf , b'\r \n OK\r \n ' ):
95115 print ('CHEAK_OK' )
96116 print (self .RecvBuf )
@@ -100,31 +120,34 @@ def set_up(self):
100120 reg = int (strbuf [num - 3 ]) - 48
101121 print (reg )
102122 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 ()
123+ self .transform ()
124+ else :
125+ self .out_line ()
126+ elif self .cmd [self .cmd_index ] == 'end' :
127+ print ('end' )
128+ break
116129 now_time = utime .ticks_ms ()
117- if send_time + 500 < now_time :
130+ # print('now',now_time)
131+ # print('last',self.send_time)
132+ if self .send_time + 500 < now_time :
118133 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 )
134+ self .retrans_times += 1
135+ print ('retrans_times' ,self .retrans_times )
136+ if self .retrans_times >= 40 :
137+ self .retrans_times = 0
138+ self .cmd_index = 0
139+ else :
140+ self .send_cmd (self .cmd [self .cmd_index ])
141+ print ('resend:' , self .cmd [self .cmd_index ])
142+ self .send_time = utime .ticks_ms ()
143+
144+ def send_data (self , data ):
145+ data = 'AT+NSOST=0,zwidas.top,8888,,\" %s\" ' % data
146+ self .send_cmd (data )
147+
125148
126149def unit_test ():
127- ts = M5310_A ()
150+ ts = M5310_A ()
128151 ts .send_data ('hello' )
129152 while True :
130153 recvlen = ts .uart .any ()
@@ -134,8 +157,9 @@ def unit_test():
134157 ts .RecvBuf .append (data )
135158 print (ts .RecvBuf )
136159 if ts .cheak_ack (ts .RecvBuf , b'+NSONMI' ):
137- ts .RecvBuf = bytearray ()
160+ ts .RecvBuf = bytearray ()
138161 ts .send_cmd ('AT+NSORF=0,100' )
139162
140- if __name__ == '__main__' :
141- unit_test ()
163+
164+ if __name__ == '__main__' :
165+ unit_test ()
0 commit comments