Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TCP Modbus



  • Hello Everyone!!

    Can someone please explain me this example code for TCP Modbus?

    // ModbusTcpClient.cpp : Defines the entry point for the application.
    //

    #include "stdafx.h"
    #include <stdio.h>
    #include "ADSMOD.h"

    void ConnectTcpServerCompletedEventHandler(long lResult, char *i_szIp, void *i_Param);
    void DisconnectTcpServerCompletedEventHandler(long lResult, char *i_szIp, void *i_Param);
    void ModbusWriteCompletedEventHandler(long lResult, void *i_Param);

    void ClientReadCoil_1_32_Handler(long lResult, unsigned char *i_byData, int i_iLen, void *i_Param);
    void ClientReadCoil_65_96_Handler(long lResult, unsigned char *i_byData, int i_iLen, void *i_Param);

    DWORD dwStartTick, dwCurTick, dwWriteTick;
    DWORD dwReadFail, dwWriteFail, dwConnect, dwDisconnect;

    int WINAPI WinMain( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nCmdShow)
    {
    // TODO: Place code here.
    unsigned long ulClientHandle;
    int iAddr = 1;
    int iCount = 0;
    int iCoil = 1, iPoints = 24, iLen = 3;
    unsigned char byData[8] = {0};
    char ch;
    DWORD dwWriteInterval = 1000;
    DWORD dwCount;
    LONG lRet;

    if (MODERR_SUCCESS == MOD_Initialize())
    {
    	if (MODERR_SUCCESS == MOD_AddTcpClientConnect("172.18.3.15",	// remote server IP
    												100,			// scan interval (ms)
    												3000,			// connection timeout (ms)
    												100,			// transaction timeout (ms)
    												ConnectTcpServerCompletedEventHandler,
    												DisconnectTcpServerCompletedEventHandler,
    												NULL,
    												&ulClientHandle))
    	{
    		MOD_AddTcpClientReadTag(ulClientHandle,
    								1,
    								MODBUS_READCOILSTATUS,
    								1,
    								32,
    								ClientReadCoil_1_32_Handler);
    		MOD_AddTcpClientReadTag(ulClientHandle,
    								1,
    								MODBUS_READCOILSTATUS,
    								65,
    								32,
    								ClientReadCoil_65_96_Handler);
    		if (MODERR_SUCCESS == MOD_StartTcpClient())
    		{
    			MOD_SetTcpClientPriority(THREAD_PRIORITY_HIGHEST);
    			printf("MODBUS-TCP client started...\n");
    			dwStartTick = GetTickCount();
    			dwCount = 1;
    			dwReadFail = 0;
    			dwWriteFail = 0;
    			dwConnect = 0;
    			dwDisconnect = 0;
    			printf("Press <ESC> to stop...\n");
    			while ((GetAsyncKeyState(VK_ESCAPE) & 0x8000) == 0)
    			{
    				dwCurTick = GetTickCount();
    				if (dwCurTick >= dwStartTick + dwWriteInterval * dwCount)
    				{
    					dwCount++;
    					lRet = MOD_ForceTcpClientMultiCoils(ulClientHandle, iAddr, iCoil, iPoints, iLen, byData, ModbusWriteCompletedEventHandler);
    					if (ERROR_SUCCESS == lRet)
    					{
    						byData[0]++;
    						byData[1]++;
    						byData[2]++;
    						dwWriteTick = dwCurTick;
    						printf("MOD_ForceTcpClientMultiCoils start (%d) = %d\n", dwCount, dwWriteTick);
    					}
    					else
    					{
    						printf("MOD_ForceTcpClientMultiCoils ret = %d\n", lRet);
    					}
    				}
    				Sleep(1);
    			}
    			MOD_StopTcpClient();
    		}
    		MOD_ReleaseTcpClientResource();
    	}
    	MOD_Terminate();
    }
    printf("Program stopped!\n");
    scanf("%c", &ch);
    printf("Loop count = %d\n", dwCount);
    printf("Read failed = %d\n", dwReadFail);
    printf("Write failed = %d\n", dwWriteFail);
    printf("Connect count = %d\n", dwConnect);
    printf("Disconnect count = %d\n", dwDisconnect);
    printf("Press any key to exit!\n");
    scanf("%c", &ch);
    return 0;
    

    }

    void ConnectTcpServerCompletedEventHandler(long lResult, char *i_szIp, void *i_Param)
    {
    printf("Connect to '%s' result = %d\n", i_szIp, lResult);
    dwConnect++;
    }

    void DisconnectTcpServerCompletedEventHandler(long lResult, char *i_szIp, void *i_Param)
    {
    printf("Disconnect from '%s' result = %d\n", i_szIp, lResult);
    dwDisconnect++;
    }

    void ModbusWriteCompletedEventHandler(long lResult, void *i_Param)
    {
    DWORD gapTick = GetTickCount() - dwWriteTick;
    printf("Coil write result = %d (%d ms)\n", lResult, gapTick);
    if (lResult != MODERR_SUCCESS)
    dwWriteFail++;
    }

    void ClientReadCoil_1_32_Handler(long lResult, unsigned char *i_byData, int i_iLen, void *i_Param)
    {
    int i;

    if (lResult == MODERR_SUCCESS)
    {
    	printf("Read[1~32] = ");
    	for (i=0; i<i_iLen; i++)
    	{
    		printf("%02X ", i_byData[i]);
    	}
    	printf("\n");
    }
    else
    {
    	printf("Failed to read err = %d\n", lResult);
    	dwReadFail++;
    }
    

    }

    void ClientReadCoil_65_96_Handler(long lResult, unsigned char *i_byData, int i_iLen, void *i_Param)
    {
    int i;

    if (lResult == MODERR_SUCCESS)
    {
    	printf("Read[65~96] = ");
    	for (i=0; i<i_iLen; i++)
    	{
    		printf("%02X ", i_byData[i]);
    	}
    	printf("\n");
    }
    else
    {
    	printf("Failed to read err = %d\n", lResult);
    	dwReadFail++;
    }
    

    }

    Header file:

    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //

    #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
    #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

    // Windows Header Files:
    #include <windows.h>

    // Local Header Files

    // TODO: reference additional headers your program requires here

    //{{AFX_INSERT_LOCATION}}
    // Microsoft eMbedded Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

    Header File:
    #ifndef NEWRES_H
    #define NEWRES_H

    #if !defined(UNDER_CE)
    #define UNDER_CE _WIN32_WCE
    #endif

    #if defined(_WIN32_WCE)
    #if !defined(WCEOLE_ENABLE_DIALOGEX)
    #define DIALOGEX DIALOG DISCARDABLE
    #endif
    #include <commctrl.h>
    #define SHMENUBAR RCDATA
    #if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
    #include <aygshell.h>
    #else
    #define I_IMAGENONE (-2)
    #define NOMENU 0xFFFF
    #define IDS_SHNEW 1

    	#define IDM_SHAREDNEW        10
    	#define IDM_SHAREDNEWDEFAULT 11
    #endif
    

    #endif // _WIN32_WCE

    #ifdef RC_INVOKED
    #ifndef _INC_WINDOWS
    #define _INC_WINDOWS
    #include "winuser.h" // extract from windows header
    #endif
    #endif

    #ifdef IDC_STATIC
    #undef IDC_STATIC
    #endif
    #define IDC_STATIC (-1)

    #endif //NEWRES_H

    Waiting for positive responses

    TIA


  • Lifetime Qt Champion

    Hi,

    In what way is it related to Qt ?

    Note that there's is support for Modbus in Qt.


Log in to reply