CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Feb 2012
    Posts
    181

    Question Com-port sometimes work incorrectly...

    I wrote 2 program
    Reader:
    Code:
      for (;;)
    	while (!dwRead)
    	{
    	  ClearCommError(hComPort, &Err, &ComStat);
          dwRead = ComStat.cbInQue;
    	  Sleep(10);
    	  be_inside++;
    	  if (be_inside > 30000)  break;
    	 }
      	 if (be_inside > 30000) 
    		 break;
         ReadFile(hComPort, read, min(dwRead, 256), &tmpBytesRead, NULL);   
    	 read[dwRead] = '\0';
         for (int i=0; i < dwRead; i++)
    	 {
             int temp = 0;
    		 temp = read[i];
    		 std::cout<<temp<<' ';
    	 }
    	 std::cout<<'\n';
         buff[0] = 31;
    	 buff[1] = 0x66;
    	 buff[2] = 'C';
    	 Sleep(5);
    	 WriteFile(hComPort, buff, 3, dwBytesWritten, NULL);
      }
    Writer:
    Code:
     	BYTE buff[50];
    	BYTE read[50];
    	LPDWORD dwBytesWritten = new DWORD;
    	for (char c = 'A'; c <= 'Z'; c++)
    	{
            buff[0] = 31;
    	  buff[1] = 0x66;
    	  buff[2] = c;
    	  WORD CRC = CRC16(3, buff);
    	  buff[3] = LOBYTE(CRC);
    	  buff[4] = HIBYTE(CRC);
         PurgeComm( hComPort, PURGE_TXCLEAR);
    	  WriteFile(hComPort, buff, 5, dwBytesWritten, NULL);
    	COMSTAT ComStat;
    	DWORD Err;
        ClearCommError(hComPort, &Err, &ComStat);
    	DWORD dwRead = ComStat.cbInQue;
    	int be_inside = 0;
    	while (!dwRead)
    	{
    	  ClearCommError(hComPort, &Err, &ComStat);
          dwRead = ComStat.cbInQue;
    	  Sleep(1);
    	  be_inside++;
    	  if (be_inside > 10000) break;
    	 }
    	if (be_inside > 10000)
    		 continue;
          DWORD tmpBytesRead = 256;
    	  ReadFile(hComPort, read, min(dwRead, 256), &tmpBytesRead, NULL);
    	  PurgeComm(hComPort, PURGE_RXCLEAR);
       }
    But sometimes a package from Writer differs with a package from Reader. Why problem is appeared?
    Last edited by AKE; February 21st, 2012 at 12:26 PM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    How are these snippets related? Separate programs/threads?

    As far as I can see you don't bother how many bytes you actually read in the first snippet. You just assume you get min(dwRead, 256) bytes.

    In the second snippet you call PurgeComm, What if not all previously written bytes have been transferred yet? The same goes for the PURGE_RXCLEAR

    There are many good com port classes available for free. Why not use one of those instead?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Feb 2012
    Posts
    181

    Question Re: Com-port sometimes work incorrectly...

    Quote Originally Posted by S_M_A View Post
    How are these snippets related? Separate programs/threads?

    As far as I can see you don't bother how many bytes you actually read in the first snippet. You just assume you get min(dwRead, 256) bytes.

    In the second snippet you call PurgeComm, What if not all previously written bytes have been transferred yet? The same goes for the PURGE_RXCLEAR

    There are many good com port classes available for free. Why not use one of those instead?
    Separate program.
    surprising, that this error is appeared only in special kind of transferring (not always), like this:
    1fh 67h x x x
    1fh 67h x x
    In other cases I don't meet this error.
    Maybe it's firewall work or mask or special command?
    Please give links on normal com-port classes...
    Last edited by AKE; February 21st, 2012 at 02:30 PM.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    I'm not trying to be rude but the code you have shown seems like having evolved out of some trial and error method so I'm not that surprised that you have issues with it.

    Some examples of ready to use classes

    MFC:
    http://www.naughter.com/serialport.html
    http://www.codeguru.com/cpp/i-n/netw...icle.php/c2503

    Non MFC:
    http://hdrlab.org.nz/articles/window...rt-in-windows/
    http://www.codeproject.com/Articles/...-Communication
    http://www.codeproject.com/Articles/...-library-for-C
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Feb 2012
    Posts
    181

    Re: Com-port sometimes work incorrectly...

    S_M_A , Thanks, I'm testing first link... Maybe problems will soon

  6. #6
    Join Date
    Feb 2012
    Posts
    181

    Question Re: Com-port sometimes work incorrectly...

    S_M_A
    Execuse me.
    The same error. I used first your class such:

    Reader
    Code:
       COMMCONFIG config;
        CSerialPort::GetDefaultConfig(1, config);
        CSerialPort port;
    	BYTE pBuf[256];
    	port.Open(L"COM1", CBR_9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::NoFlowControl);
    	bool d;
    	for (int i = 1; i <30; i++)
    	{
          d = port.DataWaiting(300000);
    	if (d)
    	{
    	DWORD dwCount = port.BytesWaiting();
        try
         {
           port.Read(pBuf, dwCount);
    	   CString str;
    	   if (pBuf[0] != 31)
    	   {
    	     d=d; //for breakpoint if error;
    	   }
    	   for (int i = 0; i < dwCount; i++)
    	   {
    	     //str += (int) pBuf[i];
    	   }
    	   
    	   port.Write(pBuf, dwCount);
         }
         catch(CSerialException* pEx)
         {
           int i = 0;
    	 }
    	}
    	}    
    	return 0;
    }
    Writer:
    Code:
        COMMCONFIG config;
        CSerialPort::GetDefaultConfig(1, config);
        CSerialPort port;
    	BYTE pBuf[256];
    
    	port.Open(L"COM3", CBR_9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::NoFlowControl);
    	for (char ch = 'A'; ch <= 'Z'; ch++)
    	{
    	pBuf[0] = 31;
    	pBuf[1] = 0x67;
    	pBuf[2] = ch;
    	WORD CRC = CRC16(3, pBuf);
        pBuf[3] = LOBYTE(CRC);
        pBuf[4] = HIBYTE(CRC);
        try
         {
          port.Write(pBuf, 5);
         }
        catch(CSerialException* pEx)
        {
          if (pEx->m_dwError == ERROR_IO_PENDING)
          {
    	    int i = 0;//for breakpoint;
    	  }   
    	}
    	bool d = true;
    	if (port.BytesWaiting() == 0)
           d = port.DataWaiting(10000);
    	if (d)
    	{
    	DWORD dwCount = port.BytesWaiting();
       try
        {
          port.Read(pBuf, dwCount);
        }
        catch(CSerialException* pEx)
        {
          int i = 0;//for breakpoint
    	}
    	}
    	}
    if i set pBuf[1] = 0x66; pBuff[1] = 0x68 all work without error... Mistery)
    P.S. This two snippets from two programs on different computers...
    Last edited by AKE; February 22nd, 2012 at 10:30 AM.

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    Before I dig any deeper into this, I neither have a serial cable nor 2 ports/machines available so I have to read the documentation and the code, what compiler/platform do you use?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    The zip file contains a test code as well. Have you runned that? I guess it should be possible to run that code on both 2 computers with a connected serial cable to do a more thorough check. If that works you have done something wrong.

    According to the documentation the code requires MSVC 2005 or newer.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Feb 2012
    Posts
    181

    Exclamation Re: Com-port sometimes work incorrectly...

    WinXP
    VisualStudio2008
    Now I'm testing including program.

  10. #10
    Join Date
    Feb 2012
    Posts
    181

    Exclamation Re: Com-port sometimes work incorrectly...

    Program copies are not bind together.
    About my program
    Strange situation:
    Notebook->PC - has bugs...
    PC->Notebook - absent...
    I use USB2COM connector...

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    Weird, since Win3.11 the only issues I have with RS232 communication is whether to use a twisted cable or not...

    USB2COM devices work well these days, even when using speeds of several Mbits/sec, but of course there's always a possibility for malfunction. Just to rule it out, can you get another one (preferably another brand) to try with?

    You might also try using the test application in one end and a terminal program in the other end and see if that makes a difference. If you have issues change the amount of data that is sent at the same time to rule out buffer handling issues.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  12. #12
    Join Date
    Feb 2012
    Posts
    181

    Re: Com-port sometimes work incorrectly...

    S_M_A,
    I tested it without USB2COM, but this error is appeared again...

  13. #13
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    Have you tried with a terminal program? I.e. the test application on the PC, terminal program on the notebook and also the other way around.
    Edit: Another test would also be to open a terminal program at both platforms. Send an ascii text file of say about 100 bytes from one side at a time and check if all characters are transferred. A modern hardware shouldn't drop any characters at 115200 baud even without any flow control.
    Last edited by S_M_A; February 22nd, 2012 at 05:15 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  14. #14
    Join Date
    Feb 2012
    Posts
    181

    Re: Com-port sometimes work incorrectly...

    S_M_A
    Please, advice me normal free terminal program...

  15. #15
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Com-port sometimes work incorrectly...

    In XP you have HyperTerminal but that's really a crappy one so go for putty instead. The full version is capeable of running all kinds of protocols so you have good use for it in other situations as well.

    http://www.putty.org/
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured