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