Thread: simple serial send/receive problem

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    simple serial send/receive problem >> Solved

    Hi,

    I’m making a simple network program that uses com1 to send/receive data,

    my packet structure is as follows
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    { D S T <--data load-----------> cs }

    D = destination
    S = source
    T = packet type
    cs = check sum
    }
    The thing is when I send my packet it seems to insert a null character at bit 9 of the packet even thou it's declared as

    char packet[16];

    Meaning that when I enter 1 - 9 then a letter for the data
    it’s received as

    packet 1 {??D1234
    packet 2 56789w*}

    If I send the data packet to HyperTerminal it displays perfectly
    so obviously it’s in my receive code... but I have no idea what it is im looking for...

    Any ideas / thoughts greatly appreciated

    Cheers,
    Kev
    Last edited by gMan; 04-02-2003 at 05:29 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a snippet of code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    deviant

    sorry

    Code:
         if ((cComChar = readcomm()) != 0)  // is a char waiting to be read from port?
          {
              
    		if (k <= 15)
    		{
    			rxpacket[k] = cComChar; // set first char to that already read
    			k++;
    		}else{
    			k = 0;
    		}
              rxflag = 1;
              iInput = COM;
              if (iInput == COM)
                {
                  //rxpacket[0] = cComChar; // set first char to that already read
    
                  for (iCount = 1 ; iCount < 16 ; iCount++)
                      rxpacket[iCount] = readcomm(); // then read next 15 to make packet
    
                  cDest = rxpacket[1]; // get destination char
                  cSource = rxpacket[2]; // get source workstation
                  cPktType = rxpacket[3]; // get packet type
                  
    			  printf("\npacket received: ");
                  for (k = 0; k <= 15; k++)
                    {
                      printf("%c", rxpacket[k]);
                      j++;
                    }
    			  printf(" End Packet\n");
    is how i read in the packet

    Cheers,
    Kev

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Hmm, I can't try your code, so I'm going to have to guess. Here's a reworked version based on what you posted.

    Code:
    for (i = 0; (cComChar = readcomm()) != 0; i++)
    {
      rxpacket[i] = cComChar;
      if (i == 15)
      {
        /* 16 bytes received, process complete packet */
        cDest    = rxpacket[1]; /* get destination char   */
        cSource  = rxpacket[2]; /* get source workstation */
        cPktType = rxpacket[3]; /* get packet type        */
        printf("\npacket received: ");
        for (k = 0; k < 16; k++)
        {
          printf("%c", rxpacket[k]);
        }
        i = -1; /* Reset i, this allows use to get a new packet */
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    deviant

    thanks ill have a go at that

    I would have posted all the code but its near on 700 lines

    not sure it would have been apprecaited

    EDIT: Solved

    The problem came down to timing :/
    I dropped in a wait function after each read from the COM port and low and behold the packets were all present and correct

    Thanks again for the help

    Cheers,
    Kev
    Last edited by gMan; 04-02-2003 at 05:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 04:07 PM
  2. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 07:07 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Weired serial port problem
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2003, 05:31 AM