0

I have a device with an rs485 port from which I would like to read the ph value. Below is the connection with the RS485 adapter.

enter image description here

I tried to read the data with this code but doesn't work:

#include SoftwareSerial.h

#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin
#define SSerialTxControl 3   //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW
#define Pin13LED         13

SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
byte byteSend;
byte request[] = {0, 0x03, 0, 0, 0, 0x01, 0x85, 0xDB};

void setup()   
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("SerialRemote");  // Can be ignored

  pinMode(Pin13LED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  

  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver


  RS485Serial.begin(9600);   
  delay(2000);
   RS485Serial.write(request, sizeof(request));

   digitalWrite(Pin13LED, LOW);  
   delay(10);
   digitalWrite(SSerialTxControl, RS485Receive);

}
void loop()   
{

  if (RS485Serial.available()) 
  {
    byteSend = RS485Serial.read();   // Read the byte
    Serial.print(byteSend, HEX);
    Serial.print(" ");
  }
}

Could you help me? Thanks

  • You aren't providing power to your RS485 interface? – Majenko May 3 at 12:29
  • yes, i provided power from arduino +5v gng (missing in the image sorry) – faustolev May 3 at 12:46
  • Can you expand on "doesn't work"? Also do you have a datasheet for your pH sensor? – Majenko May 3 at 13:14
  • explain the request's bytes please. – Juraj May 3 at 13:22
  • it is the same case of:arduino.stackexchange.com/questions/50645/… – faustolev May 3 at 13:27
0

I can't say if this is your problem or not but I don't like this bit of code:

   RS485Serial.write(request, sizeof(request));

   digitalWrite(Pin13LED, LOW);  
   delay(10);
   digitalWrite(SSerialTxControl, RS485Receive);

You're sending the request then waiting 10ms and switching to receive mode. That might be enough of a delay, but it's hard to say (best case, 960Bps at 9600 baud gives about 1ms per byte, which is 8ms for the request, but with software serial it may not be that simple).

Instead you should be using the flush method of the Stream class to wait for the data to be transmitted:

   RS485Serial.write(request, sizeof(request));

   digitalWrite(Pin13LED, LOW);  
   RS485Serial.flush();
   digitalWrite(SSerialTxControl, RS485Receive);
| improve this answer | |
  • same result: serial monitor show "FF" every loop. – faustolev May 3 at 16:36
  • Probably needs to call digitalWrite(SSerialTxControl, RS485Transmit); to enable the TX buffer, before writing the data to the port. – SoreDakeNoKoto May 3 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.