
Chapter 4
Document Version V3.1.0 (2020-08-18) 49
/* Establish the connection over TCP */
ctx = modbus_new_tcp(MODBUS_SERVER_IP,
MODBUS_SERVER_PORT);
/* Create and start the backend thread */
hThread = (HANDLE)_beginthreadex(NULL, 0, &HeartBeat, ctx, 0,
NULL);
/* You can implement your own functions here... */
/* Free the connection and disable the thread */
modbus_close(ctx);
modbus_free(ctx);
CloseHandle(hThread);
return 0;
}
When adding the "heartbeat package" function, please pay attention to
the following points:
⚫
When you need to enable multithreading for management, ensure the
safety of multithreading. When accessing the same channel, use
mutexes, locks, barriers, etc. for protection.
⚫
Use the thread functions related to the platform to ensure correct
opening and closing of threads.