Note that there are some explanatory texts on larger screens.

plurals
  1. POSerial port: Read data problem with loopback, not reading anything
    text
    copied!<p>This problem has been asked many times, I read all the answers but still not able to figure out the problem.</p> <p>I have set up connection with COM1 port Wrote data to COM1 port using WriteFile Now I am trying to read data from the port using ReadFile but it is not reading anything. I have hard ware loopback the 2nd and 3rd of RS232 so that input can be read as output</p> <pre><code>#include &lt;windows.h&gt; #include &lt;stdio.h&gt; # include &lt;tchar.h&gt; int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM3"; hCom = CreateFile( _T("COM3"), //pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access 0, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING FILE_ATTRIBUTE_NORMAL, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); if (hCom == INVALID_HANDLE_VALUE) { // Handle the error. printf ("CreateFile failed with error %d.\n", GetLastError()); } // Build on the current configuration, and skip setting the size // of the input and output buffers with SetupComm. fSuccess = GetCommState(hCom, &amp;dcb); if (!fSuccess) { // Handle the error. printf ("GetCommState failed with error %d.\n", GetLastError()); } dcb.BaudRate = 9600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit fSuccess = SetCommState(hCom, &amp;dcb); if (!fSuccess) { // Handle the error. printf ("SetCommState failed with error %d.\n", GetLastError()); } printf ("Serial port %s successfully reconfigured.\n", pcCommPort); char Buff[] = "Hello"; char Buff2[50] = {}; DWORD dwNumBytesWritten; DWORD dwBytesTransferred; printf("\n\n\n\n\n\n Start writting ! \n"); WriteFile (hCom, // Port handle Buff, // Pointer to the data to write sizeof(Buff), // Number of bytes to write &amp;dwNumBytesWritten, // Pointer to the number of bytes written NULL // Must be NULL for Windows Embedded CE ); printf("\n Bytes Written to the terminal "); for( int j=0; j&lt;dwNumBytesWritten; j++) printf("%c",Buff[j]); printf("\n Byte length %d \n", dwNumBytesWritten); printf("\n\n\n\n\n\n Start reading !\n"); ReadFile (hCom, // Port handle Buff2, // Pointer to data to read dwNumBytesWritten, // Number of bytes to read &amp;dwBytesTransferred, // Pointer to number of bytes read NULL // Must be NULL for Windows Embeddded CE ); for( int j=0; j&lt;dwNumBytesWritten; j++) printf("%c",Buff2[j]); printf("\n BytesRead from the terminal:%d \n",dwBytesTransferred); CloseHandle(hCom); int num; scanf("%d", &amp;num); } </code></pre> <p>The program is not returning any error code, but it continously waits to read data from COM1 port and never receives anything. I am stuck as I can't really decifer where the problem is. Any pointers will be helpful.</p> <p>Thanks,</p> <p>Yogesh</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload