import socket import datetime def my_fun_send_OK(): my_str_short = [0x10, 0x80, 0x01, 0x00, 0x81, 0x16] my_str_short[2] = dtu_add_Low my_str_short[3] = dtu_add_High s2 = my_101_com_genert_CRC(my_str_short) s3 = bytes(s2) conn.send(s3) my_all_step = 3 print("send data:", end='') my_str_to_hex_display(s3) def get_time_count(): i=datetime.datetime.now() time_count=i.hour*3600+i.minute*60+i.second time_count2=time_count%65535 y = time_count2 % 256 x = time_count2 // 256 return int(x),int(y) #########################3 def my_str_to_hex_display(s): #s2 = bytes(s, 'utf-8') if s==None: return print(datetime.datetime.now(),": ",end='') s2=s for i in s2: print(hex(i), end='') if i!=s2[-1]: print('-', end='') print() ####################### def my_get_fram(s0): #s2=bytes(s0,'utf-8') s2=s0 status=0 for i in s2: if i==0x10: my_start=s2.index(i) status=0x10 elif i==0x68: my_start=s2.index(i) status=0x68 else: my_start=0 status=0 if status==0x10: x=s2[my_start+5] if x==0x16: s3=s2[my_start:my_start+6] break elif status==0x68: x=s2[my_start+3] if x==0x68: lenth=int(s2[my_start+1]) s3=s2[my_start:my_start+lenth+6] break else: s3=None; #s = str(s3, encoding="utf-8") s=s3 s = s2 if s3!=None: #print("source data:", end='') #my_str_to_hex_display(s) print("get data:",end='') my_str_to_hex_display(s) else: print("None get data") return s3 #19201012131415161718 1213101213141516171819 def my_101_com_genert_CRC(s0): if s0[0]==0x10: crc=s0[1]+s0[2]+s0[3] s0[4]=crc%256 s0[5]=0x16 else: my_lenth=s0[1] count=0 for ii in range(0,my_lenth): count=count+s0[ii+4] crc=count%256 ii=ii+1 s0[ii+4]=crc s0[ii + 5] = 0X16 s1=s0 return s1 ################################333 sk = socket.socket() sk.bind(("106.14.41.25",2216)) sk.listen(5) conn,address = sk.accept() conn.sendall(bytes("Hello world",encoding="utf-8")) my_all_step=0 my_heart_time_count=1 while True: buf=conn.recv(1024) my_get_com_bytes=my_get_fram(buf) if my_get_com_bytes==None: continue if my_get_com_bytes[0]==0x10: dtu_add_Low = my_get_com_bytes[2] dtu_add_High = my_get_com_bytes[3] else: dtu_add_Low = my_get_com_bytes[5] dtu_add_High = my_get_com_bytes[6]