Welcome, Guest
Username: Password: Remember me

TOPIC: Modbus tcp with arduino

Re: Modbus tcp with arduino 11 years 8 months ago #4187

  • Van Dyck
  • Van Dyck's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 19
  • Karma: 0
Hello Tim, Hello Martin,

As stated before, I got it working, but I was now trying to extend with reading out a register.
(Today still a variable resistor, but I will replace it with the digital temperature sensors, with I2C bus as done by Martin later on)

But there it goes wrong. When checking the wireshark trace, it seems there is an issue at the arduino side.

My code is as follows. (rather easy) and using the mudbus of Martin.
void loop()
{
Mb.Run();
Mb.R[0] = analogRead(A2);
//Digital inputs
Mb.C[0] = digitalRead(3); //pin 7 to Mb.C[7]
Mb.C[1] = digitalRead(5);
Mb.C[2] = digitalRead(6);
Mb.C[3] = digitalRead(7);
//Digital outputs
digitalWrite(8, Mb.C[4]); //pin 8 from Mb.C[8]
digitalWrite(9, Mb.C[5]);
}

In the wireshark trace, I get 2 issues. (trace atached)
- The arduino seems to have it difficult with 2 modbus/TCP messages in one TCP segment.
- The arduino simply doesn't answer to the request for the register.

arduino difficult with 2 modbus/TCP messages in one TCP segment
It seems proview changed the behaviour compared to my previous project.
Now it puts 2 modbus/tcp messages (MBAP header + function code + data) within one TCP segment.
The first modbus/tcp has function code 1 read coils, the second has function 15, force multiple coils.
As far as I know Martin developed the code. (or at least the extension made for function code 15)
It would be nice if you could have a look. I will look further into the c-code as well.

I had a look at the specs as weel, and couldn't find in a first glance if it is allowed to have 2 modbus/tcp messages within one tcp segment.


Arduino simply doesn't answer to "read input registers"
When checking the trace, it seems proview send out all correctly,but no answer from arduino.
I will sart some debugging myself, but as you have expirenece with it, help (or a good direction) is appriciated.

Tim, just let me know how your trip was, and if you could see the projects of Martin. We could get a beer afterwards, but a little bit less then last time.

Thansk already for your support and best regards,
Wout
Attachments:
The administrator has disabled public write access.

Re: Modbus tcp with arduino 11 years 8 months ago #4191

  • siamect
  • siamect's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 116
  • Thank you received: 1
  • Karma: 1
Hello Wout

This issue with multiple modbus/TCP messages in one TCP segment I have had but I thought I had fixed that 12 Feb 2012 11:29.

gitorious.org/mudbus/mudbus/commits/master
gitorious.org/mudbus/mudbus/commit/7f2a7...8c44723989fffb2db098

So obviously it does not work properly, at least not in this case. I will take a closer look at this but it may take a few days... The code is really messy :whistle:

It is really good to get this feedback, thanks a lot...

Martin
The administrator has disabled public write access.

Re: Modbus tcp with arduino 11 years 8 months ago #4193

  • Van Dyck
  • Van Dyck's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 19
  • Karma: 0
Hello Martin,

I got all working now, but some strange things happens.

First of all, it seems my ubuntu (12.04) made some updates, and now it is not combining the modbus/TCP packets anymore within a single TCP segment. So I cannot reproduce the error anymore.

Also the update packets disabled my arduino USB connection. So I had to reload it by my MAC, just to be sure I was using your latest mudbus.h and .cpp files.

Another issue which I had I got solved on a strange way.
The problem was, is as soon as I added a Modbus_Module (with functioncode registers) within my object Modbus_TCP_Slave, the other Modbus_Module (with functioncode ReadCoils) was only working for the first channel, not for the others anymore.

Afterwards I changed the sequence of my objects Modbus_module within the object Modbus_TCP_Slave and it was working again. Strange behaviour, but I will do some further testing.

