Thread: Serial communication packets

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Serial communication packets

    Developing Serial communication unitlity.

    Can you please suggest me which is best way to receive the data on com port? I have created "Non -Overlapped" com Port.

    Let me tell you, where I am. Created NON-OVERLAPPED port, set the baud rate to 38400 and STOP bit by SetCommState(). sending data is perfect. But while receiving it used to splits the bytes in first ReadFile() and captures in next ReadFile, which is definately not acceptable as we wanted response to have some sensible data for the command sent.


    Here is pseudocode for Receive.
    Code:
    timeouts.ReadIntervalTimeout = 3;
    timeouts.ReadTotalTimeoutMultiplier = 3;
    timeouts.ReadTotalTimeoutConstant = 2;
    timeouts.WriteTotalTimeoutMultiplier = 3;
    timeouts.WriteTotalTimeoutConstant = 2;
    
    if ( !SetCommTimeouts(hComm,&timeouts) )
    	printf("Error Setting Communication Timeouts\n");
    while (1)
    {
    	bResult = 	ReadFile(hComm,Recvbuffer,MAX_MESSAGE,&nbytes,NULL);		
    if ( 0 == bResult )
    	{
    		printf("Reading of serial communication has problem.");
    		return FALSE;
    	}
    	if ( nbytes )
    	{
    		printf("String = %s and length = %d\n",Recvbuffer,nbytes);
    		break;
    	}
    }
    Since above the command, I tried to implement code from Allen Denver in MSDN. But this seems to be not working. In the sense, control is not coming our of for loop. Could you please tell, what could be wrong?

    Code:
    timeouts.ReadIntervalTimeout = MAXDWORD;
    timeouts.ReadTotalTimeoutMultiplier = 0;
    timeouts.ReadTotalTimeoutConstant = 0;
    timeouts.WriteTotalTimeoutMultiplier = 0;
    timeouts.WriteTotalTimeoutConstant = 0;
    
    if ( !SetCommTimeouts(hComm,&timeouts) )
    	printf("Error Setting Communication Timeouts\n");
    
    DWORD dwCommEvent; 
    DWORD dwRead; 
    char chRead; 
    if (!SetCommMask(hComm, EV_RXCHAR)) 
    // Error setting communications event mask
    
    			char chRead,i=0;
    			
    			for ( ; ; )
    			{
    		if ( WaitCommEvent(hComm, &dwCommEvent, NULL ) )
    					{
    						do 
    						{					
    							bResult = ReadFile(hComm,	// handle of file to read
    									  &chRead,          // buffer
    									  1,				// number of bytes to read
    									  &nbytes,          // pointer to number of bytes read
    									  NULL); 	        // pointer to structure for data
    							if ( bResult )
    							{
    								Recvbuffer[i++] = chRead;
    								//printf("%c",chRead);
    							}
    							else
    								break;
    						} while (nbytes);
    					}
    					else 
    						break;
    				}
    				Recvbuffer[i] = '\0';
    				printf("String = %s and length = %d\n",Recvbuffer,i);
    I have dropped this option and increased the ReadTimeout value to 20 from 3 in my eralier code. That seems to be working, but want to know, why second section of code in notworking. Anybody faced similar problem and fixed later.


    Thanks,

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Try adding start and end charactors to the message when sending, then check for these charactors when receiving. Begin filling the buffer when STX is received and stop when ETX is received, the buffer will then contain the complete message.
    Code:
    #define STX '\002'
    #define ETX '\003'
    SEND PROG: Add STX at the start of the message buffer and ETX at the end.
    Code:
        sprintf(Sendbuffer,"%c%s%c\n", STX, MessageBuffer, ETX);
    RECEIVE PROG:
    Code:
    memset(Recvbuffer, NULL, YOUR_BUFFER_SIZE);
    BOOL NonValidCharactersFlag = TRUE, bReading;
    char chRead[2];
    for(;;)
    {
          bReading = 1;
          do
          {
                memset(chRead, NULL, 2);
                ReadFile(hComm, &chRead, 1, (unsigned long*)&nbytes, NULL); 
                if(chRead[0] == STX)
                {
                      NonValidCharactersFlag = FALSE;
                      memset(Recvbuffer, NULL, YOUR_BUFFER_SIZE);
                }
    
                if(chRead[0] == ETX)
                { 
                      NonValidCharactersFlag = TRUE;
                      bReading = 0;
                }
                else if(strlen(Recvbuffer)>=YOUR_BUFFER_SIZE)
                {
                      memset(Recvbuffer, NULL, YOUR_BUFFER_SIZE);
                      NonValidCharactersFlag = TRUE;
                }
                else if(NonValidCharactersFlag == FALSE)
                {      // Add character read to last position in buffer
                      strcat(Recvbuffer, chRead);
                }
          }while(bReading);
    
          //process new message here...
    }
    Last edited by Scarlet7; 04-25-2003 at 04:23 PM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Thanks for reply.

    What is disadvantegae of reading more characters at same time(in ReadFile)?

    Somehow, I am not able get code( from MSDN) article working. Could you please tell me what is wrong with event based reception?

    Thanks again..

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    no disadvantages of reading more characters at same time its up to the developer to choose the option the best suites to task.

    You really need to know when a message starts and ends by knowing the sizes or by sending start and end markers (like the example in my last post STX & ETX ), a good method for variable text based messages.

    eg:
    if you're sending messages with a size of 300 bytes and you're reading in 400 then you're going to have one full message and part of the next.

    With reading more than 1 byte you need know the size of the message being received. You could have a fixed message size, then read that size. Or if variable sizes then have a message header that gives the size of the next massage then read that size. Or have start and end markers. Or have large circular receive buffer which is continually filled with messages with the front and back positions marked, then another thread would continually read the messages from the buffer adjusting the back position, a buffer overflow will occur when the front catches the back.

    There are many different ways.
    Last edited by Scarlet7; 04-26-2003 at 03:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplex communication thro serial port
    By Priyachu in forum Linux Programming
    Replies: 1
    Last Post: 05-30-2009, 04:03 AM
  2. Serial pot communication through c program
    By vin_pll in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2009, 12:52 PM
  3. Serial Port Communication
    By maxorator in forum C++ Programming
    Replies: 11
    Last Post: 04-27-2006, 03:13 PM
  4. Please help with serial communication problem - Long
    By spdylude in forum Windows Programming
    Replies: 3
    Last Post: 04-06-2005, 09:41 AM