Good evening,
i need to read a char from serial and the compose an array of char.
What I see now that arduino received on char of time then I need to build an array of char for have the word.
below reported the simple sketch:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.print((char)incomingByte);
}
}
I think is possible use that :
stream.readBytesUntil(character, buffer, length)
but didn't know ho to use it,
Could you give a simple sketch for build an array from incoming data on serial ?
assuming you can write up the variable, array definitions, void setup() to go with this:
x=0;
void loop(){
if (done_receiving == 0){ // more data expected
if (Serial.available()>0){
incomingArray [x] = Serial.read();
if (incomingArray [x] == done_character)
{ done_receiving = 1;
x = 0;}
else {x=x+1;}
} // end serial available
} // end done check
} // end void loop
Good Morning, firstly I’d like to say thanks you for the support
so I’ve tried to do in this way …
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
}
int x = 0;
int done_receiving;
void loop(){
char incomingArray[128];
if (done_receiving == 0)
{ // more data expected
if (Serial.available()>0){
incomingArray[x] = Serial.read();
if (incomingArray[x] == '\0')
{
done_receiving = 1;
x = 0;
}
else
{
x=x+1;
}
Serial.println(incomingArray);
incomingArray[0] = '\0';
} // end serial available
} // end done check
} // end void loop
but when I open the arduino monitor and try to type " test " my expectation is at the end to have printed “test” again but I received it:
you need to declare char incomingArray[128]; outside loop() otherwise you get a new instance every time loop is called.
quick refactor, give it a try ( not tested)
int incomingByte = 0; // for incoming serial data
int x = 0;
bool moreData = true;
char incomingArray[128];
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (moreData)
{
if (Serial.available()>0)
{
incomingArray[x] = Serial.read();
if (incomingArray[x] == '\0' || x == 127 ) // how to send a 0 char ? + added test array full !
{
moreData = false;
x = 0;
}
else
{
x++;
}
}
}
else // no moreData
{
Serial.println(incomingArray);
incomingArray[0] = '\0';
}
}
i need to read a char from serial and the compose an array of char.
You may want to just capture the individual chacters and form them into a string, which is a fairly simple operation. Below is a very simple example where a string is sent from the serial monitor and captured by the arduino, then sent back to the serial monitor.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}