Convert char buffer to String

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using rcswitch library for sending and receiving data. In rcswitch library, there is no method to send string data. I am using below method to send the string message in char buffer.

 

 

send_string("Hello World!");
---------------------------

void send_string(const char *str)
{
int i = 0;

for (char *p = str; *p; p++ ) {
mySwitch.send((i<<8) + *p, 16);
i++;
}
mySwitch.send((i<<8) + 255, 16);
}

 

And I am receiving is using this Serial.print((char)mySwitch.getReceivedValue()); build in method. But I am getting this message.

 

H
H
H
e
e
e
l
l
l
l
l
l
o
o
o



W
W
W
o
o
o
l
l
l
d
d
d
!
!
!

Now how can I get my original message?

This topic has a solution.
Last Edited: Thu. May 24, 2018 - 10:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Somewhere, a carriage return is being added after each character.

 

It has to be either in the sender or the receiver. 

 

What are you receiving with?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am receiving by calling this method.

 

(char)mySwitch.getReceivedValue();

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isn't that standard behaviour of the Arduino Serial.print() ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

chayanforyou wrote:
I am receiving is using this Serial.print ...

That doesn't make sense!

 

Serial.print() does not receive - it sends !

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need something like this. > https://stackoverflow.com/a/4474...

Here they are converting Hex array to String. But I need to convert char array to String.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In C, a string is nothing more than the array of characters you want to send followed by '\n' and '\0' (0x0A followed by 0x00).

 

So take your character array but make it two characters bigger than you need and insert those values in the last two positions. Viola, you now have a string.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Last Edited: Thu. May 24, 2018 - 07:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you share a sample code?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
In C, a string is nothing more than the array of characters you want to send followed by '\n' and '\0' (0x0A followed by 0x00).

The terminating NUL (0x00) is a 'C' string thing - but the \n is not.

 

The \n has nothing specifically to do with 'C' - if you want a newline character on the end of your string, then put it there; if you don't - then don't.

 

 

EDIT

 

And note that various different systems have different ways to denote a "new line"

  • Some just use Line Feed - aka "LF" or \n - ASCII 0x0A
  • Some just use Carriage Return- aka "CR" or \r - ASCII 0x0D
  • Some use the 2-character sequence Carriage Return followed by Line Feed - aka "CRLF"

 

Also some systems will translate \n to the specific "new line" sequence when you do a "print" ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. May 24, 2018 - 07:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is basic 'C' stuff - nothing specifically to do with AVR or Arduino.

 

Any 'C' textbook will have a section - with examples - on strings.

 

Here are some 'C' learning & reference materials: http://blog.antronics.co.uk/2011... - including a free online textbook.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can anyone share the sample code for Sending and Receiving String message using rc switch library?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SErial.print does not add \n; https://www.arduino.cc/en/Serial...

Serial.println does . https://www.arduino.cc/en/Serial...

 

One should distinguished what is received from rc into arduino, en then sent to a human

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You mean this: https://github.com/sui77/rc-switch ?

 

If so, have you studied the Wiki: https://github.com/sui77/rc-switch/wiki ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you try (nobody can guess ) rc switch examples https://github.com/sui77/rc-swit...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes, I tried it but it doesn't support send and receive string message.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

So, if that's what you want to do - why did you choose this library?

 

Look at the description of the library:

 

RC Switch Wiki wrote:
This library allows you to copy and clone the radio signals sent out and received by most popular low-cost power outlet sockets and light switches.

So it is designed just for simple switch control - it is not designed for sending text strings!

 

If you specifically want to send text strings, then find a library which does that!

 

But you still need to brush-up on your 'C' string basics ...

 

EDIT

 

quote

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Brian Fairchild wrote:
In C, a string is nothing more than the array of characters you want to send followed by '\n' and '\0' (0x0A followed by 0x00).

The terminating NUL (0x00) is a 'C' string thing - but the \n is not.

 

My bad, I looked at a diagram in K&R to double check the definition of a C string but didn't read the accompanying text.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To be pedantic, I guess one could say that the 'C' language itself has no concept of a "string" at all:

 

  • There is no specific "string" type;
  • There are no specific keywords that relate to "strings";
  • There are no specific operators that relate to "strings".

 

The only thing that the 'C' language itself knows is the char array - the language itself  neither knows nor cares whether the programmer might be using it to hold a "string".

 

But the standard 'C' Library does define a set of functions that work with NUL-terminated char arrays - calling them "strings".

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just googled "rcswitch" and ended up here:

 

https://github.com/sui77/rc-swit...

 

 

Two things about that are interesting.

 

1) First is that the version of .send() you are using appears to take a "length" - so the ", 16" you are passing is a length - but that seems a very odd value. Does it mean 16 bytes or 16 bits. Either seems wrong. You are sending "char" so they are 8 bit and if it really meant 16 BYTES that would be seriously wrong.

 

2) far more interesting is line 81. So you can simply use:

mySwitch.send("Hello World!");

and that will send the whole thing in one go. You don't have to mess about with for()/while() loops to send character by character as the author already thought of this.

 

This highlights the joys of C++. Often a function like send() (or in Arduino println()) will be "overloaded" and be able to send multiple types of data.

 

You can see the implementation they use here:

 

https://github.com/sui77/rc-swit...

 

Oh and this:

 

https://github.com/sui77/rc-swit...

 

confirms that in the send(code, len) variant that the "len" is "bits". So it still seems odd that you would be choosing to send 16 bits to actually transmit 8 bit characters - but perhaps there's something about this radio system I don't know?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

To be pedantic, I guess one could say that the 'C' language itself has no concept of a "string" at all:

...

 

 

Hmmm, string literals?

 

From K&R...

Quote:

A string literal, also called a string constant, is a sequence of characters surrounded by double quotes, as in "...". A string has type "array of characters" and storage class static and is initialised with the given characters.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There's always one ...

 

frown

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

There's always one ...

 

frown

 

cheeky

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
perhaps there's something about this radio system I don't know?

From what I saw, it looked like they were encoding a 1 bit by  the character '1' and a zero bit by the character '0'.

There also seemed to be a tri-state option with 'F' to indicate a "Hi-Z" bit ... ?

 

So "1001FF01" is:

 

Bit 7 (MSB) is 1
Bit 6 is 0
Bit 5 is 0
Bit 4 is 1
Bit 3 is Hi-Z
Bit 2 is Hi-Z
Bit 1 is 0
Bit 0 (LSB) is 1

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have found my solution.

Thanks all.

 

https://arduino.stackexchange.co...

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

chayanforyou wrote:
I have found my solution.

Jolly good.

 

Now please mark the solution in this thread - see Tip #5.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

chayanforyou wrote:

I have found my solution.

Thanks all.

 

https://arduino.stackexchange.co...

I particularly enjoyed this bit:

Oh, wait! That's exactly what you are already doing! So it appears that you are already aware of this issue and you have even implemented a solution. How come you are asking this question then?

 

OK, I'll assume in the following that you just forgot to tells us that the code snippet above is not from you, and that you didn't even understand what it is doing. And you also forgot to provide a link to the source of that code. Please, next time read How do I ask a good question? before asking, and provide all the relevant information right in the question.

So surly curmudgeons like me are not limited to Freaks alone?

 

;-)