Open Serial Port, handler from windows, error, the code works on standard C++



  • Hello, I'm trying to communicate a Qt program with a microcontroller board, (porting a program from C++). The original program works good on console, but moving the code from c++, to qt/c++ compile i get the next warning

    WriteFile(handlerP,bufferPort,strlen(bufferPort),&bytesRW,NULL);
    
    (from the above line)
    warning: C4267: 'argument' : conversion from 'size_t' to 'DWORD', possible loss of data
    
    

    and the next error

    handlerP = CreateFile(serialPort, GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
    
    (from the avobe line)
    error: C2664: 'HANDLE CreateFileW(LPCWSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE)' : cannot convert argument 1 from 'char []' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    

    In theory must work.... ???
    No idea, any help will be appreciated

    thanks in advance, greetings.


  • Lifetime Qt Champion

    Hi,

    Since you are using a serial port to communicate with your board. Why not use QSerialPort ?

    As for your original problem, what type is serialPort ?



  • It is a virtual COM, for USB. I program for windows and i think that QSerial port musst appart compiled and linkend, right?

    Greetings and thanks


  • Lifetime Qt Champion

    If you are using Qt 5, no, it's in. You have to add QT += serialport to your .pro file to use it.



  • Hi yczo,

    the Windows API often has to version of a function.
    One for use with C-like ANSI String (char) and one for wide chars like e.g. utf16 (UNICODE).
    In windows.h the defines are mapped depending on the define for UNICODE.
    Your error is because your project somewhere defines UNICODE so the
    CreateFilefunction maps to CreateFileW.
    This function expects the FileName parameter to be of type wchar_t*, but your
    serialPort is of type char*.
    Either you set your project to be non UNICODE, or you change your code to use wchar_t instead of char.
    Bad practice would be to directly call CreateFileA.



  • Thank you very much for your help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.