Hi Guys,
I am trying to send data from my local PC to my PMC with Modbus TCP protocol, but I am not able establish communication MOST of the times. I was able to establish communication and send data certain times, but the next time when I try to reload the same code , I am not able to communicate between PMC and PC.
Here are the following code snippets(NOTE: I use a LInux VM as my OS).
- Python server
from pyModbusTCP.server import ModbusServer, DataBank
from socket import *
host = '192.168.1.170'
port = 1500
class mydatabank(DataBank):
def __init__(self):
super().__init__()
def get_holding_registers(self, address,number = 1):
self._h_regs[0] = 0
self._h_regs[1] = 1
#repcakage data into a list
try:
return[self._h_regs[i] for i in range(address,address + number)]
except KeyError:
return
if __name__ == '__main__':
server = ModbusServer(host=host, port=port, data_bank=mydatabank())
server.start()
- Arduino Client
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
#include <Arduino_MachineControl.h>
using namespace machinecontrol;
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);
byte mac[] = {
0x90, 0xA2, 0xDA, 0xF8, 0x5A, 0xF3
};
IPAddress ip(192, 168, 1, 11);
IPAddress server(192,168,1,170);
void setup() {
Serial.begin(9600);
while(!Serial)
;
Ethernet.begin(mac, ip);
Serial.print("My IP is ");
Serial.println(Ethernet.localIP());
int f = modbusTCPClient.begin("192.168.1.170", 1500);
Serial.println(f);
}
void loop() {
// int f = modbusTCPClient.begin("192.168.1.170", 100);
// Serial.println(f);
// digital_outputs.set(4, modbusTCPClient.holdingRegisterRead(0));
// delay(2000);
// digital_outputs.set(4, modbusTCPClient.holdingRegisterRead(1));
int value = modbusTCPClient.holdingRegisterRead(1);
Serial.println(value);
}
I am totally new to this Protocol. So Any help regarding solving this would be greatly appreciated!
Best Regards,
H_SE
