# Modbus\_serial\_master.C

**URL:** <https://forums.digi.com/t/modbus-serial-master-c/10851>\
**Category:** Rabbit\
**Created:** [April 29, 2011, 10:09am UTC](https://forums.digi.com/t/modbus-serial-master-c/10851 "2011-04-29T10:09:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Termi](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@Termi](https://forums.digi.com/u/Termi)\
**Post date:** [April 29, 2011, 10:09am UTC](https://forums.digi.com/t/modbus-serial-master-c/10851/1 "2011-04-29T10:09:01Z")

</div>

Hello everyone!  
Excuse me to post too many messages.

I have a RCM3900 is connected to a slave via RS485. I want to read the registry via the Modbus protocol. So I used the example modbus\_serial\_master. c that I adapted. But I can not play my records. I could see that in the example there modbus\_serial\_master.c different functions that are included in RS232.lib serXrdUsed like () or serCread ().

Should I replace them because I have a RS485 connection?  
Help me please, thank you!

My program:

#define Mesure  
#define MODBUS\_DEBUG\_PRINT 0 // define to 1 to print transactions  
#define CINBUFSIZE 15  
#define COUTBUFSIZE 15  
#define MODBUS\_PORT C  
#define \_RS485 485  
#define SERIAL\_PORT\_MODE \_RS485  
#use modbus\_master.lib

void brdInit();  
int MBM\_Send\_ADU ( char \*Packet, int ByteCount );  
int MBM\_Rcv\_Resp ( char \* Packet );  
void serInit ( void );  
void ser485Tx( void ) ;  
void ser485Rx( void );

main ()  
{long i, RegsValue[50];  
serInit();  
i = MBM\_ReadRegs (005, &RegsValue[0], 4096, 2 ); // read reg 0  
printf("%d, %d,  
", RegsValue[0], i);  
while(1);  
}

void serInit ( void )  
{  
serCopen (9600 ); // open the serial port  
}  
/\* START FUNCTION DESCRIPTION \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
MBM\_Send\_ADU  
END DESCRIPTION \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

void brdInit()  
{ WrPortI(PCFR, &PCFRShadow, PCFRShadow&0xEA); //clear bit 4,2,0 to normal function  
//bits 5,3,1 normally inputs  
WrPortI(PCDR, &PCDRShadow, PCDRShadow|0x15); //set bits 4,2,0 high  
WrPortI(PDCR, &PDCRShadow, 0x00); //clear all bits to pclk/2  
WrPortI(PDFR, &PDFRShadow, 0x00); //clear all bits to normal function  
WrPortI(PDDCR, &PDDCRShadow, 0x00); //clear all bits to drive high and low  
WrPortI(PDDR, &PDDRShadow, 0x0b); //set bits 3,1,0 high  
WrPortI(PDDDR, &PDDDRShadow, 0x8b); //set bits 7,3,1,0 to output, rest inputs  
}

void ser485Tx( void )  
{  
#asm  
set 7,a ;set bit 7  
ioi ld (PDDR),a ;set PD7 high  
#endasm  
}

void ser485Rx( void )  
{  
#asm  
res 7,a ;clear bit 7  
ioi ld (PDDR),a ;set PF5 low  
#endasm  
}

int MBM\_Send\_ADU ( char \*Packet, int ByteCount )  
{ auto unsigned CRCvalue;  
auto unsigned long Endtime;  
int i;

// insert CRC  
CRCvalue = MODBUS\_CRC ( Packet, ByteCount );  
Packet[ByteCount+1] = CRCvalue; // store low byte  
Packet[ByteCount] = CRCvalue\>\>8; // store high byte  
ByteCount+=2; // adjust for CRC

#ifdef MODBUS\_DEBUG\_PRINT & 1  
printf ( “Tx:” );  
for ( i=0; i