So before the Node tree was as follows (not working)
Nodes -> $NodesHier
testmodbus -> $Node
modbusPCeee -> Modbus_Master
ModbusArduino -> Modbus_TCP_Slave
ArdModRegRead -> Modubus_Module (functioncode ReadHoldingRegisters)
ChanAi1 -> ChanAi
ArdModRead -> Modbus_Module (functioncode ReadCoils)
ChanDi4 -> ChanDi (Number -> 3)
ChanDi3 -> ChanDi (Number -> 2)
ChanDi2 -> ChanDi (Number -> 1)
ChanDi1 -> ChanDi (Number -> 0)
ArdModWrite -> Modbus_Module (functioncode WriteMultipleCoils; address 4)
LedGreen -> ChanDo (Number -> 0)
LedRed -> ChanDo (Number -> 1)

After the change the Node tree was as follows (complete working)
Nodes -> $NodesHier
testmodbus -> $Node
modbusPCeee -> Modbus_Master
ModbusArduino -> Modbus_TCP_Slave
ArdModRead -> Modbus_Module (functioncode ReadCoils)
ChanDi4 -> ChanDi (Number -> 3)
ChanDi3 -> ChanDi (Number -> 2)
ChanDi2 -> ChanDi (Number -> 1)
ChanDi1 -> ChanDi (Number -> 0)
ArdModWrite -> Modbus_Module (functioncode WriteMultipleCoils; address 4)
LedGreen -> ChanDo (Number -> 0)
LedRed -> ChanDo (Number -> 1)
ArdModRegRead -> Modubus_Module (functioncode ReadHoldingRegisters)
ChanAi1 -> ChanAi

Trace in wireshark looked 2 times the same, so no difference from arduino.
Do you know if this is normal behavior? And if yes, do you know if it is for more objects like this.

But the important thing is, I (with of course a lot of your help) got it working. Up to the next step, the next extension..

If you (or anybody else on the forum) need more info, just let me know, and I will try to provide.

My next project/test will be, let the arduino function as a modbus client, and proview as server.
The background is to reduce the network traffic. Now I have to scan continuous the modbus, while the buttons will only be pressed sporadically. I also looked and it seems you got proview working on the rasperry, as an operator system. Looks interesting as well.

Thanks & best regards,
Wouter
The administrator has disabled public write access.

Re: Modbus tcp with arduino 11 years 8 months ago #4195

  • claes
  • claes's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 3180
  • Thank you received: 502
  • Karma: 133
Hi Wouter,

I think you should change the order of the ChanDi. Normally you start with Number 0 followed by icreasing Numbers. A new Number 0 will indcate the start of a new word. So in your case Number 1, 2 and 3 is placed in the first word and Number 0 in the second.

/Claes
The administrator has disabled public write access.

Re: Modbus tcp with arduino 11 years 8 months ago #4196

  • Van Dyck
  • Van Dyck's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 19
  • Karma: 0
Hello Claes

Indeed, it when it try the following as well, it works

Nodes -> $NodesHier
testmodbus -> $Node
modbusPCeee -> Modbus_Master
ModbusArduino -> Modbus_TCP_Slave
ArdModRegRead -> Modubus_Module (functioncode ReadHoldingRegisters)
ChanAi1 -> ChanAi (number -> 0)
ArdModRead -> Modbus_Module (functioncode ReadCoils; address 0)
ChanDi1 -> ChanDi (Number -> 0)
ChanDi2 -> ChanDi (Number -> 1)
ChanDi3 -> ChanDi (Number -> 2)
ChanDi4 -> ChanDi (Number -> 3)
ArdModWrite -> Modbus_Module (functioncode WriteMultipleCoils; address 4)
LedGreen -> ChanDo (Number -> 0)
LedRed -> ChanDo (Number -> 1)

OK, I understand I have to be careful when creating modbus objects, sequence (of objects) and numbers matter.

Thanks a lot for your support. (I'm still in a learning curve)

Best regards,
Wouter
The administrator has disabled public write access.

Re: Modbus tcp with arduino 11 years 1 week ago #5187

  • angelferni
  • angelferni's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 2
  • Karma: 0
Hello,

I am working with an arduino and trying to set a communication with a Modicom PLC. I found the same library which you are working with, but I’m a beginner and I have some doubts with the code…
Why do I have to define these variables?

#define MB_N_R 125
#define MB_N_C 128
#define MB_PORT 502

Are “MB_N_R”, “MB_N_C” and “MB_PORT” special definitions? I cannot find anywhere why I have to use exactly these definitions at the beginning of the code.
Can someone explain it to me? Many thanks in advance.
The administrator has disabled public write access.
Time to create page: 7.909 seconds