code:
1 import serial,time,itertools 2 3 try: 4 ser=serial.Serial(2,115200,timeout=0) 5 except: 6 print 'Open COM2 Failed!!' 7 8 9 #transno=(1,100,500,1380,1381) 10 #transno=(1,20) 11 indexsets=[[0],[1],[2],[3],[0,1],[0,2],[0,3],[1,2],[1,3],[2,3],[0,1,2],[0,1,3],[0,2,3],[1,2,3],[0,1,2,3]] 12 indexsets=[[0,1,3],[0,1,2]] 13 #indexsets_ts=[] 14 15 def gene(): 16 for n in range(1,5): 17 indexsets_ts+=list(itertools.combinations([0,1,2,3],n)) #forget what is itertool now 18 return indexsets_ts 19 20 #sig functions 21 def sigsnd(): 22 for actmod in ('diract','apnact'): 23 act(actmod) 24 for connmod in ('TCP','UDP'): 25 conn(connmod) 26 connt=0 27 for dismod in ('dirsht','closht'): 28 for sndmod in ('uctl','fixl'): 29 snd(sndmod) 30 dis(dismod) 31 if connt<1: 32 conn(connmod) 33 connt+=1 34 35 36 def init(): 37 swrt(chr(0x27),1) 38 swrt('\r\n',1) 39 ser.write('+++') 40 swrt('\r\n',1) 41 def shutConn(): 42 swrt('at+cipshut',4) 43 def cloShut(): 44 swrt('at+cipclose',4) 45 swrt('at+cipshut',4) 46 def cipmod(): 47 swrt('at+cipmode=0',2) 48 def olmod(): 49 swrt('at+cipmode=1',2) 50 def muxmod(): 51 swrt('at+cipmux=1',2) 52 def sigmod(): 53 swrt('at+cipmux=0',2) 54 def srd(): 55 srd=ser.readlines() 56 for line in srd: 57 print line 58 fid=open('tcp_snd.log','a') 59 fid.write(line) 60 fid.close() 61 return srd 62 def swrt(atcmd_str,wt): 63 #fid=open('tcp_snd.log','a') 64 #fid.write(atcmd_str) 65 #fid.close() 66 ser.write(atcmd_str+'\r\n') 67 time.sleep(wt) 68 srd() 69 def wrtmsg(transtr,wstm): 70 ser.write(transtr+chr(0x1a)) 71 time.sleep(wstm) 72 srd() 73 def act(actmod): 74 if actmod=='apnact': 75 swrt('at+cstt="CMNET","",""',2) 76 swrt('at+ciicr',4) 77 if actmod=='diract': 78 pass 79 def conn(connmod): 80 if connmod=='TCP': 81 swrt('at+cipstart="TCP","124.205.119.38",1562',6) 82 if connmod=='UDP': 83 swrt('at+cipstart="UDP","124.205.119.38",1562',6) 84 def snd(sndmod): 85 if sndmod=='uctl': 86 for tran in transno: 87 for n in range(2): 88 swrt('at+cipsend',1) 89 wrtmsg(tran*'v',8) 90 if sndmod=='fixl': 91 for tran in transno: 92 for n in range(2): 93 swrt('at+cipsend='+str(tran),1) 94 wrtmsg(tran*'v',8) 95 def dis(dismod): 96 if dismod=='dirsht': 97 shutConn() 98 if dismod=='closht': 99 cloShut() 100 101 102 #online mode function 103 def modTs(): 104 for actmod in ('diract','apnact'): 105 act(actmod) 106 for connmod in ('TCP','UDP'): 107 print connmod 108 conn(connmod) 109 connt=0 110 for dismod in ('dirsht','closht'): 111 print dismod 112 olsnd() 113 dis(dismod) 114 print dismod 115 if connt<1: 116 conn(connmod) 117 connt+=1 118 119 def olsnd(): 120 for rtm in range(2): 121 for tran in transno: 122 swrt(tran*'v',1) 123 nmipmd() 124 ato() 125 nmipmd() 126 def ato(): 127 swrt('ato',2) 128 def nmipmd(): 129 ser.write('+++') 130 time.sleep(3) 131 srd() 132 133 #mux-mode functions 134 135 indset=[0,1,3] 136 def muxTS(indset): 137 for actmod in ('diract','apnact'): 138 act(actmod) 139 for dismod in ('dirsht','closht'): 140 for x in range(2**len(indset)): 141 binstr=bin(int(str(x),10))[2:].rjust(len(indset),'0') 142 typset=[] 143 for nc in binstr: 144 if nc=='0': 145 typset.append('"TCP"') 146 else: 147 typset.append('"UDP"') 148 for ind in range(len(indset)): 149 constr='at+cipstart='+str(indset[ind])+','+typset[ind]+',"124.205.119.38",1562' 150 swrt(constr,7) 151 print 'wrt done' ##connect OK 152 for sndmod in ('uctl','fixl'): 153 for index in indset: 154 print indset,index 155 for tran in transno: 156 for n in range(2): 157 if sndmod=='uctl': 158 swrt('at+cipsend='+str(index),1) 159 wrtmsg(tran*'v',15) 160 else: 161 swrt('at+cipsend='+str(index)+','+str(tran),1) 162 wrtmsg(tran*'v',15) 163 dis(dismod) 164 165 sndmod='' 166 def muxmod(): 167 swrt('at+cipmux=1',2) 168 169 def muxsnd(indset): 170 for index in indset: 171 print indset,index 172 for tran in transno: 173 for n in range(5): 174 if sndmod=='uctl': 175 swrt('at+cipsend='+str(index),1) 176 wrtmsg(tran*'v',15) 177 else: 178 swrt('at+cipsend='+str(index)+','+str(tran),1) 179 wrtmsg(tran*'v',15) 180 181 #sigle-mode Ts 182 init() 183 shutConn() 184 cipmod() 185 sigmod() 186 sigsnd() 187 188 189 #online-mode Ts 190 init() 191 shutConn() 192 cipmod() 193 sigmod() 194 olmod() 195 modTs() 196 197 #mux-mode Ts 198 init() 199 shutConn() 200 cipmod() 201 sigmod() 202 muxmod() 203 [muxTS(indset) for indset in indexsets] 204 205 init() 206 shutConn() 207 ser.close()