sending and receive data from client using python -
i using rs485 wifi router
above model rs485 communication.
intailly tried simple code arduino. sending serial data on client every 2s.
void setup() { serial.begin(9600); } void loop() { serial.println("smcb1,1,1,1,1,2,3,4,5,6,6,7,8,9,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2"); delay(2000); }
i used below python code, receive data client 1 , client 2
import socket tcp_ip = '192.168.1.3' tctcp_ip1='192.168.1.2' tcp_port = 8899 buffer_size = 100 buffer_size1=100 message = "hello, world!" s = socket.socket(socket.af_inet, socket.sock_stream) s1 = socket.socket(socket.af_inet, socket.sock_stream) s.connect((tcp_ip, tcp_port)) s1.connect((tctcp_ip1, tcp_port)) s.send(message) s1.send(message) data = s.recv(buffer_size) data1= s1.recv(buffer_size) s.close() s1.close() print "received data:", data print "received data:", data1
now have uploaded modbus rtu code , sending request , trying response. request need send in hex format. tried wired communication seems working fine. have shared request format below. able read thing process under progress read
import socket tcp_ip = '192.168.1.3' tctcp_ip1='192.168.1.2' tcp_port = 8899 buffer_size = 1000 buffer_size1=1000 #message = "01 03 00 00 00 1d 85 c3" #message1 = "02 03 00 00 00 1d 85 fo" message = "01030000001d85c3" message1 = "02030000001d85fo" s = socket.socket(socket.af_inet, socket.sock_stream) s1 = socket.socket(socket.af_inet, socket.sock_stream) print "before connect" s.connect((tcp_ip, tcp_port)) s1.connect((tctcp_ip1, tcp_port)) print "after connect" print message print message1 print "request before sending" s.send(message1) s1.send(message) print "request after sending" data = s.recv(buffer_size) data1= s1.recv(buffer_size) s.close() s1.close() print "received data:", data print "received data:", data1
now problem facing trying send request not able reply. wired communication can able data.
i have modified code getting response. need convert data decimal format. understna received data correct or not. need convert whole data decimal format. let me know how can achieve this
import socket tcp_ip = '192.168.1.3' tcp_port = 8899 buffer_size = 1000 s = socket.socket(socket.af_inet, socket.sock_stream) s.connect((tcp_ip, tcp_port)) s.send('01030000001d85c3'.decode('hex')) decode_hex='01030000001d85c3'.decode('hex') print decode_hex print "decode hex:",decode_hex data = s.recv(1024) s.close() print "data recieved :", data d=data.encode('hex').upper() print 'hexa decimal value deceived:',repr(d) mod_hex=data.encode('hex').upper() print 'hexa decimal value modified:',repr(mod_hex) #print 'received',d decimal=int(d,20) print "decimal converted value:",decimal
here out put file attached
decode hex: data recieved : : hexa decimal value deceived: '01033a0000000d00120041003400410056004b000000000000000000df000000e1000000e700f800e600ec0000000000000000001f00010001000100dc496e' hexa decimal value modified: '01033a0000000d00120041003400410056004b000000000000000000df000000e1000000e700f800e600ec0000000000000000001f00010001000100dc496e' decimal converted value: 214364598880234057828928146607817359105482384449559342919180352622648369076567392756173653847449417881349290133492250705920000000000003670016655364096025643555734
Comments
Post a Comment