This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Sending and receiving Multiple Chars



I am currently trying to interface between LabView and the MSP430 receiving a character from the UART and sending a certain string based on the character received (for instance if i get an a i send the string alpha, b sends bravo, etc)

There are 3 different outputs in Lab View that correspond to the letter sent and the string to be recieved. These are controlled by switches on LabView that will constantly send a letter if turned on. 

Here are my interrupts for RX and TX:

#pragma vector= USCIAB0RX_VECTOR
interrupt void USCIORX_ISR(void){

	P1OUT |= RXLED; // turn on red LED
	buf = UCA0RXBUF;
	if(buf == 'a') //did we get an A?
	{
		string = a; // string = to alpha
		UC0IE |= UCA0TXIE;
		UCA0TXBUF = string[i++]; // send the string alpha

	}
	if(buf == 'b') //did we get a B?
		{
			string = b;
			UC0IE |= UCA0TXIE;
			UCA0TXBUF = string[i++]; // send the string alpha

		}
	if(buf == 'c') //did we get a C?
		{
			string = c;
			UC0IE |= UCA0TXIE;
			UCA0TXBUF = string[i++]; // send the string alpha

		}



	P1OUT &= ~RXLED;
}

#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
	P1OUT |= TXLED;


	UCA0TXBUF = string[i++];
	if(i == sizeof string-1)
	{
		UC0IE &= ~UCA0TXIE; // if end of string, stop sending//
		buf = NULL;
		i = 0;

	}

	
	P1OUT &= TXLED;
}


Right now 2 different things are happening.
1. the outputs in lab view will read in odd garbage chars
2. The outputs will fluctuate between the strings corresponding to the letters the 
MSP430 recieves. 



How may I change/improve this code to send the strings steadily to the right out puts. 
(Lab view sends the letters constantly in an order based on the switch positions.
Ex: if just a is on ---> sends: 'a' 'a' 'a' ...
Ex: if a and b are on ---> sends: 'a' 'b' 'a' 'b'...
Ex: if a and b are on ---> sends: 'a' 'c' 'a' 'c'...
and anything in between)

 

  • The problem is that you're immediately servicing each new Rx character, even if Tx is going on. This causes the shared data structures to jump around, including running "i" past the end of "string". This latter is probably what is causing the garbage characters.  As long as your response string is longer than the request string you're open to this.

    What I see here is a definitional problem: What result do you want if:

    1) you get 'a', and, while you're sending the response string, you get another 'a'? Should you ignore it? Restart the response? Queue up another copy of the response?

    2) Similarly, but for the case where the second character is 'b'?

    I'm going to guess at the effect you'd like to see, and offer a very simple approach:

    1) When you see an 'a', append the response string to a shared buffer (I suggest a circular buffer) that feeds the Tx interrupt, and set a separate flag that says "I have sent an 'a' response". Similarly for 'b', 'c', etc.

    2) If you see an 'a', and the "I have sent an 'a' response" flag is set, ignore it.

    3) When the Tx buffer becomes empty (in the Tx ISR), clear all of the "I have sent.." flags.

    With this, each response is delivered complete, you're guaranteed to reach step (3), and if you keep getting requests you'll keep sending responses (but not one-for-one). If you add up the lengths of all of the response strings, this gives you an upper bound on the size of the buffer. Dividing this by your bit rate (e.g. at 9600bps, one byte is one millisecond) gives you a lower bound on your auto-repeat rate.

    Embellishments are left as an exercise.

**Attention** This is a public forum