Connecting Tech Pros Worldwide Forums | Help | Site Map

problem in serial communication between pc and mobile

Newbie
 
Join Date: Aug 2008
Posts: 9
#1: Sep 4 '08
hi i have written the following code to receive data from mobile using bluetooth through serial communication.

but it is not working
<code>

#include <stdio.h>
#include <conio.h>
#include <windows.h>

#include "stdafx.h"
#include "bluetoothConnection.h"


bool BluetoothConnection::openPort(char *portNum)
{
if (portNum != NULL)
{
hcomm = CreateFile((LPCWSTR)portNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, NULL, 0);
if (hcomm == INVALID_HANDLE_VALUE)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}

bool BluetoothConnection::initPort()
{
dcb.fOutxCtsFlow = false;
dcb.fOutxDsrFlow = false;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fOutX = false;
dcb.fInX = false;
dcb.fRtsControl = RTS_CONTROL_DISABLE;

//time settings
timeouts.ReadIntervalTimeout = 20;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.ReadTotalTimeoutConstant = 100;
if (!SetCommTimeouts(hcomm, &amp;timeouts))
{
return false;
}
else
{
return true;
}
}

void BluetoothConnection::ReadPort()
{
memset(chRead, '\x91', 250);
Sleep(1000L);
ReadFile(hcomm, chRead, 250, &amp;dwRead, NULL);
}

void BluetoothConnection::display()
{
printf("%s\n", chRead);
}
int main()
{
BluetoothConnection *bth1 = new BluetoothConnection();
if (bth1 != NULL)
{
if (bth1-&gt;openPort("COM4"))
{
if (bth1-&gt;initPort())
{
bth1-&gt;ReadPort();
printf("the data from com4 is ....\n");
bth1-&gt;display();
}
else
{
printf("fail in init\n");
}
}
else
{
printf("unable to connect\n");
}

}
else
{
printf("unable to connect failed in initial state\n");
}
Sleep(1000L);
return 0;
}

</code>

its failing in CreateFile function coz its returns 0xffffff..

can this code be used for bluetooth coz i found such code for serial port comm and infrared.


is there any configurations between mobile and pc before running this application if there please provide me the steps..

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,370
#2: Sep 4 '08

re: problem in serial communication between pc and mobile


Quote:

Originally Posted by lakshmiRam

hcomm = CreateFile((LPCWSTR)portNum, GENERIC_READ | GENERIC_WRITE, 0, 0,

portNum is a char*. Typecasting the address as a LPCWSTR does not make it a Unicode string.

You should be using the TCHAR mappings in tchar.h (Google MSDN for this).

If you don't want to do this, then you have to create a Unicode string from your char* string using MultiByteToWideChar. That will give you a WSTR string. Now you can cast the WSTR string to an LPCWSTR and off you go.
Reply