Compile Libmodbus for Windows CE

123 views
Skip to first unread message

Amine

unread,
Apr 15, 2021, 8:11:00 AM4/15/21
to libmodbus
Hi everybody,

I tried compiling libmodbus for Windows CE (through cross compilation under VS 2008) and I had link errors related to Winsock or functions like CreateFileA.

I only need the RTU part but I couldn't compile without the TCP source files.

Has anyone succeeded to compile libmodbus for such OS ?

Thanks.

Amine

unread,
Apr 15, 2021, 10:37:32 AM4/15/21
to libmodbus
I succeeded in modifying Libmodbus to be compilable for Windows CE (ARM). In fact, I compiled it statically with a program, I will try to create a clean DLL project (if I succeed) and I will share it on github.

hints for the people in a hurry :

* stdint.h => there's already a comment related to wchar.h, just change "C" to "C++"
* Search and Change CreateFileA to CreateFile, on MS documentation, they have the same signature
* In modbus.h, put : 
#ifdef __cplusplus
extern "C" {
#endif
extern int errno;
#ifdef __cplusplus
}
#endif

and in modbus.cpp define a global int errno variable !

* To fix winsock link errors, just add this : 
#pragma comment(lib, "ws2.lib")
under the winsock2 include statement

* strerror and perror are missing ? no problem ! add these two macros somewhere under modbus.h
#define strerror(n) _T("file error")
#define perror(n) _T("file error")

if there's an issue linking wmain like me, in VS => Link edition -> Advanced -> Entry Point -> mainACRTStartup

Best regards.

Amine

unread,
Apr 16, 2021, 5:37:55 AM4/16/21
to libmodbus
As my SDK (Trizeps VII ARM V7) enforces me to use UNICODE, I need to use this kind of code to convert the port name to a wide string version (in _modbus_rtu_connect)

     { // needed for VS2008 (C89)
    TCHAR wszDest[32];

        win32_ser_init(&ctx_rtu->w_ser);

        MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, ctx_rtu->device, -1, wszDest, 32);

        /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal
         * number */
        ctx_rtu->w_ser.fd = CreateFile(wszDest,
                                       GENERIC_READ | GENERIC_WRITE,
                                       0,
                                       NULL,
                                       OPEN_EXISTING,
                                       0,
                                       NULL);
}

Otherwise, as I'm using RS485, I also need to add this in _modbus_rtu_connect :

/* Specifies that the RTS line will be high if bytes are available for transmission. After all buffered bytes have been sent, the RTS line will be low. */
dcb.fRtsControl = RTS_CONTROL_TOGGLE;

And it works wonderfully !

Hope that helps for the people trying to use libmodbus under WinCE

Amine

unread,
Apr 16, 2021, 9:12:15 AM4/16/21
to libmodbus
Reply all
Reply to author
Forward
0 new messages