ReadFile () reads Junk characters when tried to read from COM1 Port
-
Hi , I have created sample application which opens the serial COM Port and tried to write and Read the data. I have used CreateFile () method to open the COM1.
hCom = CreateFile (pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0,NULL );
Then I set the COM state
GetCommState( hCom, &lpCC.dcb); lpCC.dcb.BaudRate = CBR\_9600; // baud rate lpCC.dcb.StopBits = ONESTOPBIT; // stop bit lpCC.dcb.ByteSize = 8; // data size, xmit and rcv lpCC.dcb.Parity = NOPARITY; // parity bit lpCC.dcb.fBinary = TRUE ; lpCC.dcb.fParity = TRUE ; SetCommState( hCom, &lpCC.dcb );
I am trying to write the data using
WriteFile (hCom,(LPCVOID)ptr,20,&byteswritten,NULL);
and trying to read the data using
ReadFile (hCom, (LPVOID)ptr, 20, &dwBytesTransferred, 0);
ptr is pointer to the BYTE array. The above code of "ReadFiles() returns true and Reads Junk characters. I have written exact 20 characters through WriteFile() and it returned TRUE . I am not able to figure out the exact problem . Please provide any input in this regards. Thanks
-
Hi , I have created sample application which opens the serial COM Port and tried to write and Read the data. I have used CreateFile () method to open the COM1.
hCom = CreateFile (pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0,NULL );
Then I set the COM state
GetCommState( hCom, &lpCC.dcb); lpCC.dcb.BaudRate = CBR\_9600; // baud rate lpCC.dcb.StopBits = ONESTOPBIT; // stop bit lpCC.dcb.ByteSize = 8; // data size, xmit and rcv lpCC.dcb.Parity = NOPARITY; // parity bit lpCC.dcb.fBinary = TRUE ; lpCC.dcb.fParity = TRUE ; SetCommState( hCom, &lpCC.dcb );
I am trying to write the data using
WriteFile (hCom,(LPCVOID)ptr,20,&byteswritten,NULL);
and trying to read the data using
ReadFile (hCom, (LPVOID)ptr, 20, &dwBytesTransferred, 0);
ptr is pointer to the BYTE array. The above code of "ReadFiles() returns true and Reads Junk characters. I have written exact 20 characters through WriteFile() and it returned TRUE . I am not able to figure out the exact problem . Please provide any input in this regards. Thanks
pandit84 wrote:
...and Reads Junk characters.
How are you verifying this? Do you know that data is available to read before calling
ReadFile()
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
pandit84 wrote:
...and Reads Junk characters.
How are you verifying this? Do you know that data is available to read before calling
ReadFile()
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
And that looks like?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
And that looks like?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
I doubt very much the COM redirector understands how to loopback i.e. You can't read and write on the same COM port on the same computer, and if you have a machine with two COM ports, you still need a null modem cable between them
modified on Wednesday, May 18, 2011 4:31 AM
-
That's what your
printf()
statement looks like?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
That's what your
printf()
statement looks like?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Ok, so are you using communications events?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Ok, so are you using communications events?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Hi , I have created sample application which opens the serial COM Port and tried to write and Read the data. I have used CreateFile () method to open the COM1.
hCom = CreateFile (pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0,NULL );
Then I set the COM state
GetCommState( hCom, &lpCC.dcb); lpCC.dcb.BaudRate = CBR\_9600; // baud rate lpCC.dcb.StopBits = ONESTOPBIT; // stop bit lpCC.dcb.ByteSize = 8; // data size, xmit and rcv lpCC.dcb.Parity = NOPARITY; // parity bit lpCC.dcb.fBinary = TRUE ; lpCC.dcb.fParity = TRUE ; SetCommState( hCom, &lpCC.dcb );
I am trying to write the data using
WriteFile (hCom,(LPCVOID)ptr,20,&byteswritten,NULL);
and trying to read the data using
ReadFile (hCom, (LPVOID)ptr, 20, &dwBytesTransferred, 0);
ptr is pointer to the BYTE array. The above code of "ReadFiles() returns true and Reads Junk characters. I have written exact 20 characters through WriteFile() and it returned TRUE . I am not able to figure out the exact problem . Please provide any input in this regards. Thanks
-
No I am not using communication event. I have tried using
::WaitCommEvent
. But it never get any event and it goes into continuous wait state. So I have removed that and tried to read the Port using ::ReadFile () without using any event.
pandit84 wrote:
. But it never get any event and it goes into continuous wait state.
Which means nothing is available to read.
pandit84 wrote:
So I have removed that and tried to read the Port using ::ReadFile () without using any event.
So reading what's not available (to read) seemed like a better idea?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
You just got to use overlapped I/O when you are using COM ports. There are some classes/samples here. Just search for a suitable article.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
Finally this solution works for me. What I did is 1. In ReadFile() - Read from different COM port. E.G Writing is done on COM1 and Reading is Done on COM2. As I was having only 1 COM Port . I installed Virtual Port Software on my machine and Paired COM1 and COM2 2. ReadFile() provided a buffer of exact size which I used in WriteFile () method.