Author Topic: uartGetByte return data code problem  (Read 5149 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
uartGetByte return data code problem
« on: February 23, 2008, 08:55:39 PM »
Here is what I want to do . . . I want to recieve a character from the UART, but it also needs to check if a value is there to recieve first (a -1 is returned if there is no value). I'm not entirely sure how to code it, but here is my attempt:

Code: [Select]
if (characterFromCamera = uart2GetByte() != -1)//check for data
  {
  do stuff
  }

this is the related warning I get when compiling:
file.c:290: warning: suggest parentheses around assignment used as truth value

Is there a better way to write this code?

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: uartGetByte return data code problem
« Reply #1 on: February 23, 2008, 09:03:50 PM »
Use a FIFO buffer and you can use it to check if there is anything in the buffer, there's the wiring_serial.c and .h file from the Arduino core folder that you can use for this.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: uartGetByte return data code problem
« Reply #2 on: February 23, 2008, 09:21:30 PM »
I'm using uart.h from avrlib (specially modified to handle 4 uart's).

It checks if anything is in the buffer, returning a -1 if nothing is in it.

I just don't know how to check and store the value at the same time in my if statement . . .

btw, characterFromCamera is a char, so I can't just store -1 in it and then do the check . . .

Offline Darkbluestar

  • Jr. Member
  • **
  • Posts: 36
  • Helpful? 0
Re: uartGetByte return data code problem
« Reply #3 on: February 24, 2008, 12:36:17 AM »
I think that there is no way to check if the return value is -1 without saving it to a variable first. So you could just make a temporary int to save the return value and then if it's not -1 set characterFromCamera to it.

Code: [Select]
int temp;
if ((temp = uart2GetByte()) != -1)//check for data
  {
  characterFromCamera = temp;
  do stuff
  }

I also believe your warning (file.c:290: warning: suggest parentheses around assignment used as truth value) is happening because you need parenthesis around "characterFromCamera = uart2GetByte()" so that the compiler doesn't get confused.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: uartGetByte return data code problem
« Reply #4 on: February 24, 2008, 09:25:18 AM »
Adding just the parenthesis results in this warning:
"file.c:297: warning: comparison is always true due to limited range of data type"

Makes sense, cause -1 can't be stored in a char . . .

And I can't just store uart2GetByte() into an int type variable, because I could also recieve letters . . .

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: uartGetByte return data code problem
« Reply #5 on: February 24, 2008, 01:34:06 PM »
why not

Code: [Select]
int temp = uart2GetByte();
if (temp != -1)//check for data
{
  characterFromCamera = temp;
  do stuff
}

Offline Darkbluestar

  • Jr. Member
  • **
  • Posts: 36
  • Helpful? 0
Re: uartGetByte return data code problem
« Reply #6 on: February 25, 2008, 12:40:43 AM »
What frank said would work too. You are getting that warning because char variable only goes from 0 to 255 (no negative numbers). You can however store the return value into an int check it and then transfer it to a char. You might have to cast temp to a char first but I doubt you have to.

edit-
let me be a little more clear about putting the return value in an int. The char variable is basically just a number between 0 and 255 that is used to represent some letter or symbol. So if you save a character to an int it treats it as a number, but when you save an int to a char it treats it as the character(value have to be between 0-255).
« Last Edit: February 25, 2008, 03:50:47 AM by Darkbluestar »

paulstreats

  • Guest
Re: uartGetByte return data code problem
« Reply #7 on: February 25, 2008, 07:16:35 PM »
could you use an interrupt function on receive?

Ive got a PIC program that when a byte is received, it runs through the interrupt sequence, and then run a function if the correct interrupt code is used. Im not sure if your mcu supports it though.

Also have you tried just running the code to see if it works, You can still compile on 'warning' usually, such as the "suspicious pointer conversions" that i sometimes get but the code still works fine. Its just warning you that its not expected syntax/coding behaviour but not necessarily an error. Also it is the flag bit that you need to read (even if the uart buffer is empty it will still have a value - probably 0x00 but it is still present so it will always return true)

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: uartGetByte return data code problem
« Reply #8 on: March 08, 2008, 10:37:30 PM »
Still doesn't quite work . . . no more compiling errors at least!

This is my code. Sorry for a change in variable names, but I'm doing almost the same thing for two separate projects . . .

Code: [Select]
char car_id;
int temp;

while(1)
{
temp=uart3GetByte();
rprintf("temp: %d\r\n",temp);
car_id=temp;
rprintf("car ID: %d\r\n",car_id);

  if (temp != -1)//check for data
{
car_id=temp;

rprintf("car ID transmitted: %d\r\n",car_id);
break;
}
}

It detects whether data is sent correctly or not, however, the data it recieves is incorrect.

For example, suppose I transmit "345as" to it, it returns:

temp: 51
car ID: 51
car ID transmitted: 51

thoughts?

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: uartGetByte return data code problem
« Reply #9 on: March 09, 2008, 12:16:10 AM »
345as? you are sending a string and trying to place it into a single byte
the decimal value of the ASCII character "3" is 51

if your ID is a string, you can't use uart3GetByte() alone, I don't mess with strings so I would just use a array variable to store 5 characters, and verify each one.

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: uartGetByte return data code problem
« Reply #10 on: March 09, 2008, 06:03:15 AM »
%d returns the numerical value of a variable.
if you want this translated into the ASCII value you want to use %c

instead of
Code: [Select]
rprintf("car ID transmitted: %d\r\n",car_id);try using
Code: [Select]
rprintf("car ID transmitted: %c\r\n",car_id);

Edit:
forgot to say, if you transmit the value 51 direct to the UART (ie, not using rprintf) it will output the ASCII character "3".
the code goes something like this:
Code: [Select]
// transmit a character to the UART.
// note that this routine doesn't use interrupts.
// program execution will pause here if the UART is busy until cleared.
void output_ch_0 (char  data)
        {
        wdt_reset();
        while (!(UCSRA & (1<<UDRE)))
                { }
        UDR = data;
       
        return;
        }


dunk.
« Last Edit: March 09, 2008, 07:00:42 AM by dunk »

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: uartGetByte return data code problem
« Reply #11 on: March 09, 2008, 08:37:55 AM »
Quote
%d returns the numerical value of a variable.
if you want this translated into the ASCII value you want to use %c
I feel dumb now, I should have checked the rprintf.h comments but just wasn't thinking at the time . . . that fixed it . . .

Quote
   /// Called by rprintf() - does a simple printf (supports %d, %x, %c).
   /// Supports:
   /// - %d - decimal
   /// - %x - hex
   /// - %c - character

Quote
345as? you are sending a string and trying to place it into a single byte
the decimal value of the ASCII character "3" is 51
Yea, I'm programming it in small steps to make it easier to debug. The next step is not to just read the first character, but the entire string.

 


Get Your Ad Here

data_list