Sample codes to write modbus via MAX485

i am looking for sample codes to write modbus to another device (PLC) via MAX485

Hi @chickbuildljtelebrico

Try to Google arduino rs485 example, or arduino rs485 modbus

There are plenty of examples online available, and even Gemini, for instance, provides a code snippet right out of the box.

There are also many examples right here! A simple search for RS485 or modbus should provide examples.

You may be lucky and find the exact code you need but if not, be prepared to experiment yourself.

if this means, that you are searching for a Modbus Server (Slave) implementation, you can check my Library which comes with several examples.

But I suggest you define precisely what your device should really do.

thank you so much. i am able to read RFID cards via Pin 2&3. I want to write the read data to modbus device thru MAX485 (connected to Pin 0/Rx & Pin 1/Tx to RO and DI of MAX485. However, response of Modbus Poll (thru USB-485 converter) is checksum error/insufficient bytes.

just to clarify something

your device+RFID will read tags. That device is a Modbus Slave.
The PLC is the Modbus Master. The PLC will ask your device regarding the last readed tag.
Your Device should answer the PLC request.

Ist that correct?

yes it is. my Allen Bradley micro850 PLC is configured as Master.

Please show your code using code tags.

#include <ArduinoModbus.h>
#include <ArduinoRS485.h>
#include<ModbusRTUServer.h>
#include<HardwareSerial.h>
#include<SoftwareSerial.h>

#define DE_RE_PIN 10

const byte slaveAddress = 1; // Modbus slave address
const int holdingRegisterAddress = 0; // Address of the holding register to write
int valueToWrite = 1234; // Value to write to the holding register


#define MAX_BITS 100                 // max number of bits 
#define WEIGAND_WAIT_TIME  3000      // time to wait for another weigand pulse.  

unsigned char databits[MAX_BITS];    // stores all of the data bits
unsigned char bitCount;              // number of bits currently captured
unsigned char flagDone;              // goes low when data is currently being captured
unsigned int weigand_counter;        // countdown until we assume there are no more bits
 
unsigned long facilityCode=0;        // decoded facility code
unsigned long cardCode=0;            // decoded card code
const int numHoldingRegisters = 10;


void ISR_INT0()
{
  //Serial.print("0");   // uncomment this line to display raw binary
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
 
}
 
// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1()
{
  //Serial.print("1");   // uncomment this line to display raw binary
  databits[bitCount] = 1;
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
}

void setup()
{
    ModbusRTUServer.begin(1, 9600);
  
  int address;
  int nb;
  address=1;
  nb = 1;
      // ... start serial/network ... 
    
     // Configure 10 holding registers starting at address 0x00 (40001) 
     ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
  //ModbusRTUServer.begin(id, baudrate, config);
  
  //Serial.println("Modbus RTU Client Example - Write Holding Register");
  if (!ModbusRTUClient.begin(9600, SERIAL_8E1)){
    Serial.println("Failed to start Modbus RTU Client");
    while (true);
  }
  pinMode(13, OUTPUT);  // LED
  pinMode(2, INPUT);     // DATA0 (INT0)
  pinMode(3, INPUT);     // DATA1 (INT1)
 
  Serial.begin(9600);
  
  Serial.println("RFID Readers");
 
  // binds the ISR functions to the falling edge of INTO and INT1
  attachInterrupt(0, ISR_INT0, FALLING);  
  attachInterrupt(1, ISR_INT1, FALLING);
  weigand_counter = WEIGAND_WAIT_TIME;
    
}
 
void loop()
{
  ModbusRTUServer.poll();
  // This waits to make sure that there have been no more data pulses before processing data
  if (!flagDone) {
    if (--weigand_counter == 0)
      flagDone = 1;	
  }
 
  // if we have bits and we the weigand counter went out
  if (bitCount > 0 && flagDone) {
    unsigned char i;
 
    Serial.print("Read ");
    Serial.print(bitCount);
    Serial.print(" bits. ");
 
    // we will decode the bits differently depending on how many bits we have
    // see www.pagemac.com/azure/data_formats.php for mor info
    if (bitCount == 32)
    {
      // 35 bit HID Corporate 1000 format
      // facility code = bits 2 to 14
      for (i=2; i<16; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
 
      // card code = bits 15 to 34
      for (i=16; i<32; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
 
      printBits();
    }
    else if (bitCount == 26)
    {
      // standard 26 bit format
      // facility code = bits 2 to 9
      for (i=1; i<9; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
 
      // card code = bits 10 to 23
      for (i=9; i<25; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
 
      printBits();  
    }
    else {
      // you can add other formats if you want!
     Serial.println("Unable to decode."); 
    }
 
     // cleanup and get ready for the next card
     bitCount = 0;
     facilityCode = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++) 
     {
       databits[i] = 0;
     }
  }
 
}
 
void printBits()
{
      // this function is for reading the access card tags. it is working now.
      Serial.print("FC = ");
      Serial.print(facilityCode);
      Serial.print(", CCJT = ");
      Serial.println(cardCode); 
      uint8_t serverAddress = 1;
      uint16_t registerAddress = 1;
      uint16_t valueToWrite = cardCode;
      Serial.print("Attempting to write value ");
      Serial.print(valueToWrite);

    // this function aims to write to modbus register. 
    //but checksum error at modbus poll
      uint16_t sensorvalue = cardCode;
      int id1;
      id1 = 1;
      int regadd;
      regadd=400001;
      ModbusRTUServer.holdingRegisterWrite(id1,sensorvalue);
      Serial.print(", sensorva = ");
      Serial.println(sensorvalue);
     
}