How to read rs485 data through MC60

Hi Experts,
Currently, i am working with MC60 open CPU, now i am trying to integrate the MC60 module with Modbus enabled meter i have a max485 ic based design which is used to reading the rs-485 data through the microcontroller
i was testing my max485 board and my Modbus slave with Arduino that is working fine but if i am connected with my MC60 module i am trying to read the data from my Modbus device that is return nothing, my doubt is Arduino have a one serial function serial.write(value); that is i am used to sending the bytes to my Modbus, if there is any available function like this for MC60 kindly share with me

1 Like

Hi i have attached my Arduino and MC60 code for your reference kindly help to solve this problem

Arduino Code

#include <SoftwareSerial.h>
byte byteSend;

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.println(“FLOW READING…”);
}
void loop()
{

while (Serial1.available())
{
byteSend = Serial1.read(); // Read the byte
Serial.print(“RDATA:”);
Serial.println(byteSend, HEX);
}
delay(1000);
byte request[] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3};
//[01h] [03h] [00h] [75h] [00h] [04h] [55h] [D3h]
//[01h] [03h] [00h] [6Dh] [00h] [04h] [D5h] [D4h]
Serial1.write(request, sizeof(request));
}

MC60 Code

#ifdef UART_TEST

#include <string.h>
#include “ql_type.h”
#include “ql_trace.h”
#include “ql_timer.h”
#include “ql_uart.h”
#include “ql_stdlib.h”
#include “ql_error.h”

#define SERIAL_PORT UART_PORT1
#define DEBUG_PORT UART_PORT2

#define dataBuffer_lenght 2048
static char dataBuffer[dataBuffer_lenght];

#define SerialPrint(stringData,…) {
Ql_memset(dataBuffer, 0, dataBuffer_lenght);
Ql_sprintf(dataBuffer,stringData,##VA_ARGS);
Ql_UART_Write((Enum_SerialPort)(SERIAL_PORT), (char*)(dataBuffer), Ql_strlen((const char *)(dataBuffer)));
}

#define RS485Print(stringData,…){
Ql_memset(dataBuffer, 0, dataBuffer_lenght);
Ql_sprintf(dataBuffer,stringData,##VA_ARGS);
Ql_Debug_Trace(dataBuffer);
}

#define RS485_TIMER TIMER_ID_USER_START
#define RS485_TIMER_PERIOD 1000
static void READ_RS485(u32 timerId, void* param);

static void CallBack_UART_Hdlr(Enum_SerialPort port, Enum_UARTEventType msg, bool level, void* customizedPara);
static s32 ReadSerialPort(Enum_SerialPort port, /[out]/u8* pBuffer, /[in]/u32 bufLen);
u8 m_Read_Buffer[2048];
int count = 0;

void proc_main_task(s32 taskId)
{
s32 ret;
ST_MSG msg;

Ql_UART_Register(SERIAL_PORT, CallBack_UART_Hdlr, NULL);
Ql_UART_Open(SERIAL_PORT, 115200, FC_NONE);

Ql_UART_Register(DEBUG_PORT, CallBack_UART_Hdlr, NULL);
Ql_UART_Open(DEBUG_PORT, 9600, FC_NONE);

ret= Ql_Timer_Register(RS485_TIMER, READ_RS485, NULL);
Ql_Timer_Start(RS485_TIMER, RS485_TIMER_PERIOD, TRUE);

while (TRUE)
{	
	Ql_OS_GetMessage(&msg);

    switch(msg.message)
    {
        case MSG_ID_RIL_READY:
            Ql_RIL_Initialize();
            SerialPrint("<-- UART_READ_WRITE -->\r\n");
        break;

        case MSG_ID_USER_START:
            break;

        default:
            break;
    }
}

}

static void READ_RS485(u32 timerId, void* param)
{
/* 00000001
00000011
00000000
01110101
00000000
00000100
01010101
11010011*/

u8 request[8] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3};
int i = 0;
for (i = 0; i < sizeof(request); i++)
{
   // SerialPrint("DATA=\r\n");
    RS485Print("%c",request[i]);
}

}

static s32 ReadSerialPort(Enum_SerialPort port, /[out]/u8* pBuffer, /[in]/u32 bufLen)
{
s32 rdLen = 0;
s32 rdTotalLen = 0;
if (NULL == pBuffer || 0 == bufLen)
{
return -1;
}
Ql_memset(pBuffer, 0x0, bufLen);
while (1)
{
rdLen = Ql_UART_Read(port, pBuffer + rdTotalLen, bufLen - rdTotalLen);
if (rdLen <= 0) // All data is read out, or Serial Port Error!
{
break;
}
rdTotalLen += rdLen;
// Continue to read…
}
if (rdLen < 0) // Serial Port Error!
{
RS485Print("<–Fail to read from port[%d]–>\r\n", port);
return -99;
}
return rdTotalLen;
}

static void CallBack_UART_Hdlr(Enum_SerialPort port, Enum_UARTEventType msg, bool level, void* customizedPara)
{
switch (msg)
{
case EVENT_UART_READY_TO_READ:
{
if(DEBUG_PORT == port || SERIAL_PORT == port)
{
Ql_memset(m_Read_Buffer, 0x0, sizeof(m_Read_Buffer));
s32 totalBytes = ReadSerialPort(port, m_Read_Buffer, sizeof(m_Read_Buffer));
SerialPrint(“Received Data=?%s”,m_Read_Buffer);
break;
}
}
}
}
#endif

Hi,
you can use s32 Ql_UART_Write(Enum_SerialPort port, u8* data, u32 writeLen ); function to write data on UART.
for example
u8 request[] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3};
Ql_UART_Write(UART_PORT1,request,8);
where 8 is the total byte length.

Regards
Rahul

Hi Rahul,
Thanks for the update, then i will let you know the result

Hi @rahul.mahakalkar
Ql_UART_Write(UART_PORT1,request,8);
is working and i get the below result
read from port[F03D2B34]
RS485 Data —>len(9)<—>F03D2B34

here read length is 9 but the shown data length is 8, so i do some changes in the serial read line

if(port==RS485_PORT)
{
Ql_memset(RS485_Buffer, 0x0, sizeof(RS485_Buffer));
BufLen = ReadSerialPort(RS485_PORT, RS485_Buffer, 10);
//Ql_UART_Read(RS485_PORT, RS485_Buffer, 10);
// SerialPrint(“RS485 Data <—>%X\r\n”,RS485_Buffer);

            if(BufLen>0)
            {
                SerialPrint("RS485 Data --->len(%d)<--->%X\r\n",BufLen, RS485_Buffer);
                break;
            }
        }

i get the same output, this is the error result because the value does not change i am getting the same value all the time, i don’t know why kindly help to solve this issue

regards
kannannatesh

Hi
please mail to support@quectel.com.

@rahul.mahakalkar
thanks for the reply i will send the mail

regards
kannannatesh