473,884 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(HANDL E 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(comPor t, &readBytes, dwBytesToRead, &dwBytesRead ,
NULL);

CString s = (LPCTSTR)readBy tes;

if (s == "")
return FALSE;

return s;

if (bResult == FALSE)
return FALSE;
}

Thanks,
Arthur
Jun 27 '08 #1
1 4033
ar**********@gm ail.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(comPor t, &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)readBy tes;
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
1455
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 WinSockets, but I cannot figure out how to make the program use my Bluetooth device. I'm assuming I need to use a "using xxx.xxx.xxx" line, or install some SDK. I've found a bunch of .h files for C++ that will allow bluetooth communication, but I can't...
0
2291
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 not the expensive SDK from WIDCOMM to use the bluetooth programming interfaces. But, as I know most pocket PCs with integrated bluetooth chips use the WIDCOMM bluetooth stack. So my questions are as follows: 1) Is it possible to...
3
8048
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
2687
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 USENET...get rewarded for it!
0
2792
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
3752
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
7928
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 app both side mobile and pc are able to discover the devices but when i start sending data from pc to mobile a exception will be caught. if intelbth.dll is needed then where can i find it please provide me some link..
1
2284
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 micrososft will support only a few dongles, mine is broadcom bcm2045a, but is there any alternative than this i came to know that we can use serial communication, but i have a doubt that by using serial com can we able to discover the devices and also...
0
1677
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 configured the bluetooth connection for serial communication (i.e from system stray selecting bluetooth icon and from quick connect selecting serial connection). instead it shows that connection is succesfull for PIM transfer, so do i need to write a...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10769
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10869
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10426
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.