|
From: arthurclucas on 29 Apr 2008 10:27 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
From: Ben Voigt [C++ MVP] on 29 Apr 2008 18:36 arthurclucas(a)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
|
Pages: 1 Prev: linking to a project that's building an .exe Next: DeviceIOControl Windows Vista |