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 appreciatedthanks in advance, greetings.
-
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 ?
-
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).
Inwindows.h
the defines are mapped depending on the define forUNICODE
.
Your error is because your project somewhere definesUNICODE
so the
CreateFile
function maps toCreateFileW
.
This function expects theFileName
parameter to be of typewchar_t*
, but your
serialPort
is of typechar*
.
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 callCreateFileA
.