473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bluetooth communication

Hi everyone,

I'm have two iPac Devices and I'm trying send some bytes from one to
another via Bluetooth communication.
I use the CreateFile command to send the data and the ReadFile command
to read the data.
In booth commands I use the appropriate COM port.
When I try to send the bytes, it appears to work (the return is 1).
When I try to read the bytes, it also appears to work (the return is
1).

But it always gives me the return 1 when I read the bytes, even when
nothing is sent. So I don't know if my process is working. Can anyone
help me?

I use the code below to open the COM port:
HANDLE PortOpen(LPTSTR lptszPortName, DWORD* dwError)
{
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
HANDLE hPort;
// Open the serial port.
hPort = CreateFile(
lptszPortName, // Pointer to
the name of the port
GENERIC_READ | GENERIC_WRITE, // Access(read-write)
mode
0, // Share mode
NULL, // Pointer to the
security attribute
OPEN_EXISTING, // How to open the
serial port
0, // Port attributes
NULL // Handle to port
with attribute to copy
);

// If it fails to open the port, return FALSE.
if (INVALID_HANDLE_VALUE == hPort)
{
return INVALID_HANDLE_VALUE;
}

return hPort;
}

And the code below is to write data on the open port:
BOOL PortWrite(HANDLE hPort, BYTE* Bytes, DWORD dwBytesToWrite)
{
BYTE* ucptr;
BYTE* cptr;
DWORD dwBytesWritten;
DWORD dwBytesPacket;
DWORD dwBytesRemain;
int nCounter;
BOOL bRet;
bRet = FALSE;

ucptr = Bytes;
cptr = ucptr;

dwBytesRemain = dwBytesToWrite;
do
{
dwBytesPacket = (dwBytesRemain < PACKET_SIZE) ? dwBytesRemain :
PACKET_SIZE;

nCounter = 1;
do
{
bRet = WriteFile(hPort, ucptr, dwBytesPacket, &dwBytesWritten,
0);
nCounter++;
Sleep(300);
}
while ((bRet == FALSE) && (nCounter < 5));

if (bRet == FALSE)
return FALSE;

dwBytesRemain -= dwBytesWritten;
ucptr += dwBytesWritten;
}
while (dwBytesRemain 0);

return TRUE;
}

The code below I use to read te data:
CString PortRead(HANDLE comPort, DWORD dwBytesToRead)
{
BOOL bResult;
BYTE * totalBytes; //total de bytes
BYTE * readBytes; //bytes parcialmente lidos
DWORD dwBytesRemain; //bytes restantes a serem lidos
DWORD dwBytesRead; //bytes lidos
DWORD dwBytesPacket; //tamanho do pacote

int nCounter;

dwBytesPacket = PACKET_SIZE;
dwBytesRemain = dwBytesToRead;
readBytes = 0;
bResult = ReadFile(comPort, &readBytes, dwBytesToRead, &dwBytesRead,
NULL);

CString s = (LPCTSTR)readBytes;

if (s == "")
return FALSE;

return s;

if (bResult == FALSE)
return FALSE;
}

Thanks,
Arthur
Jun 27 '08 #1
1 4012
ar**********@gmail.com wrote:
Hi everyone,

I'm have two iPac Devices and I'm trying send some bytes from one to
another via Bluetooth communication.
I use the CreateFile command to send the data and the ReadFile command
to read the data.
In booth commands I use the appropriate COM port.
When I try to send the bytes, it appears to work (the return is 1).
When I try to read the bytes, it also appears to work (the return is
1).

But it always gives me the return 1 when I read the bytes, even when
nothing is sent. So I don't know if my process is working. Can anyone
help me?
[snip]
The code below I use to read te data:
CString PortRead(HANDLE comPort, DWORD dwBytesToRead)
{
BOOL bResult;
BYTE * totalBytes; //total de bytes
unused variable, delete
BYTE * readBytes; //bytes parcialmente lidos
DWORD dwBytesRemain; //bytes restantes a serem lidos
no logic for partial reads, this is also useless
DWORD dwBytesRead; //bytes lidos
DWORD dwBytesPacket; //tamanho do pacote
unused variable, delete
>
int nCounter;
unused variable, delete
>
dwBytesPacket = PACKET_SIZE;
dwBytesRemain = dwBytesToRead;
readBytes = 0;
Now your pointer is NULL.
bResult = ReadFile(comPort, &readBytes, dwBytesToRead, &dwBytesRead,
NULL);
You are reading into the pointer variable, not into a buffer. So you can
read at most (sizeof (BYTE*)) bytes, probably 4. Bad idea. You need to
allocate a buffer and pass a pointer to the buffer, not the address of a
pointer.

You need to check bResult here, before using the data.
>
CString s = (LPCTSTR)readBytes;
Since the pointer got overwritten, it is garbage. This should cause your
program to crash.
Once you use a buffer properly, you have another problem because ReadFile
doesn't NUL-terminate the data.
Also you should never use TCHAR with sockets, the data must be either
Unicode or not (on WinCE it is always Unicode).
>
if (s == "")
return FALSE;

return s;

if (bResult == FALSE)
return FALSE;
and this code down here never runs, because you already returned. End
result is you never even checked bResult.

}

Thanks,
Arthur

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steven Landes | last post by:
I'm attempting to use Visual C# 2003 to build a bluetooth enabled program to allow laptops to "talk" to one another. Normally, this wouldn't be an issue, because I'm fairly familiar with...
0
by: Thomas_Germany | last post by:
I would like to develop a smart device bluetooth application for pocket PC 2003. As I know, Pocket PC 2003 includes the microsoft bluetooth stack. My intention is to use the SDK from mircrosoft and...
3
by: Tor Erik Sønvisen | last post by:
Hi I'm making a server-side solution in Python and need to be able to communicate through bluetooth. Is there any bluetooth-packages out there for python? regards tores
3
by: Vikas | last post by:
Hello, Is there a support in .NET framework to work with BlueTooth enabled devices? Thanks. *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in...
0
by: praveeny | last post by:
Hi, I would like to know how to design program that should connect pc to mobile either by serial, bluetooth. And i wanted to know how to read MMS from mobile to PC. I am waiting for reply.
7
by: gayatri1282 | last post by:
How to transfer the file to mobile using dot net 1.1 application using the bluetooth dongle connected to PC.Anybody can help me out from this problem
2
by: lakshmiRam | last post by:
hi iam doing an project where i need to send data in and out between pc and mobile using bluetooth and i red some where that we need intelbth.dll in system for this communication is it true. coz my...
1
by: lakshmiRam | last post by:
hi i have a bluetooth dongle and a program to serach all the devices using bluetooth, but the problem is findFirstDevice() function always returns null so it fails in searching but, i know that...
0
by: lakshmiRam | last post by:
hi i have a code which was supposed to read data from serial com (bluetooth) and save in a file. but when i send a audio file from my mobile to pc the program will not read the port i have ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.