HTCC-AB02S RX2 issue

Hi,
I’ve been struggling with a strange problem for several days now. I have a soil moisture sensor that sends data through a modbus (RS485 <> TTL) interface. I use a HTCC-AB02s because it has a second serial port. The process is simple, I send a request and get the data from some registers. But whatever I try I only get 7 bytes of data back while I see on my logic analyzer that 11 bytes are received. Does anyone have any suggestions?

    static void prepareTxFrame( uint8_t port )
    {
    	 digitalWrite(GPIO7 , HIGH); // Power up the modbus interface and sensor
       delay(500);

       
       //Send request to modbus sensor
      uint8_t request[] ={0x01,0x03,0x00,0x00,0x00,0x03,0x05,0xCB} ; // Sensor request (first 3 registers)

      for (int n=0; n<8 ; n++)
      {
        Serial1.print(char(request[n]));
      }

      delay(300);

       //recieve data from modbus sensor
      uint8_t serialBuffer[11] = {0} ;
      int i=0;
      
     while (Serial1.available()) 
     {
         serialBuffer[i] =  (char) Serial1.read();
         i++;   
     }
      Serial.println(i-1);
      
        appDataSize = 6;
        appData[0] = serialBuffer[3]; //Temp msb
        appData[1] = serialBuffer[4]; //Temp lsb
        appData[2] = serialBuffer[5]; // moister msb
        appData[3] = serialBuffer[6]; // moister lsb
        appData[4] = serialBuffer[7]; // EC msb
        appData[5] = serialBuffer[8]; // EC lsb

    digitalWrite(GPIO7, LOW); // Power down the modbus interface and sensor
    }

hi,

we can’t find any problem from your code and your describe。

could you show more details(Connection of pins…ect.)?

Hi jasonXu,

The RS485 interface is connected to pin 29 (RX2) and pin 30 (TX2). Baudrate is set to 9600-8-1-N.

hi,

this buffer length is 8.

we have updated the code:


%E5%9B%BE%E7%89%87

Super, thanks…problem solved.

Do you think it would be possible to make this on HTCC-AB01, using the serial connected to USB chip?
Thanks!

sorry, i can’t understand your mean well. can you explain more?

The Rx and Tx pins aren’t connected to cp2102?
I meant if it’s possible to used even if the pins are connected to that chip.
Thanks

Here the github link fails.

Can you please fix this link, or (re) provide your solution?

Thanks

Here is a working solution,

#include <Arduino.h>
#include “Wire.h”
#define TIMEOUT 10 //time in ms for rs485 timeout

// To write and read from a soil moisture sensor RS485 using a RS485TTL module ‘k485’

byte buf[8]={0x01,0x03,0x00,0x01,0x00,0x04,0x15,0xC9};
uint8_t serialBuffer[12];
int size = 0;

void setup() {
Serial.begin(9600); // for debugging, do not use between writing to the device and listening for reply, or you will miss reply
Serial1.begin(9600); // pins rx2 - 36,tx2 - 37
pinMode(23,OUTPUT); // this controls the power to my peripherals using the df robot solar power mangement
}

void loop() {
digitalWrite(23, HIGH);
delay(500); // let the power arrive at the RS485 interface

Serial1.write(buf,8);
size = Serial1.read(serialBuffer,TIMEOUT);

//Serial.println("");  !!  This delayed the listening for the return from the sensor, causing failure

if(size)
{
for (int j; j<17;j++){Serial.printf(" gets %x ",serialBuffer[j]);}
Serial.printf(“size = %d “,size);
Serial.println(””);

int tempC = ((serialBuffer[3] << 8) | serialBuffer[4])/10;
int sMoist = ((serialBuffer[5] << 8) | serialBuffer[6])/10;
Serial.print("temp: ");
Serial.print(tempC);
Serial.print("  Soil_Moisture: ");
Serial.println(sMoist);
for (int n=0; n<9 ; n++)
  {
    Serial.printf("%x",char(serialBuffer[n]));
    Serial.print(", ");
    Serial.println();
  }
    appDataSize = 4;
    appData[0] = serialBuffer[3]; //Temp msb
    appData[1] = serialBuffer[4]; //Temp lsb
    appData[2] = serialBuffer[5]; // moister msb
    appData[3] = serialBuffer[6]; // moister lsb

}
Serial.println(“finishing loop”);
digitalWrite(23,LOW); // turn power off to periph
delay(2000);
}

Thanks, this site helped a lot.