Why is python sendall sending the last byte twice?

I am using Python 2.7.5 to send a message to the modbus tcp simulator. I don't understand why python is sending the last byte twice to sendall:

This is what the python script outputs to my code (below) ...

S: 00020000000c1110000400030600790083008d
R: 000200000006111000040003
R: 8d0000000003d8f603

      

The simulator logs this where (TX is received from the script and RX is sent to the script) ...

TX: 00020000000c1110000400030600790083008d
RX: 000200000006111000040003
TX: 8d
Modbus message in error x03
RX: 8d0000000003d8f603

      

So the question is ... why is 8d sent twice (according to the Modbus simulator listening)? Because of this, the simulator throws an error ... Code excerpt:

def writeMultipleHoldingRegister(socket, transactionid, unitid, address,valuesToWrite):
    TRANSACT_ID_BYTES = convertIntToBytes(int(transactionid))
    DATA_ADDRESS_BYTES = convertIntToBytes(int(address))
    FUNCTIONCODE_BYTES = '10'
    NUMREGISTERS_BYTES = convertIntToBytes(len(valuesToWrite))
    NUMBYTES = len(valuesToWrite)*2
    NUMBYTES_BYTES = str(('%04X' % NUMBYTES)[2:4])
    MESSAGE_LENGTH_BYTES = convertIntToBytes(int(6+(len(valuesToWrite)*2)))
    modbustcp_write = (TRANSACT_ID_BYTES + '0000' + MESSAGE_LENGTH_BYTES + unitid + FUNCTIONCODE_BYTES + DATA_ADDRESS_BYTES + NUMREGISTERS_BYTES + NUMBYTES_BYTES).decode('hex')
    for value in valuesToWrite:
        modbustcp_write = modbustcp_write + convertIntToBytes(int(value)).decode('hex')
    print 'S:', binascii.hexlify(modbustcp_write)
    socket.sendall(modbustcp_write) #modbus tcp
    data = socket.recv(12)
    print 'R:', binascii.hexlify(data)

    HOST = 'localhost'
    PORT = 502
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    writeMultipleHoldingRegister(s, 2, '11', 4, [121, 131, 141])
    print 'R:', binascii.hexlify(s.recv(100))
    s.close()

      

Following the advice is left in the comment. Process Monitor showed me traffic, but not the bytes themselves. So I downloaded and ran RawCAP and viewed the output in Wireshark. To paraphrase ... where "S" is sent by the simulator and "R" is received by the simulator ...

R: 00020000000c1110000400030600790083008d (modbus request recv)
S: 000200000006111000040003 (modbus ack good request sent)
S: 8d0000000003d8f603 (modbus error notification sent...wtf)

      

I never see "8d" twice ... so I don't know what's going on here. Possibly a bug in the simulator ... http://www.plcsimulator.org/

+3


source to share





All Articles