Hello,
please, I write a project for Arduino Mega2560 (IDE 1.6.5) which read data from Modbus on Serial1, store in array and distribute to higher system by SNMP answers.
Standalone project for decoding SNMP request works well (read SNMP UDP datagram, decode it and send back answer) as standalone project for reading modbus (send modbus question to slave, receive answer and decode it) works well.
But problems start when I merge this two project to one.
In main loop I cyclicaly calling procedure for update data from modbus on Serial1 (one time per 30 sec) and calling procedure for handle incoming UDP packets (SNMP requests). For debug purpose, I send some information to Serial0.
Whats a problem : When I have in loop active boths of moduls, answers to UDP packets is OK, but reading from modbus is crazy
In this code
if (millis() > (LastModbus+PeriodModbus)) {
retval = read_input_registers(1,12545,8, dest1, 8);
delay(200);
LastModbus = millis();
}
Serial.println ("Value from modbus: ");
for (int i=0;i<8;i++) {
Serial.print(dest1[i], HEX);
Serial.print(" ");
}
Serial.println();
I see on Serial1 echo, that message to modbus are sent every 30 seconds (as PeriodModbus variable is 30000). But on Serial0 monitor I see only part of message "Value from m" and in data are not values.
When I comment part for handle UDP
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Modbus function will start works correct. (all messages to Serial0 are writed correct, data from modbus are in data[] array...)
Modbus code was taken from this project
https://github.com/4-20ma/ModbusMaster
and UDP was taken from arduno examples.
I have not idea, how (and why) udp ethernet makes modbus part of code so crazy..
All code is too long to write here directly and is attached.
Thank you for help or idea where to try find a problem
Gateway.ino (33.6 KB)