Pages: [1] 2 3   Go Down
Author Topic: Varible Conversion & Serial question  (Read 1714 times)
0 Members and 1 Guest are viewing this topic.
Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im trying to send a incomming serial message over RF. but when i try to compile the code i get an error like: invailed conversion from int to const char*
and than highlights this line:
Code:
const char* msg = Serial.read();

i have tried to use char(Serial.read());
but than the compiler tells me that i cannot convert from char to const char*
so i removed const since it isn't contante (i think) but still get error because of the "*" (star)

i just want to put the incomming serial message into the msg
anyway please note that i am using VirtualWire and therfore have trouble when chaning the var type of msg

my total code:
Code:
#include <VirtualWire.h>

void setup()
{
  //Setup VirtualWire to transmit on 4
  vw_setup(100);
  vw_set_tx_pin(4);
  
  pinMode(13, OUTPUT);    //Power LED
  digitalWrite(13, HIGH); //Always ON
  
  //setup a serial connection
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)
  {
    const char* msg = Serial.read();
    if(*msg != 'for' ||
       *msg != 'bac' ||
       *msg != 'lef' ||
       *msg != 'rig' ||
       *msg != 'xxx')
       {Serial.println("ERROR SENDING:");Serial.println(msg);}
    else
       {
         vw_send((uint8_t *)msg, strlen(msg));
       }
  }
}

thanks alot already smiley-wink
nick
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.read returns a single int, not a pointer to a char.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Serial.read returns a single int, not a pointer to a char.
If you ALWAYS call Serial.available() first, and only call Serial.read() IF there is data available, the return value can be stored, safely, in a char. Still not the same as a char *, though. You can, of course, store the char that you get in an array, along with several others, to make a string.
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, i've read all that, but now i come to the question how do i do that,
how do i store the Serial data to a Array and than convert it to a string..?
btw, dont you get an error when compiling a string to a char?

thnx
nick
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
yes, i've read all that, but now i come to the question how do i do that,
how do i store the Serial data to a Array and than convert it to a string..?
btw, dont you get an error when compiling a string to a char?
First thing you need to do is to learn when to use a capital letter, and when not to. You can create an array, not an Array.

Second thing you need to do is to define exactly what constitutes a packet. What do you want to put in the NULL terminated array of chars, also known as a string? Specifically, when do you start and stop reading?

Yes, you get a compiler error when comparing a string to a char. They are different things, and will never be equal. You'd get the same error comparing a string and a float or an int.

If you add/use start and end of packet markers, like you should, you can read serial data and store it in an array using code like this:
 
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

some questions..


why using buadrate 57600, is 9600 not fast enough?
why is inData having a size of 80 chars?
does Serial.read() only return 1 char at the time? if so.. did you solve that by making the var inData an Array instead of a regular char?
whats the difference between an array and Array?
what excactly does "if(inChar == SOP) " compare inChar too?
if "if(inChar == SOP)" returns true, notthing happens only some variables are set to a state, but later on the variables are used nowhere, why would u even compare than?
furthere more, this is a difficuld way to me for compiling the serial data to a const char*, isn't there a simpler way?
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
why is inData having a size of 80 chars?
The number of characters on a punched card, of course!

Us oldies are suckers for tradition.

Quote
if "if(inChar == SOP)" returns true, notthing happens only some variables are set to a state,

SOP stands for "start of packet", so we've received the start of our data, so reset the array index, and indicate that we've started, but not finished. For the character received itself, there's noting very much else to do, we're no longer interested in it.
« Last Edit: May 01, 2012, 04:00:14 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
why using buadrate 57600, is 9600 not fast enough?
Not any more. 20 years ago, it was. Barely.

Quote
does Serial.read() only return 1 char at the time? if so.. did you solve that by making the var inData an Array instead of a regular char?
Yes, and yes (except that it is array, not Array.

Quote
whats the difference between an array and Array?
One is correct terminology. The other is not.

Quote
what excactly does "if(inChar == SOP) " compare inChar too?
The value #defined as SOP:
Code:
#define SOP '<'

Quote
but later on the variables are used nowhere, why would u even compare than?
Which variables are not used elsewhere? The variables that are reset are index, inData, started, and ended, all of which ARE used later.

Quote
furthere more, this is a difficuld way to me for compiling the serial data to a const char*, isn't there a simpler way?
Certainly. Head over to Gigs and Collaboration, and hire someone to do the programming for you.
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh, i get it now..
but cant i do the same thing in a for-loop?

so the define gives a name to '<' and '>'
but what happens if you compare a variable to '<' ..like:
if(varName == <){}
what does '<' stand for?

edit: btw, can i assign inData to msg now with your exaple? like:
Code:
const char* msg = inData[*];
« Last Edit: May 02, 2012, 07:12:10 am by nickn4 » Logged

humankind cannot obtain anything without
sacrificing something of equal value.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but cant i do the same thing in a for-loop?
I think a clue to the answer to that question comes from the observation that an experienced programmer didn't use a for loop.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

please note my edit..

what do you mean by that?
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The compiler has already told you it doesn't understand this
Code:
const char* msg = inData[*];
, and neither do I - what are you trying to do?

I was saying that, if PaulS could have done all that with a for loop, don't you think he would have?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am trying to get the incomming serial message into msg. because msg is the variable wich will be sended over the RF.
and yes you have a point there. i geus i just dont understand what to do with the example, even if i would copy everything into my code, it woundn't change anything, or am i wrong?
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are clearly struggling with basic programming concepts and really should spend some time focusing on just learning the fundamentals of C/C++.

If you had those fundamentals, you'd know that char* msg and char inData[80] aren't nearly as disimilar as they appear, and that you can in fact use inData in exactly the same way you would use msg (with the one caveat that inData is, effectively a const pointer and cannot be reassigned).  So there's really no point in trying to assign inData to msg (or even having a separate msg variable at all).

There's an abundance of C/C++ tutorials on the web.  Trying to write code without knowing the basics is like trying to build a house without a foundation.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i am trying to get the incomming serial message into msg. because msg is the variable wich will be sended over the RF.
There is NOTHING magic about the name msg. ANY character array that is properly NULL terminated, as inData would be, is suitable input to any function that expects a const char * variable.

What is sending the serial data to the Arduino? Do you have any control over that process? If so, you need to make that process send "<your data>". The < and the > define the start and end of the packet. The code I posted will store everything between the < and the > in inData, if there is room. You can then send inData using the radio sending code.
Logged

Pages: [1] 2 3   Go Up
Jump to: