Sending Modbus write single register from Arduino to pic microcontroller

I have a pic microcontroller that I can connect to with a USB to rs232 cable and I can communicate with Modbus protocol
now i want to connect to the pic microcontroller with arduino mega2560
I connected the arduino to rs232 to ttl (max3232) and the max 3232 is connected to the pic
I use this library Modbus RTU Master
and this is my code

#include <ModbusRTUMaster.h>
ModbusRTUMaster modbus(Serial3);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  modbus.begin(9600);
  modbus.setTimeout(1000);

}

void loop() {
  // put your main code here, to run repeatedly:
  modbus.clearExceptionResponse();
  modbus.writeSingleHoldingRegister(1,0,0);
  Serial.println(modbus.getExceptionResponse());
  delay(5000);

}

the result of the modbus.getExceptionResponse() is 0 and the register is not being rewritten
How can I fix this?

yes - assuming the PIC microcontroller has an ethernet connection and you can program both microcontrollers
use a simple TCP or UDP protocol

the pic microcontroller does not have an ethernet connection I want to make it so that it can connect to ethernet using enc28j60 and arduino

you require ethernet functionality for the PIC?
what PIC are you using?
if the PIC has a spare serial port have a look at XPORT Serial-to-Ethernet Wired Device Servers
I have used these in the past, they work well and are easy to configure

the pic is PIC18F46K42 and I cant program this pic right now

How do you intended to make connections if you can't program the pic?

right now I'm trying to use the Arduino to write registers on the pic (with no success) and when I'm successful at that ill connect the arduino to ethernet

are you attempting to send control commands over serial from an Arduino to the PIC?
then when that works replace the serial with ethernet?
what Arduino are you using?

if that would work it would be great
right now I want to send control commands to my pic (with rs232 port) from my Arduino
I'm using Arduino Mega 2560

you can use a Mega hardware serial port connected to a TTL-RS232 module
image

are you sure the PIC is using RS232 not TTL?

how do you normally communicate with the PIC?, e.g. use a RS232 serial port on a PC?

I used a USB to rs232 cable and I was successful in doing what I wanted in Windows with a program called Modbus poll I was able to connect to the pic and write holding registers on the pic

@morteza20deris

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

I bought the rs232 to TTL converter and I still can't communicate with the pic I've added my Arduino code above

also, the pic is the brain of a led flash controller board

first check the Mega to TTL-RS232 works
run the following code

// Arduino Mega serial1 test

// mega Serial1 pin 18 is Tx
//      Serial1 pin 19 is Rx
// for loopback test connect pin 18 to pin 19

// for RS232 shield connect
// Mega pin 18 TXD to TTL/RS232 Tx
// Mega pin 19 RXD to TTL/RS232 Rx
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
// connect GND pins together and VCC to 5V

void setup() {
  Serial.begin(115200);     // initialise serial monitor port
  Serial1.begin(115200);    // initialise Serial1
  Serial.write("Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19\n");
  Serial.write("RS232: Mega pin 18 TXD to TTL/RS232 Tx and pin 19 RXD to TTL/RS232 Rx\n");
  Serial.write("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  if (Serial1.available()) {  // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  }
  if (Serial.available()) {  // read from Serial outut to Serial1
    char inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

run the loopback tests, e.g. connect RS232 9pin Dtype connector pins 2 and 3
characters entered on mega serial monitor should echo back to the display

when it work connect the TTL-RS232 modules to the PIC RS232
you will probably require a crossover cable or just connect
TTL_RS232 pin 2 to PIC RS232 pin 3
TTL_RS232 pin 3 to PIC RS232 pin 2
TTL_RS232 pin 5 to PIC RS232 pin 5

this is results of a Mega Serial1 connected to TTL-RS232 mudule connected to Quectel EC21 modem

at
OK
at+cgmi
Quectel
OK
at+cgmm
EC21
OK
at+csq
+CSQ: 20,99
OKOK
at+cimi
+CME ERROR: 3

photo

I checked the mega to TTL-RS232 using the following code

#include <string.h>
#include <stdio.h>

#include <ModbusRTUMaster.h>
ModbusRTUMaster modbus(Serial3);

void setup()
{
  
  Serial.begin( 9600 );
  
  modbus.begin(9600);
  modbus.setTimeout(1000);
  
}

void loop()
{ 
  if (Serial.available()) {
    String res = Serial.readString();
    Serial3.println(res);
    Serial.println(res);
    // boolean res2 =modbus.writeSingleHoldingRegister(1, 0, 0);
    // Serial.println(res2);
    
  }
  if (Serial3.available()) {
    String res = Serial3.readString();
    Serial.println(res);
    Serial3.println(res);
  }
  

}

the TTL-rs232 is connected to the PC using a USB-RS232 cable
I have two Arduino IDE instances launched one connected to Arduino USB serial port and the second instance is connected to a USB-RS232 cable port
by sending a message in either of the serial monitors I receive the message on the second serial monitor meaning the TTL-RS232 is sending and receiving data correctly

why are you using SoftwareSerial on a Mega which has three spare hardware serial ports

this test is using the code of post 15 running on a Mega communicating over RS232 with a microcontroller trainer based on a PIC24FJ1024GB610

when RESET is pressed on the PIC24 trainer the Mega serial monitor displays

Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19
RS232: Mega pin 18 TXD to TTL/RS232 Tx and pin 19 RXD to TTL/RS232 Rx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx
UART2 baud rate 57600 set up


PIC24 Mechatronics trainer V1.0 USB V0.3, build date Apr 27 2016
BoardData 26 CANBUSdata 52

CAN enabled normal mode 

PIC24 trainer Version 0.3 (<ESC> key will reset the processor)
   A = read analogue potentiometer
   D = LCD display control 
   F = display motor Fan RPM
   H = Heater control
   L = LED control 
   M = Motor control
   Q = read QEI digital potentiometer
   S = read switches
   T = read temperature of heater
   K = read key pad
   U = read message from second D-type connector
   C = Canus functions
   Z = general test
   ? display this menu

> 

the software serial is leftover from previous code snippets that I used i forgot to delete it
can you send me your Arduino code I'm trying to send Modbus function code 16 to my pic
and i don't know how

it is the code from post 15 - I am reading characters from the PIC RS232 and displaying them - not Modbus

is it even possible to use Modbus Master Library to communicate with the rs232 port?
I was using ModbusRTUMaster because it had support for rs232 in its documentation but it's not working (or I'm not using it right)