Using the serial com port
-
Hi, I am using the following code to talk to the 'Com1': int CommPort::CreatePort(_Port port , HANDLE *hCom) { DCB dcb; bool fSuccess; char *pcCommPort; CString buffer; buffer.Format("\\\\.\\%s", port.com); pcCommPort = new char[buffer.GetLength() + 1]; for(int i = 0; i <= buffer.GetLength(); i++) *(pcCommPort + i) = buffer.GetAt(i); *hCom = CreateFile(pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); delete[] pcCommPort; if(*hCom == INVALID_HANDLE_VALUE) return -1; fSuccess = GetCommState(*hCom, &dcb); //Portkonfiguration mit GetCommState if(!fSuccess) return -1; dcb.BaudRate = port.dcb.BaudRate;//Konfigurationswerte des Ports dcb.ByteSize = port.dcb.ByteSize; dcb.Parity = port.dcb.Parity; dcb.StopBits = port.dcb.StopBits; dcb.fAbortOnError = true; dcb.fTXContinueOnXoff = true; fSuccess = SetCommState(*hCom, &dcb);//Portkonfiguration mit SetCommState if(!fSuccess) return -1; return 1; } So far so good. But if I try to read the port by using : bool CommPort::ReadPort(HANDLE hCom, CString *ScannText) { char buf[128]; DWORD lpNumberOfBytesRead; bool bReturn; bReturn = ReadFile(hCom, &buf, 128, &lpNumberOfBytesRead, NULL); buf[lpNumberOfBytesRead] = '\0'; *ScannText = buf; return bReturn; } I don't get the information of the port. I am able to read the port only if I connect to COM1 with hyperterminal. After connecting with hyperterminal the program runs without problems. But if I shut down my PC and start it again I will not be able to run my program. I must always start hyperterminal first. Is there anybody who can help?
-
Hi, I am using the following code to talk to the 'Com1': int CommPort::CreatePort(_Port port , HANDLE *hCom) { DCB dcb; bool fSuccess; char *pcCommPort; CString buffer; buffer.Format("\\\\.\\%s", port.com); pcCommPort = new char[buffer.GetLength() + 1]; for(int i = 0; i <= buffer.GetLength(); i++) *(pcCommPort + i) = buffer.GetAt(i); *hCom = CreateFile(pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); delete[] pcCommPort; if(*hCom == INVALID_HANDLE_VALUE) return -1; fSuccess = GetCommState(*hCom, &dcb); //Portkonfiguration mit GetCommState if(!fSuccess) return -1; dcb.BaudRate = port.dcb.BaudRate;//Konfigurationswerte des Ports dcb.ByteSize = port.dcb.ByteSize; dcb.Parity = port.dcb.Parity; dcb.StopBits = port.dcb.StopBits; dcb.fAbortOnError = true; dcb.fTXContinueOnXoff = true; fSuccess = SetCommState(*hCom, &dcb);//Portkonfiguration mit SetCommState if(!fSuccess) return -1; return 1; } So far so good. But if I try to read the port by using : bool CommPort::ReadPort(HANDLE hCom, CString *ScannText) { char buf[128]; DWORD lpNumberOfBytesRead; bool bReturn; bReturn = ReadFile(hCom, &buf, 128, &lpNumberOfBytesRead, NULL); buf[lpNumberOfBytesRead] = '\0'; *ScannText = buf; return bReturn; } I don't get the information of the port. I am able to read the port only if I connect to COM1 with hyperterminal. After connecting with hyperterminal the program runs without problems. But if I shut down my PC and start it again I will not be able to run my program. I must always start hyperterminal first. Is there anybody who can help?
just try like this CreateFile(szPortNumber,// Pointer to the name of the port GENERIC_READ|GENERIC_WRITE , // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute CREATE_ALWAYS,//ow to open the serial port 0, // Port attributes NULL);// Handle to port with attribute to copy anju
-
Hi, I am using the following code to talk to the 'Com1': int CommPort::CreatePort(_Port port , HANDLE *hCom) { DCB dcb; bool fSuccess; char *pcCommPort; CString buffer; buffer.Format("\\\\.\\%s", port.com); pcCommPort = new char[buffer.GetLength() + 1]; for(int i = 0; i <= buffer.GetLength(); i++) *(pcCommPort + i) = buffer.GetAt(i); *hCom = CreateFile(pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); delete[] pcCommPort; if(*hCom == INVALID_HANDLE_VALUE) return -1; fSuccess = GetCommState(*hCom, &dcb); //Portkonfiguration mit GetCommState if(!fSuccess) return -1; dcb.BaudRate = port.dcb.BaudRate;//Konfigurationswerte des Ports dcb.ByteSize = port.dcb.ByteSize; dcb.Parity = port.dcb.Parity; dcb.StopBits = port.dcb.StopBits; dcb.fAbortOnError = true; dcb.fTXContinueOnXoff = true; fSuccess = SetCommState(*hCom, &dcb);//Portkonfiguration mit SetCommState if(!fSuccess) return -1; return 1; } So far so good. But if I try to read the port by using : bool CommPort::ReadPort(HANDLE hCom, CString *ScannText) { char buf[128]; DWORD lpNumberOfBytesRead; bool bReturn; bReturn = ReadFile(hCom, &buf, 128, &lpNumberOfBytesRead, NULL); buf[lpNumberOfBytesRead] = '\0'; *ScannText = buf; return bReturn; } I don't get the information of the port. I am able to read the port only if I connect to COM1 with hyperterminal. After connecting with hyperterminal the program runs without problems. But if I shut down my PC and start it again I will not be able to run my program. I must always start hyperterminal first. Is there anybody who can help?
I had the exact same problem 3 years ago. You can't just read by calling ReadFile. I cann't really remember how I solved it but I will give you a part of an article that I downloaded back then and that luckily for you I still have it. The article is called "Serial Communications in Win32". Reading The ReadFile function issues a read operation. ReadFileEx also issues a read operation, but since it is not available on Windows 95, it is not discussed in this article. Here is a code snippet that details how to issue a read request. Notice that the function calls a function to process the data if the ReadFile returns TRUE. This is the same function called if the operation becomes overlapped. Note the fWaitingOnRead flag that is defined by the code; it indicates whether or not a read operation is overlapped. It is used to prevent the creation of a new read operation if one is outstanding. DWORD dwRead; BOOL fWaitingOnRead = FALSE; OVERLAPPED osReader = {0}; // Create the overlapped event. Must be closed before exiting // to avoid a handle leak. osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osReader.hEvent == NULL) // Error creating overlapped event; abort. if (!fWaitingOnRead) { // Issue read operation. if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) { if (GetLastError() != ERROR_IO_PENDING) // read not delayed? // Error in communications; report it. else fWaitingOnRead = TRUE; } else { // read completed immediately HandleASuccessfulRead(lpBuf, dwRead); } } The second part of the overlapped operation is the detection of its completion. The event handle in the OVERLAPPED structure is passed to the WaitForSingleObject function, which will wait until the object is signaled. Once the event is signaled, the operation is complete. This does not mean that it was completed successfully, just that it was completed. The GetOverlappedResult function reports the result of the operation. If an error occurred, GetOverlappedResult returns FALSE and GetLastError returns the error code. If the operation was completed successfully, GetOverlappedResult will return TRUE. Note GetOverlappedResult can detect completion of the operation, as well as return the operation’s failure status. GetOverlappedResult returns FALSE and GetLastError returns ERROR_IO_INCOMPLETE when the operation is not completed. In addition, GetOverlappedResult can be made to block until the operation completes. This effectively tu