This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What is wrong the sio.c?

I download intsio.zip and only changed some word in main.c as following:

void main (void)
{
com_initialize ();              /* initialize interrupt driven serial I/O */
com_baudrate (38400);           /* setup for 38400 baud */

printf ("Interrupt-driver Serial I/O Example\r\n\r\n");
while (1)
  {
  unsigned char c,cCmd,cCmdLen,i;

	c = getchar();
	if (c == 0xaa)
	{
		c = getchar();
		if (c == 0x55)
		{
			c = getchar();
			if (c == '@')
			{
				c = getchar();
				cCmd = c;
				c = getchar();
				if ((c+cCmd) == 0xff)
				{
					cCmdLen = getchar();
					for ( i = 0 ; i < cCmdLen ; i ++)
					{
						c = getchar();
						printf("%c" ,c);
					}
				}
			}
		}
	}
  }
}
and sio.c was never touched.Last,I transmit AA55409669EAC3F7D4C2BCB8CAB1D3D0A3BFB0D1BEC6CECAC7E5CCECA1A3... to 8051_rxd every_second .so,pc could received data transmitted from 8051,which sometimes is rightAA AA 55 55 40 40 96 96 69 69 EA EA C3 F7 D4 C2 BC B8 CA B1 D3 D0 A3 BF B0 D1 BE C6 CE CA C7 E5 CC EC A1 A3 B2 BB D6 AA CC EC C9 CF B9 AC E3 DA A3 AC BD F1 CF A6 CA C7 BA CE C4 EA A1 A3 CE D2 D3 FB B3 CB B7 E7 B9 E9 C8 A5 A1 A3 CE A9 BF D6 C7 ED C2 A5 D3 F1 D3 EE A3 AC B8 DF B4 A6 B2 BB CA A4 BA AE A3 AC C6 F0 CE E8 C5 AA C7 E5 D3 B0 A3 AC BA CE CB C6 D4 ....,and sometimes is incorrectAA 55 40 96 69 EA C3 C3 F7 F7 D4 D4 C2 C2 BC BC B8 B8 CA CA B1 B1 D3 D3 D0 D0 A3 A3 BF BF B0 B0 D1 D1 BE BE C6 C6 CE CE CA CA C7 C7 E5 E5 CC CC EC EC A1 A1 A3 A3 B2 B2 BB BB D6 D6 AA AA CC CC EC EC C9 C9 CF CF B9 B9 AC AC E3 E3 DA DA A3 A3 AC AC BD BD F1 F1 CF CF A6 A6 CA CA C7 C7 BA BA CE CE C4 C4 EA EA A1 A1 A3 A3 CE CE D2 D2 D3 D3 FB FB B3 B3 CB CB B7 B7 E7 E7 B9 B9 E9 E9 C8 C8 A5 A5 A1 A1 A3 A3 CE CE A9 A9 BF BF D6 D6 C7 C7 ED ED C2 C2 A5 A5 D3 D3 F1 F1 D3 D3 EE EE A3 A3 AC AC B8 B8 DF DF B4 B4 A6 A6 B2 B2 BB ....
and the message later just was doulbly transmitted everybyte.Can you tell what is wrong?
Best regards,
Nantiangumo.

Parents Reply Children
  • The ring buffer is ,256 bytes for transmitting while 256 bytes for receiving,and others to variables.I try changing getchar() to _getkey(),after first transmitting (prefix+234bytes),pc could receive some of prefix('@','cCmd') and 234bytes' message ,and the second time just received 6 bytes' prefix('AA','55','@','cCmd','cCmdComplex','cCmdLen');the thirst time's just like the first's,and the fourth's likes the second's ,such revolve.But everytime just transmit the same thing ,prefix + 234 bytes' message.
    I long for that you teach me how to resolve data in the ring buffer like these.
    Thank you very much.
    Best regards,
    Nantiangumo.

  • Now,I found that the main_loop would stop excuting when program runing in _getkey() while UART hadn't income data, in others words,the program would poll RI untill the data coming . If i want communicating with ISR_driven and running others program when serial has no incoming data,how can i do?

    Thanks a lot!

  • "the main_loop would stop excuting when program runing in _getkey() while UART hadn't income data"

    That's the way the SIO example works - it's just doing what it says on the tin!

    If this is not the behaviour you require, you will have to change it to your requirements.

    Me, I created a rx_available() function to check if any received character(s) are available, then I call getchar() only if there's something to get.

    "the program would poll RI untill the data coming "

    Not if you're doing interrupt-driven serial IO!
    If your program really is doing this, it might explain your problem...

    "If i want communicating with ISR_driven and running others program when serial has no incoming data,how can i do?"

    See above.

  • Well, the _getkey routine is included in the main.c file as follows:

    /*------------------------------------------------------------------------------
    _getkey waits until a character is received from the serial port.  This may not
    be the exact desired operation (for example if the buffer is empty, this
    function hangs waiting for a character to be received).
    ------------------------------------------------------------------------------*/
    char _getkey (void)
    {
    int k;
    
    do
      {
      k = com_getchar ();
      }
    while (k == -1);
    
    return ((unsigned char) k);
    }
    

    The SIO.C file includes routines to test the length of the serial buffer BEFORE you try to get a character using _getkey. Alternatively, you can use com_getchar (also in SIO.C) that returns a -1 if there are no characters in the buffer.

    Jon