1

I am programming a dynamic C# interface to show real time states of my line follower robot sensors and motors. I am also sending a threshold of sensors to a microcontroller.

Now I want to receive the threshold value with MikroC. My code is:

char uart_rd[10];
unsigned long v;

while (UART1_Data_Ready() == 1) {
  UART1_Read_Text(uart_rd, "\0", 255);
  delay_ms(1000);

  v = uart_rd;
  UART1_Write_Text(v);

  PORTD = v;
}

My C# Code to send the number is :

private void sendData()
        {

            Console.WriteLine("sending");
            string Seuil = SeuilVal.Text;
            ComPort.Write(Seuil);           // Send the user's text straight out the port
            Console.WriteLine(Seuil);
            //SeuilVal.Clear();                       //clear screen after sending data
        }

When I send a number with 2 digits (99 for example) things are fine but when I send a number with 3 digits or more, I just receive two first digits (example I send 1234, but I receive 12).

1 Answers1

0

Try this to test if you can get back what you send.

C Code 1:

const int BUFFER_LENGTH = 10;          // Define the buffer length.
char buffer[BUFFER_LENGTH + 1];        // Extra one for NULL terminator.

while(1)
{
    if (UART1_Data_Ready())
    {
        memset(buffer, 0, BUFFER_LENGTH + 1);          // Clear the buffer.
        UART1_Read_Text(buffer, "\0", BUFFER_LENGTH);  // Double quotes for terminator string. Only read BUFFER_LENGTH number of chars.
        UART1_Write_Text(buffer);
    }
}

C Code 2:

char one_char = 0;

while(1)
{
    if (UART1_Data_Ready() == 1)
    {
        one_char = UART1_Read();
        UART1_Write(one_char);
    }
}

C# Code:

private void sendData()
{
    Console.WriteLine("sending");
    string Seuil = SeuilVal.Text + '\0';  // Text plus NULL terminator.
    ComPort.Write(Seuil);                 // Send the user's text plus NULL terminator straight out the port.
    Console.WriteLine(Seuil);
    //SeuilVal.Clear();                     // Clear screen after sending data.
}
tim
  • 482
  • 3
  • 12
  • It gives me the same problem tim :( – Oussama Elhassouni Jul 15 '19 at 22:34
  • Then I would suggest it's a hardware issue. That code should simply bounce back what it gets. BTW: I've tweaked the C code to clear the buffer before every read from the UART. – tim Jul 15 '19 at 23:16
  • Also try changing my char terminator '\0' to back your original string terminator "\0" in the C code. – tim Jul 15 '19 at 23:38
  • Thank u for help but i get the same result ive just changed "\0" with "1" and i got something different 323 is my value when i press send the first time the mikroc dnt respond but when i press a second time on send bouton i receive 32323 – Oussama Elhassouni Jul 15 '19 at 23:43
  • I've added another C code snippet to bounce back the chars one by one using simpler read/write functions. – tim Jul 16 '19 at 00:15
  • Thank you but still not working i simulate that with ISIS proteuse so i dnt think that its an hardware issue. im So gratefull for ur help TIm – Oussama Elhassouni Jul 16 '19 at 09:09