6

I'm trying to send/receive a string through C#, in C# i just do:

SerialPort.WriteLine("A6");

but in CCS, if i try sending a string char after char it does not work at all, neither with ReadLine nor with ReadExisting! This is what i have tried creating an array, so that everytime we enter the RXBUFF pragma, we add the received char to the array, until the array is full (i randomly defined the array size to be 2, which means we deal with 2-char-length strings), and eventually send the string by sending char after char:

 #pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)

if(__even_in_range(UCA1IV,18) == 0x02){          // Vector 2 - RXIFG
    if(counter==0)
    {
        Data[0]=UCA1RXBUF;
        counter++;
    }
    else
    {
        Data[1]=UCA1RXBUF;
        counter=0;
        UCA1TXBUF=Data[0];
        while(!(UCA1IFG & UCTXIFG)); // until UCTXBUF1 is empty
        UCA1TXBUF=Data[1];
    }
}

in C#:

 listBox2.Items.Add(SerialPort.ReadExisting());

i get non-sense text, like : ??A??? sometimes : ????A? etc.., but with:

listBox2.Items.Add(SerialPort.ReadLine());

in the first time i press the Send button which sends the "A6", i get nothing, the second time i get non-sense aswell , just like the ReadExisting behavior.

by the way, even if i try to send the string in the easiest way (without array and conditions), i mean like this:

#pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
        UCA1TXBUF='A';
        while(!(UCA1IFG & UCTXIFG));  // until UCTXBUF1 is empty
        UCA1TXBUF='6';

i also get inconsistent items in the listbox.

However, if i do this:

#pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
UCA1TXBUF=UCA1RXBUF;

i do get "A6" in the listbox and everything just work fine (with ReadLine and ReadExisting)! could anyone just tell me why this is happening?

Renya Karasuma
  • 1,044
  • 4
  • 11
  • 18

2 Answers2

1

I'v just neutralized the Parity bit, everything works now, Thank you all!

Renya Karasuma
  • 1,044
  • 4
  • 11
  • 18
0

This indicates that you shouldn't be waiting for the TX flag inside the RX ISR. The RX interrupt routine should simply fill a FIFO buffer (a byte queue), so that you can parse its contents somewhere else (main routine?), and then create a response when needed.

Pseudo code for the RX ISR should be something like:

#pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
FIFO_Enqueue(&RxBuffer, UCA1RXBUF);

And somewhere inside the main() loop you can parse its contents:

while (1) 
{
    // find the first occurrence of "A6" and dequeue it
    if (FIFO_StartsWith(&RxBuffer, "A6")
        SendResponse();
}
vgru
  • 49,838
  • 16
  • 120
  • 201
  • 1
    You are right, it fills a FIFO buff, and that's what i'm doing using the array, which i don't think its the problem, but i still don't understand what is the difference between sending the chars in the ISR or in main! anyway i tried as you said: http://postimg.org/image/cggftjfdl/ but it does not work aswell. – Renya Karasuma Jul 22 '14 at 01:57
  • after neutralizing the Parity bit everything works fine but only in ISR routine and not in main (also just with ReadExisting but not with ReadLine), how weird! – Renya Karasuma Jul 22 '14 at 03:08