serial port problem!! pLz help!!

A

Thread Starter

Aditi

Hi grp!

I have a problem while testing the modbus master module against the modbus slave (simulator) over a serial port connection.

The problem is encountered sometimes when my module misses out the fist byte sent from the modbus slave module. i.e. in the reponse received from the slave side, my master module receives function code instead of the station address. hereby missing out the first byte of the station address.

I have done the following settings for the com port communication.

Kindly suggest the possible problem & the solution.

Regards..

Aditi
------------------------------------------------
--------------------------------------------------
HANDLE g_hCOM1=CreateFile( "COM1: ",GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0, NULL);


void SetPortSettings(HANDLE *p_phPortHandle, unsigned short p_usHeaderNum)

{

DCB dcb;
COMMTIMEOUTS lp;

if(!FlushFileBuffers(*p_phPortHandle))
{
LogEvents("Port Write Buffer","Error in flushing port's write buffer",GetLastError(),1);
CleanUp();
exit(0);
}

if(!PurgeComm(*p_phPortHandle,PURGE_RXCLEAR))
{
LogEvents("Port Receive Buffer","Error in flushing port's Receive buffer",GetLastError(),1);
CleanUp();
exit(0);
}

if ( !GetCommState ( *p_phPortHandle , &dcb ) )
{
LogEvents("Port State", "Error in getting current port settings",GetLastError(),1);
CleanUp();
exit(0);
}

dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

if (!SetCommState(*p_phPortHandle, &dcb))
{
LogEvents("Port State", "Error in setting port",GetLastError(),1);
CleanUp();
exit(0);
}

lp.ReadIntervalTimeout = 0;
lp.ReadTotalTimeoutMultiplier=3400;
lp.ReadTotalTimeoutConstant=0;

if(!SetCommTimeouts(*p_phPortHandle,&lp))
{
LogEvents("Port Timeout", "Error in setting port timeout",GetLastError(),1);
CleanUp();
exit(0);
}
else
{
LogEvents("Port Timeout", "Success in setting port timeout",-1,0);
}
}//end of SetPortSettings.
 
A

Automation Linse

Sorry, I don't understand Windows SDK-ish. I just do embedded C/C++ programming. But I'd guess that your FLUSH is occurring too late and your not only flushing out any garbage but the first byte of the slave response.

If this is an RS-485 system where you see your own echo (ie: why you flush), you may need to instead RECEIVE your own echo and carefully discard the bytes. This way you won't be losing the first byte of the response.

best regards
- LynnL, www.digi.com
 
Top