A
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.
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.