Hi everyone, I'm new here,I'm playing a lot with my Flyport but I have a problem with the UART:
I want to control the Flyport with commands throught the UART this is my code :
char MChoose[8] = " ";
char new_string[6];
int number;
i = 1;
while(UARTBufferSize(1)>0)
{
UARTWrite(1,"Buffer Size:");
number = UARTBufferSize(1);
sprintf(new_string ,"%d" , number);
UARTWrite(1,new_string);
UARTWrite(1,"\n");
UARTRead(1,MChoose[i],1);
UARTWrite(1,"Reading:\n");
UARTWrite(1,MChoose[i]);
UARTWrite(1,"\n");
if(MChoose[i]=="\n") {
UARTWrite(1,"got it!\n");
UARTWrite(1,MChoose);
}
i++;
}
}
BUT it doen't work, when the flyport write MChoose[i] I get wrong charter, and MChoose it's all wrong,
Did someone have an idea of the problem?
thank you :)

.png)

No idea? anyone control the flyport throgh commands? did some one have an example?
If you can't open it, you don't own it.
Hi! to have an example on how Flyport can be controlled throu UART commands you can refer to the Programmers Guide (in the download section), and see the example of the "Energy Saving Usage Example" @ page 62
A little suggestion by me is to insert a vTaskDelay(20); before the number = UARTBufferSize(1);
This can help Flyport to wait the complete transaction of UART messages on receive buffer.
This Cat's on a Hot Tin Roof!
Thanks simmartin , I got it working with the delay :)
If you can't open it, you don't own it.
No way, I can't get that "\n"!
this is my new code :
unsigned char buffer_uart[32] = "-";
unsigned char capture_uart[32] = "-";
static int buf_ptr;
static char got_char;
while(1)
{
if (UARTBufferSize(1)>0) // Check if datas are ready to be read.
{
vTaskDelay(50);
UARTWrite(1,"There something in the buffer...\r\n");
UARTRead(1, buffer_uart[buf_ptr] , 1);
if (buffer_uart[buf_ptr]== '\r') // CR character received ?
{
UARTWrite(1,"got CR\r\n");
buffer_uart[buf_ptr] = '*'; // Replace it with * character and ends the buffer string
}
if (buffer_uart[buf_ptr]== '\n') // LF character received ?
{
UARTWrite(1,"got LF\r\n");
buffer_uart[buf_ptr] = '\0'; // Replace it with \0 character and ends the buffer string
got_char=TRUE;
}
// Overload check : If string is > 32 bytes long then ends it with '\0'
if (buf_ptr==31)
{
buffer_uart[31] = '\0';
got_char=TRUE;
}
buf_ptr++;
}
if(got_char) // If buffer ready to be used : do something :
{
buf_ptr=0;
got_char=0;
capture_uart[0]='\0'; // clear old displayed datas
strcpy (capture_uart,buffer_uart); // copy current buffer to buffer to be displayed
UARTWrite(1,buffer_uart);
buffer_uart[0]='\0'; // clear current buffer and be ready for next incoming string
}
}
DId someone getting the "\n" or "\r" ?
I think UARTRead(1, buffer_uart[buf_ptr] , 1); have something wrong.
If you can't open it, you don't own it.
Hi! you are not following my suggestion well... you have to wait, and them retry with the number = UARTBufferSize(1);
Then you should use UARTRead(1, buffer_uart, number); in this way you will read all the values...
after, inside a for loop you can check if there are some '\r' and/or '\n' characters...
and don't forget to use the UARTFlush(1); to empty the uart receive buffer character counter!
This Cat's on a Hot Tin Roof!
This code work, without delays:
#define cmd_len_max 20
int toread = 0;//counter form charter
char uread[1];//charter got from serial
char ustg[cmd_len_max];//command string
while(1)
{
// Check UART commands
while(UARTBufferSize(1))
{
UARTRead(1,uread,1);//get a char
if ((int)uread[0]==13)//if i get a CR
{
if(toread>0)//and there are charter in the string
{
// UARTWrite(1,"\r\n");
for(i=0; i<toread; i++)//print back the command charter by charter
{
UARTWriteCh(1,(int)ustg[i]);
}
UARTWrite(1,"\r\n");
ustg[toread+1]='\0';//add null at the and of the command
//
//put command parsing here!
//
toread = 0;//reset the string array index
}
}
else
{
if(toread<(cmd_len_max-1))//if there is space in the array string
{
ustg[toread]=uread[0];//put charter in the array
toread++;//increment the string array index
}
}
}
}
If you can't open it, you don't own it.