So I am using the below example I found online:
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R MB_N_C
//Port 502 (defined in Mudbus.h) MB_PORT
void setup()
{
uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
uint8_t ip[] = { 192, 168, 10, 245};
uint8_t gateway[] = { 192, 168, 10, 1 };
uint8_t subnet[] = { 255, 255, 255, 0 };
Ethernet.begin(mac, ip, gateway, subnet);
//Avoid pins 4,10,11,12,13 when using ethernet shield
delay(5000);
Serial.begin(9600);
//pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
}
void loop()
{
Mb.Run();
//Analog inputs 0-1023
Mb.R[1] = analogRead(A0); //pin A0 to Mb.R[0]
Mb.R[2] = analogRead(A1);
Mb.R[3] = analogRead(A2);
Mb.R[4] = analogRead(A3);
Mb.R[5] = analogRead(A4);
Mb.R[6] = analogRead(A5);//analogRead(A5);
//Analog outputs 0-255
analogWrite(6, Mb.R[6]); //pin ~6 from Mb.R[6]
//Digital inputs
Mb.C[7] = digitalRead(7); //pin 7 to Mb.C[7]
//Digital outputs
digitalWrite(13, Mb.R[13]); //pin 13 from Mb.C[8]
}
I am trying to get Advanced HMI to talk to the Arduino and so far I guessed at which registers to use and could only get the Analog Inputs to work.
I don't know what registers I need to drive in order to read or write to the pins.
For example:
Mb.R[1] = analogRead(A0); //Register 40002
Mb.R[2] = analogRead(A1); //Register 40003
Mb.R[3] = analogRead(A2); //Register 40004
Mb.R[4] = analogRead(A3); //Register 40005
Mb.R[5] = analogRead(A4); //Register 40006
Mb.R[6] = analogRead(A5); //Register 40007
I guessed to find out that the 4000x registers are to read the assigned registers. And so far, they are the only ones I got to work.
I was expecting that the 1000x, and 3000x registers to be for reading and writing to digital inputs/outputs.
Can anyone explain the function codes of modbus for this idiot??? I've spent the last hour looking for answers and explanations but all of the hex stuff gets to me.