Good morning,
In the enclosed place the code I use to control arduino (control) via codesys with mdbusrtu protocol.
Everything works perfectly!
Now I wanted to ask you how to modify this code to work on modbus tcp slave.
I found much code on the internet but it made me no confusion.
Could you help me?
Thank you, Alberto
\#include<Controllino.h> /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */\#include<ModbusRtu.h>\#defineID3Modbusslave(ID,0,0);// this is slave ID and RS-232 or USB-FTDIint8_tstate=0;// data array for modbus network sharinguint16_tau16data[9];voidsetup(){ io_setup();// I/O settings slave.begin(38400);}voidloop(){ // poll messages state=slave.poll(au16data,9); io_poll();}voidio_setup(){ //Set digital out pinMode(CONTROLLINO_R0,OUTPUT);// next to the digital output screw terminal  pinMode(CONTROLLINO_R1,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R2,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R3,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R4,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R5,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R6,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R7,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R8,OUTPUT);// next to the digital output screw terminal pinMode(CONTROLLINO_R9,OUTPUT);// next to the digital output screw terminal // Set analog in (used as digital) pinMode(CONTROLLINO_A0,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A1,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A2,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A3,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A4,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A5,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A6,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A7,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A8,INPUT); // next to the analog input screw terminal pinMode(CONTROLLINO_A9,INPUT); // next to the analog input screw terminal}voidio_poll(){ // get analog inputs -> au16data[0] bitWrite(au16data[0],0,digitalRead(CONTROLLINO_A0)); bitWrite(au16data[0],1,digitalRead(CONTROLLINO_A1)); bitWrite(au16data[0],2,digitalRead(CONTROLLINO_A2)); bitWrite(au16data[0],3,digitalRead(CONTROLLINO_A3)); bitWrite(au16data[0],4,digitalRead(CONTROLLINO_A4)); bitWrite(au16data[0],5,digitalRead(CONTROLLINO_A5)); bitWrite(au16data[0],6,digitalRead(CONTROLLINO_A6)); bitWrite(au16data[0],7,digitalRead(CONTROLLINO_A7)); bitWrite(au16data[0],8,digitalRead(CONTROLLINO_A8)); bitWrite(au16data[0],9,digitalRead(CONTROLLINO_A9)); //bitWrite( au16data[0], 10, digitalRead(CONTROLLINO_IN0)); //bitWrite( au16data[0], 11, digitalRead(CONTROLLINO_IN1)); // set digital outputs -> au16data[1] digitalWrite(CONTROLLINO_R0,bitRead(au16data[1],0)); digitalWrite(CONTROLLINO_R1,bitRead(au16data[1],1)); digitalWrite(CONTROLLINO_R2,bitRead(au16data[1],2)); digitalWrite(CONTROLLINO_R3,bitRead(au16data[1],3)); digitalWrite(CONTROLLINO_R4,bitRead(au16data[1],4)); digitalWrite(CONTROLLINO_R5,bitRead(au16data[1],5)); digitalWrite(CONTROLLINO_R6,bitRead(au16data[1],6)); digitalWrite(CONTROLLINO_R7,bitRead(au16data[1],7)); digitalWrite(CONTROLLINO_R8,bitRead(au16data[1],8)); digitalWrite(CONTROLLINO_R9,bitRead(au16data[1],9)); // diagnose communication au16data[6]=slave.getInCnt(); au16data[7]=slave.getOutCnt(); au16data[8]=slave.getErrCnt();}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good morning,
In the enclosed place the code I use to control arduino (control) via codesys with mdbusrtu protocol.
Everything works perfectly!
Now I wanted to ask you how to modify this code to work on modbus tcp slave.
I found much code on the internet but it made me no confusion.
Could you help me?
Thank you, Alberto
Depending on what board you use on the arduino, you could check out Mudbus. This has worked great for me in the past.
I used it to make a custom library for a Uno using Wifi shield and another for the Yun. http://www.verra.biz/free-downloads
If you need more help, I think you are better of posting it on the arduino forums