--------------------------------------------------------------- * ICPDAS. * Power Meter Visual C++6.0 Demo Program --------------------------------------------------------------- What is it? ----------- A simple program to read/write ICPDAS PM-21XX power meter. This program use CModbus source code to implement modbus protocol. 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 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. 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 LocalModbus.h / LocalModbus.cpp file. 1. Add one parameter in function ReadResponseRTU(). Example: 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 2. In function CLocalModbus::TxRxMessage(), to use ReadResponseRTU like that: ReadResponseRTU(m_abyBuffer,dwNumberOfBytesRead,wLengthResponse) The "wLengthResponse" is a variance already declared in function TxRxMessage(). 3. In function CLocalModbus::ReadResponseRTU(go to the Line 967 at LocalModbus.cpp). Use the following codes to add at Line 945: **************************************** DWORD dwTimeOut = 0; WORD wError; dwTimeOut = ::timeGetTime() + m_lTimeOut; **************************************** 4. In function CLocalModbus::ReadResponseRTU(go to the Line 967 at LocalModbus.cpp). Use the following codes instead of the original codes at Line 968 to 979: **************************************** 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. Compiler the program project again!