# !!!! README BEFORE COMPILED !!!!

-----------------------------------------------------------------------------------------------------------------------------------

* ICPDAS.
* Power Meter Visual C++6.0 Demo Program
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------
1. What is it?
------------------------------------------------------------------------------------------------------------------------------------
A simple program to read/write ICPDAS power meter.
This program use CModbus source code to implement modbus protocol.
------------------------------------------------------------------------------------------------------------------------------------
2. What is CModbus?
------------------------------------------------------------------------------------------------------------------------------------
CModbus is a free Windows Modbus Master project.
Free to download its Visual C++ 6.0 Source Code, Executables and Documentation.
More information at the CModbus project web site http://mbserver.tripod.com/index.htm
------------------------------------------------------------------------------------------------------------------------------------
3. Before your first compiler this program!
------------------------------------------------------------------------------------------------------------------------------------
(1). Download [ modsrc02.zip ] file from http://mbserver.tripod.com/index.htm
(2). Decompress [ modsrc02.zip ] file, then you will see the [ utilclasses ] folder.
(3). Copy the [ utilclasses ] folder.
(4). Paste to the location that is the same layer with the [ Power Meter program ] folder.
(5). Now you can open [ mfcApp.dsw ] demo program project, and compiler it.
--------------------------------------------------------------------------------------------------------------------------------------
4. Modify CModbus CRC Error! (Optional)
--------------------------------------------------------------------------------------------------------------------------------------
When use the read/write register command of CModbus, it probably get a "CRC ERROR".
Because CModbus receive the return data form a modbus slave device just once.
If modbus master (ex: your programm) can't receive all return datas at a time, "CRC ERROR" will happen!
Modify CModbus "CRC Error" is optional ,not nessary.
Do the changes listed below to avoid "CRC ERROR":
(1). Open the "include\LocalModbus.h" and "scr\LocalModbus.cpp" file.
(2). Add one parameter "fullLengthResponse" in function ReadResponseRTU().
LocalModbus.h:
BOOL ReadResponseRTU(CByteArray& abyResponse, //Modbus Response with crc
DWORD& dwRead, //Real Number of bytes read from serial port
WORD fullLengthResponse); //Total Number of bytes should read from serial port
LocalModbus.cpp:
BOOL CLocalModbus::ReadResponseRTU(CByteArray& abyResponse,//bytes read
DWORD& dwRead, //Real Number of bytes read from serial port
WORD fu llLengthResponse){ //Total Number of bytes should read from serial port
(3). In function CLocalModbus::ReadResponseRTU(go to the Line 932 at LocalModbus.cpp).
Use the following codes in blue color to add at Line 945:
****************************************
iRespMaxSize = abyResponse.GetSize();
dwRead=0;
DWORD dwTimeOut = 0;
WORD wError;
dwTimeOut = ::timeGetTime() + m_lTimeOut;
****************************************
(4). In function CLocalModbus::ReadResponseRTU(go to the Line 932 at LocalModbus.cpp).
Use the following codes instead of the original codes at Line 960 to 990, or add the folloing codes in blue color :
****************************************
if (INITIAL_READ==dwNumberOfBytesRead) {
if (dwRead>DWORD(iRespMaxSize-INITIAL_READ)) {
return FALSE;
}
fullLengthResponse = fullLengthResponse + 2; // CRC has 2 bytes
do{
if( !::ReadFile(m_hComHandle,&abyResponse[dwRead],(iRespMaxSize-INITIAL_READ),&dwNumberOfBytesRead,NULL) )
{ //read one charcter
if (::ClearCommError(m_hComHandle,&dwErrors,&cstStatus))
{ TRACE("ReadFile Errors 0x=%xd",dwErrors); }
else
{ TRACE(" ClearCommError Failed reading"); }
return (FALSE);
}
dwRead = dwRead+dwNumberOfBytesRead;
//if no data to receive,or baudrate/stopbit changed
if( dwTimeOut<::timeGetTime() )
{
wError=(ERR_TIMEOUT);
dwRead=0;
break; // break do-while
}
}while(dwRead < fullLengthResponse);
}
****************************************
(5). In function CLocalModbus::TxRxMessage(), to use ReadResponseRTU like that:
ReadResponseRTU(m_abyBuffer, dwNumberOfBytesRead, wLengthResponse)
The "wLengthResponse" is a variance already declared in function TxRxMessage().
Example:
if (!ReadResponseRTU(m_abyBuffer, dwNumberOfBytesRead, wLengthResponse))
{
TRACE("ReadResponse Failed");
wError=ERR_RD_PORT);
goto TxRxError;
}
(6). Compiler the program project again!