PDA

View Full Version : bluetooth communications (RS-232)



freekill
10th February 2010, 13:01
I have a working code in dev-c++ where I'm transmitting from a host PC to a mobile robot using bluetooth. However, I'm getting errors when I try to integrate the working program into my Qt GUI. I'm getting this particular error:

In member function 'void Bluetooth::initialize()': error: cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'void* CreateFileW(const WCHAR*, DWORD, DWORD, _SECURITY_ATTRIBUTES*, DWORD, DWORD, void*)'

Here's my code:
bluetooth.h


#ifndef BLUETOOTH_H
#define BLUETOOTH_H

#include <windows.h>

class Bluetooth {
public:
Bluetooth();
~Bluetooth();

void initialize();
void transmit(int right, int left);

private:
DWORD bytes_written;
BOOL Status;
HANDLE SerialHandle;
DCB SerialConfigs;
};

#endif


bluetooth.cpp


#include <QDebug>
#include "bluetooth.h"
#include "settings.h"

Bluetooth::Bluetooth() {}

Bluetooth::~Bluetooth() {}

void Bluetooth::initialize() {
qDebug()<<"Initializing BlueTooth...";
Status = 0;
SerialHandle = CreateFile("COM5",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

if(SerialHandle==INVALID_HANDLE_VALUE) {
if(GetLastError()==ERROR_FILE_NOT_FOUND) {
qDebug()<<"E: Serial Port Does Not Exist!";
}
qDebug()<<"An Error Occured!";
}
ZeroMemory(&SerialConfigs, sizeof(SerialConfigs));
SerialConfigs.DCBlength = sizeof(SerialConfigs);
if (!GetCommState(SerialHandle, &SerialConfigs)) {
qDebug()<< "Error Getting State!";
}
SerialConfigs.BaudRate = CBR_9600;
SerialConfigs.ByteSize = 8;
SerialConfigs.StopBits = ONESTOPBIT;
SerialConfigs.Parity = NOPARITY;

if(!SetCommState(SerialHandle, &SerialConfigs)) {
qDebug()<<"Error Setting Serial Port State!";
}
qDebug()<<"Initialization Done";
}

void Bluetooth::transmit(int right, int left) {
WriteFile(SerialHandle,
&right, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);
Status = WriteFile(SerialHandle,
&left,
1,
&bytes_written,
NULL);
if (Status == 0) {
qDebug()<<"Error Writing";
}
}

kuzulis
10th February 2010, 13:42
In member function 'void Bluetooth::initialize()': error: cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'void* CreateFileW(const WCHAR*, DWORD, DWORD, _SECURITY_ATTRIBUTES*, DWORD, DWORD, void*)'

:) use CreateFileA

or ready-made libraries:
QSerialDevice -> http://fireforge.net/snapshots.php?group_id=199
or
QExtSerialPort -> http://code.google.com/p/qextserialport/

kuzulis
10th February 2010, 13:43
In member function 'void Bluetooth::initialize()': error: cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'void* CreateFileW(const WCHAR*, DWORD, DWORD, _SECURITY_ATTRIBUTES*, DWORD, DWORD, void*)'

:) use CreateFileA

or ready-made libraries:
QSerialDevice -> http://fireforge.net/snapshots.php?group_id=199
or
QExtSerialPort -> http://code.google.com/p/qextserialport/

freekill
10th February 2010, 14:41
:) use CreateFileA
I didn't really specify CreateFileW my code just reads CreateFile.

high_flyer
10th February 2010, 15:21
It looks like you have an issue with unicode.
See if somewhere _UNICODE is defined in your project settings.
here is some info on the subject:
http://msdn.microsoft.com/en-us/library/7dzey6h6%28VS.71%29.aspx

Derived from this link you might try using one of the macros specified for example:



SerialHandle = CreateFile(_T("COM5"),
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

freekill
10th February 2010, 23:53
It looks like you have an issue with unicode.
See if somewhere _UNICODE is defined in your project settings.
here is some info on the subject:
http://msdn.microsoft.com/en-us/library/7dzey6h6%28VS.71%29.aspx

Derived from this link you might try using one of the macros specified for example:



SerialHandle = CreateFile(_T("COM5"),
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);



The _T() flags an error: undeclared (first use of this function).:(

kuzulis
11th February 2010, 05:27
I didn't really specify CreateFileW my code just reads CreateFile.


if you look at include the compiler (such as MinGW) you will see the following:


...
WINBASEAPI HANDLE WINAPI CreateFileA (LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
WINBASEAPI HANDLE WINAPI CreateFileW (LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
...
# define CreateFile CreateFileW
...


As you can see under CreateFile default means CreateFileW!!!

One solution to your problem is to use CreateFileA. This is the easiest, or use macros TEXT()

freekill
12th February 2010, 14:48
CreateFileA worked..all the thanks ;)