How to change Modbus from serial to RS485?

Hi, I am using Modbus to comunicate with a Nano through the serial usb but it only works when the client is in highlight execution (which means it is running slowly). I can't believe the Nano is too slow so I think it is the serial connection. I have tried running at higher baud rates as suggested previously but that doesnt help. Another way round is to use the serial through the Rx and TX pins.

In the further information on CMBullinar modbusRTU slave library it mentions that there is a dePin function for use with RS485 but I cannot understand wtfhat it says. and there is no descritpion of the hardware connections. I have tried implementing the dePin code but the compile produces errors, similarly to what happens if I try to use recent code posted by others using this library. Does anyone know how to used this library with RS485? The description appears not to match the library! heres the library link.

https://github.com/CMB27/ModbusRTUSlave?tab=readme-ov-file#description
Here is my code

#include <ModbusRTUSlave.h> // ModbusRTUSlave by CM Bullinar
ModbusRTUSlave modbus(Serial, dePin);// recomended by description
//ModbusRTUSlave modbus_slave(Serial); working version
const uint8_t slaveID = 1;
const uint32_t baud = 9600;
const uint8_t dePin = 13;


uint16_t holdingRegisters[20] = {0};

int sensor;
void setup() {
  // put your setup code here, to run once:
modbus_slave.configureHoldingRegisters(holdingRegisters, 20);
modbus_slave.begin(slaveID, baud, SERIAL_8N1);
}

void loop() {
  // put your main code here, to run repeatedly:
sensor = analogRead(A0);
holdingRegisters[2] = 123456;
modbus_slave.poll();

}
ERROR MESSAGES
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino:2:31: error: 'dePin' was not declared in this scope
 ModbusRTUSlave modbus(Serial, dePin);// recomended by description
                               ^~~~~
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino:2:31: note: suggested alternative: 'rewind'
 ModbusRTUSlave modbus(Serial, dePin);// recomended by description
                               ^~~~~
                               rewind
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino: In function 'void setup()':
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino:14:1: error: 'modbus_slave' was not declared in this scope
 modbus_slave.configureHoldingRegisters(holdingRegisters, 20);
 ^~~~~~~~~~~~
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino:14:1: note: suggested alternative: 'ModbusRTUSlave'
 modbus_slave.configureHoldingRegisters(holdingRegisters, 20);
 ^~~~~~~~~~~~
 ModbusRTUSlave
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino: In function 'void loop()':
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino:22:1: error: 'modbus_slave' was not declared in this scope
 modbus_slave.poll();
 ^~~~~~~~~~~~
C:\Users\Pete\Documents\Arduino\modbusMulinar\modbusMulinar.ino:22:1: note: suggested alternative: 'ModbusRTUSlave'
 modbus_slave.poll();
 ^~~~~~~~~~~~
 ModbusRTUSlave

exit status 1

Compilation error: 'dePin' was not declared in this scope

You are trying to use dePin before you declare it. Move those constants to the top of the file

Depends on your ttl-rs485 converter. What you have?
Also your sketch is quite strange with 20 registers and analogRead in loop etc...

EDIT:
You already have post for this subject, you should not open another!

I have an easysync usb rs485 converter which I have used extensively with other modbus server devices. sorry about the new topic I suppose they are similar.

They are not similar, your subject is same.

Ok, should I link them now?
It seems that to make the dePin variable compatible with the other modbus variables I had to change the other variables from modbus_slave.something to modbus.something. But i still don't know how to implement the dePin, what it does or if it will help.

Yes.

And how you made that connect to arduino??
Many converters have automatic direction control. Does yours have DE and RE pins?

Its an rs485 bus just A and B wires connect to the tx and rx. I dont think it has ADC. with other devices I just switch the wires to get it working. maybe I should try going back to defining input pins?

Then it has DE and RE pins....

Your code is a mess..
Did you ever try the one I gave you on your other post?

Yes, thanks I tried at 4800 and 19200 baud it tends to time out or get a framing error message I dont believe those messages completely though.

#include <SoftwareSerial.h>
#include <ModbusRTUSlave.h> // ModbusRTUSlave by CM Bullinar


const uint8_t slaveID = 1;
const uint32_t baud = 4800;
// Define software serial pins for RX and TX
#define RX_PIN 10
#define TX_PIN 11

// Create a software serial port
SoftwareSerial mySerial(RX_PIN, TX_PIN);
ModbusRTUSlave modbus_slave(mySerial);
uint16_t holdingRegisters[2];

int sensor;
void setup() {
  // put your setup code here, to run once:
modbus_slave.configureHoldingRegisters(holdingRegisters, 2);
modbus_slave.begin(slaveID, baud, SERIAL_8N1);
}

void loop() {
  // put your main code here, to run repeatedly:

holdingRegisters[0] = 333;
holdingRegisters[1] = 777;
modbus_slave.poll();
delay(10);
}

Slave address 1, holding registers 0 and 1 (40001 and 40002), nothing?

Hold on...! Are you trying to read arduino pins directly without ttl-rs485 module?
tx and rx connected to A and B pins of your Usb-rs485??
It can't work!

Why? There is a guy who did it with RS232

https://www.youtube.com/watch?v=dNlaE5UcSRk

I don't care what some youtube guy do. Good luck.
RS-485 is half-duplex.
Next time show your hardware setup in first post...

No he did not. He used a "MAX232 RS232 to TTL converter to Connect to the RX and TX serial pins of the Arduino"