Hi,
I want to ask if it is possible to use Stringreplace () function with serial communication? I am sending data over bluetooth ant I need to change the question mark to minus sign.
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(115200);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
inByte = Serial1.read();
//Serial.write(inByte);
String stringOne = inByte;
String stringTwo = stringOne;
stringTwo.replace("?", "<-");
Serial.println("Modified string: " + stringTwo);
}
}
I am using this code and getting this error in line 10: error: invalid conversion from 'int' to 'const char*'. I don't know what I have to change that the code would work or even it's impossible to use this function in this situation.
It won't compile either if I declare inByte as a char... Now it's saying error: invalid conversion from 'char' to 'const char*'. Is this means that Stringreplace function doesn't work in serial communication?
It won't compile either if I declare inByte as a char.
What won't? You made a code change. You need to post your revised code.
Now it's saying error: invalid conversion from 'char' to 'const char*'.
It's saying a lot more than that. It's telling you what line in the code has a problem, but you are not telling us that, or what is on that line.
Lacking that information, all I can suggest is that you fix that line.
Is this means that Stringreplace function doesn't work in serial communication?
It means nothing of the sort.
Of course, one assumption you are making that is incorrect is that Serial.read() returns a string. It does not. It returns ONE character. Handling that ONE character as a String is a huge waste of resources.
char inByte;
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(115200);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
inByte = Serial1.read();
//Serial.write(inByte);
String stringOne = (inByte);
String stringTwo = stringOne;
stringTwo.replace("?", "<-");
Serial.println("Modified string: " + stringTwo);
}
}
And this is an error:
mikroschema.ino: In function 'void loop()':
mikroschema:13: error: invalid conversion from 'char' to 'const char*'
mikroschema:13: error: initializing argument 1 of 'String::String(const char*)'
Sorry, my mistake:) Now it's changing char's , but in serial monitor it's showing only one char at the time, when I need all string...
Modified string: E
Modified string: S
Modified string: T
Modified string: A
Modified string: R
Modified string: T
Modified string: X
Modified string: :
Modified string: -
Modified string: K
Modified string: A
Modified string: S
Modified string: T
Modified string: A
Modified string: R
Modified string: K
Modified string: S
PaulS:
Of course, one assumption you are making that is incorrect is that Serial.read() returns a string. It does not. It returns ONE character. Handling that ONE character as a String is a huge waste of resources.
You need to add each single character from Serial.read() to a String, or better still to a string, until the input is complete. How many characters are you expecting to receive and/or is there a delimiter at the end that would allow you to determine that the input is complete ?
If you look at Nick Gammon's code it looks for a newline character ('\n') to signal the end of the input. Can you see what Paul was getting at with his question ?
Nick's code would need to be adjusted not only to look for the terminating 'E' but also to add it to the string as well as to add the terminating null.
The stringreplace() function worked fine. This is the code:
char inByte;
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(115200);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
inByte = Serial1.read();
String stringOne(inByte);
stringOne.replace("?", "-");
Serial.print(stringOne);
}
}
The string looks like that: "STARTASX:9.61511Y:-0.99599K:6.79433Skristi:0AUKSTIS:21E" . Maybe someone can tell me function that will extract all numbers? Before this I worked with Serial.parseFloat() function, but it doesn't work with strings...
You could use code like below (modified from using a comma as a delimiter to using an E) to capture your String, then work with the String in the "do stuff" part of the code. You can test by copy/pasting your string into the serial monitor and see if it gets captured and echoed back.
//zoomkat 3-5-12 simple delimited 'E' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == 'E') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
mantazzz:
I need t extract a float numbers not the whole string...
Simple algorithm:
declare a char array large enough to store the float values
declare and set floatStarted to false
If a char is available in serial
read the char
if the char is a number, '-' or '.'
put it in the next spot in the char array
set floatStarted to true
else if floatStarted is true
convert the char array to a float.
do something with the float
You may also need a variable to keep track of which float value it is.
I see You are a good programer and I want to ask You for some help. I am doing my bachelor's project, but my c++ knowledge is very poor, so maybe You can explain for me how can I extract floats from the string? Here is my code:
char inByte;
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(115200);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
inByte = Serial1.read();
String stringOne(inByte);
stringOne.replace("?", "-");
}
}
there is a data sentence ("STARTASX:9.61511Y:-0.99599K:6.79433Skristi:0AUKSTIS:21E") coming in Serial and I need to extract all numbers... Please, help me somebody, because my ass is on fire...
maybe You can explain for me how can I extract floats from the string?
You don't have a string. You have a String that contains 1 character. That ONE character does not, in any way, represent a float. So, the short answer is that you can't.
You have been told before to look at some examples for reading serial data. That string you are trying to read needs to be read until the end of the packet is found. That E, that is.
Until you learn to read and store the string correctly, you can not proceed with the parsing of the string or the conversion of the tokens to ints or floats.