PDA

View Full Version : Serial Port Programming (QT 4.7.4, Windows Seven 64Bit)



pitopito
15th November 2012, 16:32
Hi everybody,

I need some help for Serial Port programming (My environment is :Win7 64Bit, QT Creator 2.4.1, QT 4.7.4 32bit).

1) First of all I'd like to know if I can use Windows API's function suc as CreateFile, WriteFile etc in order to write the code.
I tryed to write some lines, using the "CreateFile" function but an error accurs on building : It is not possible to convert from const char[4] to LPCWSTR.
This is the code :

HANDLE hSerial;
hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hSerial==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//--code
}
//--some other error occurred. Inform user.
}

2) Is there any other simplier way for programming serial ports in QT?
I looked up in the forum and tried downloading QTserialPort but I can't manage to use it...

Anybody could please help a newbe??? I'm going crazy
Thank you in advance

Giovanni

kuzulis
16th November 2012, 09:01
You can use ready made libraries:

QSerialDevice (https://gitorious.org/qserialdevice) - it supports Qt 4.7.4, but it development is stopped.

QtSerialPort (http://qt-project.org/wiki/QtSerialPort) - is a fork of QSerialDevice and is actively developing, but for it needs Qt >= 4.8.0.

I recommend QtSerialPort, though you can try and other libraries...

pitopito
16th November 2012, 21:07
Thank you Kuzulis, I'll try to use QTSerialPort Library with QT 4.8

Besides this, could you give me an hint on how to work out the "step 1" problem I wrote?
Why can't I build that simple code without errors?

Besides the serial port programming methods I need to UNDERSTAND what I am doing and why that error occurs...("It is not possible to convert from const char[4] to LPCWSTR" in Italian "Non e' possibile convertire da const char[4] a LPCWSTR")

thanks to those who can give me an help :)

Giovanni

cincirin
18th November 2012, 20:34
Besides the serial port programming methods I need to UNDERSTAND what I am doing and why that error occurs...
Your project is set to use unicode character set. So first parameter of CreateFile functions is wchar_t* type and not char*. You need to use
CreateFile(L"COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

pitopito
22nd November 2012, 18:08
Thank you, how should I avoid the "undeclared identifier" message when building using L("COM1") ?

Gio

wysota
22nd November 2012, 20:54
Lose the brackets.