Hello, I am trying to use modbus from a plc through a gate way connected to a line of PCB. The nano is on the pcb. I have tried multiple modbus libraries and none of them seemed to be working for me. I am wondering if I am missing something in my code or if it's a hardware issue. I am continuing a project that someone else has started so it's been a journey to figure out what is going on.
Plc is master, nanos are slaves.
RS485, using a RS487 max on the pcb.
Also using a usb converter.
I have primarily been working with the Simple Modbus slave library. Then I converted it to ModbusRTU library and also ModbusRTUSlave.
I tried different types of modbus simulators, none of them can communicate to the arduino.
I have downloaded drivers, updated everything I can, research every part I am using to make sure I have correct software and connections. Ive research modbus on arduino for two weeks now and nothing has help solved my problem.
I have tried switching the connections of the rx and tx pins.
The board has LED lights and buttons. Those are the values for the registers. There is a dipswitch that determines the address. I've changed somethings since making the comments. Also somethings like the intensity is commented out because it is not being used.
SimpleModbus
#include <SimpleModbusSlave.h>
//Working with Amber- setup for the PCB circuit
/* parameters(HardwareSerial* SerialPort,
long baudrate,
unsigned char byteFormat,
unsigned char ID,
unsigned char transmit enable pin,
unsigned int holding registers size,
unsigned int* holding register array)
*/
/* Valid modbus byte formats are:
SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
SERIAL_8E1: 1 start bit, 8 data bits, 1 Even parity bit, 1 stop bit
SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bit, 1 stop bit
You can obviously use SERIAL_8N1 but this does not adhere to the
*/
#define TXEnable 2
//LED colors set by different pins
#define GRN 9
#define RED 6
#define BLU 5
#define Intensity 255
//Functions
byte address();
void Regs();
//void Testing();
//////////////// registers of your slave ///////////////////
enum
{
// just add or remove registers and your good to go...
COLOR,
Button,
HOLDING_REGS_SIZE // leave this one
// total number of registers for function 3 and 16 share the same register array
// i.e. the same address space
};
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600,SERIAL_8N2);
int SlaveID;
SlaveID = address();
modbus_configure(&Serial, 9600, SERIAL_8N2, SlaveID, TXEnable, HOLDING_REGS_SIZE, holdingRegs);
// modbus_update_comms(baud, byteFormat, id)
modbus_update_comms(9600, SERIAL_8N2, SlaveID);
// pinMode(LED, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GRN, OUTPUT);
pinMode(BLU, OUTPUT);
pinMode(10, INPUT);
//Intialize all LEDs off
analogWrite(GRN, 0);
analogWrite(RED, 0);
analogWrite(BLU, 0);
}
void loop()
{
// modbus_update() is the only method used in loop().
modbus_update();
Regs();
/
}
/* The function address sets up the dipswitch with the pins on the Arduino.
The values of each pin are then made into one binary address.
The address is used by the Master (PLC) to communicated with individual lights.
The Slave (lights) compare its address to the address the master sends out.*/
byte address(){
//Serial.begin(9600);//Begin Serial Communication at baud rate of 9600
char dipPins[] = {A4, A3, A2, A1, A0}; //DIP Switch Pins
int i,j=0; //Counter variables
//Initialize Dipswitch
for(i = 0; i<=4; i++){
pinMode(dipPins[i], INPUT); // sets the digital pin 2-5 as input
digitalWrite(dipPins[i], HIGH);} //Set pullup resistor on
delay(100);
//Read Dipswitch's Values for pins 1-5
for(i=0; i<=4; i++){
j = (j << 1) | digitalRead(dipPins[i]);} // read the input pin then shift to the next bit
return j;} //return address
/*The Regs function reads or writes to the slaves registers.
The Master sends a binary string, the first section is color, second is address, third is intensity.
There are 3 registers: Button, PWM_VAL, and COLOR.
Button tells the Master if the button is pressed or unpressed. The Slave writes to this register and the master reads it.
PWM_VAL is the intensity or the flashing speed of the light. The Slave reads from this register and the Master writes to it.
COLOR is the color of the LED. Each color is a pin on the board that connect to LED pins. The Slave reads this register and the Master writes to it.
The shift instruction is to read from each bit of the binary string. A 1 indicates the settings that are wanted and a 0 is for the settings that aren't wanted.*/
void Regs(){
int btnState;//State of Button
//Button Register
btnState = digitalRead(10); //Read if button is HIGH or Low from pin 10
//Button Pressed
if(btnState == HIGH)
holdingRegs[Button] = 1;
//Button NOT Pressed
else if(btnState == LOW)
holdingRegs[Button] = 2;
// Error
else
holdingRegs[Button] = 3;
//Pulse Width Modulation Register
//High
/*if(holdingRegs[PWM_VAL] & (1<<0))
Intensity = 255;
//MEDIUM
else if(holdingRegs[PWM_VAL] & (1<<1))
Intensity = 125;
//LOW
else if(holdingRegs[PWM_VAL] & (1<<2))
Intensity = 50; */
//Color Register
//GREEN
if(holdingRegs[COLOR] = 1){
analogWrite(GRN, Intensity);
analogWrite(RED, 0);
analogWrite(BLU, 0);}
//RED
else if(holdingRegs[COLOR] = 2) {
analogWrite(GRN, 0);
analogWrite(RED, Intensity);
analogWrite(BLU, 0);}
//BLUE
else if(holdingRegs[COLOR]= 4) {
analogWrite(GRN, 0);
analogWrite(RED, 0);
analogWrite(BLU, Intensity);}
//YELLOW
else if(holdingRegs[COLOR] = 8) {
analogWrite(GRN, Intensity*.39);
analogWrite(RED, Intensity);
analogWrite(BLU, 0);}
// Wrong number, defaults to White
else{
analogWrite(GRN, 255);
analogWrite(RED, 255);
analogWrite(BLU, 255);}
}
//Turns on all the lights to test if they are working
/*void Testing(){
int j =0;
modbus_update_comms(9600, SERIAL_8N2, j);
analogWrite(GRN, 255);
analogWrite(RED, 255);
analogWrite(BLU, 255);
}*/
My Problem:
The Modbus simulations / Gateway cant read or write to the arduino.
I got the arduino to return 0s instead of just not being able to transmit or receive anything.
Do not be hateful, I am asking for assistance and CONSTRUCTIVE criticism please.