Reading and sending data via serial port

Hello guys,
I have to send some data from Matlab to arduino uno via bluetooth. Interfacing isn't problem.
I have to send some numbers from matlab to arduino for example (123), then send same numbers back to matlab. That's not problem, but then my arduino should send me some string for example("end") and after that I should be able send another data from matlab to arduino. I hope you understand what I mean. There I have some problems because I need to send ("end") only once but in my program arduino is sending it in infinitive loop. I tried moved( Serial.write("end"); ) from loop to setup, but it sends me ("end") in the beggining of my program which is wrong. I have there some delays because interfacing via bluetooth is little bit slower.

It has to be like this:

  1. recieve numbers in arduino, immediately send them back
  2. send ("end") from arduino to matlab
  3. and again recieve numbers in arduino, immediately send them back
byte byteRead;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
delay(500);

}

void loop() {
  // put your main code here, to run repeatedly:

  
  
  
  if(Serial.available( )){
  byteRead = Serial.read();
  Serial.write(byteRead);
  
  }
  delay(5000);
  Serial.write("end");
}

How does the Arduino know that the computer has stopped sending the data?

If you’re doing it based on time delay, use millis. Store the time when you receive a character, and test against that to see if you need to send the ending.

Have a look at the second and third examples in Serial Input Basics

It sets newData = true; when a complete message has been received.

If the word "end" is an indication for Matlab to stop then that would be a good place to send it.

If the word "end" is an indication for Matlab to send the next thing then the point where newData is set to false would be a good place for it.

...R

Arduino doesn't know that. In the future arduino supposed to be robot. But that's is doing my mate. My work is to fix communication, that's why I used there delay. And Robin2 I don't know where put "newData = true;" I tried something but with same result. Numbers representing robot 's movement. So I send him 123 and that means 1-go straight, 2-turn left,3-turn right. After robot finish its movement, sends "end" to matlab this "end" I compare in matlab and send him another numbers. I'm able send data from matlab and then recieve them back. I'm able send string "end" from arduino to maltab compare them and then my matlab send numbers to arduino. But when I have to do it in one program it always sends me "end" more than once, so I can't compare it in matlab. Which means my Matlab's program stop running.

st39651:
And Robin2 I don't know where put "newData = true;"

I was not asking you to put it anywhere. I was suggesting you look at where I have it in my examples. Have you studied my examples?

Arduino doesn't know that.

What do you mean by this?

In the grand scheme of things an Arduino does not know anything because it does not have a human brain.

...R

It was reaction on drazzy. I have to look at yours examples once again.

It seems like Arduino reads data gradually. So when I send numbers 123 void loop will run 3 times. That's why I can see in serial monitor (1end2end3end)

st39651:
It seems like Arduino reads data gradually. So when I send numbers 123 void loop will run 3 times. That's why I can see in serial monitor (1end2end3end)

If that is not what you want and you are looking for advice you need to post your latest program.

...R

It's same. When I have only this part of program. In serial monitor show me right numbers (123).

byte byteRead;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly:

  
  
  
  if(Serial.available( )){
  byteRead = Serial.read();
  Serial.write(byteRead);

But when add there the part of the program with the (end). The data in serial monitor looks like this(1end2end3end). So I think At first I need receive whole data and send them back then wait few seconds and send some indicator(end) which I compare in matlab and send him another numbers. So I have to fix somehow loop. Otherwise the data are connected.

byte byteRead;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly:

 
  if(Serial.available( )){
  byteRead = Serial.read();
  Serial.write(byteRead);
  
  }
  delay(5000);
  Serial.write("end");
}

Also when I tried while instead of if. Serial showed me:

end
end
123
end
end
end

So now I have numbers together but I still need send "end" only once and only when I receive something from matlab.

 while(Serial.available( )){
  byteRead = Serial.read();
  Serial.write(byteRead);
  
  }
  delay(20000);
  Serial.println("e");

It's more than 2 weeks since you started this Thread. Please remind us what you are trying to do and what the problem is in relation to your latest code.

My memory is only good for about 24 hrs.

...R

I'm on the phone right now, but I'll try explain IT. The arduino is robot And matlab is controlling system. Now I have to send numbers from matllab to arduino (123) 1- move forward 2- Turn left etc.). Now I have to send same numbers from arduino to matlab. And check if I send correct numbers So matlab has to show 123. After while (arduino complied his jurney And want another numbers which represent another jurney). So I have to send something from arduino to matlab for example string end which I compare in matlab And matlab know that arduino want another numbers. So I send him for example numbers 456 from keyboarr. But problem is i cant compare it end beacase ITS send me 1end2end etc. So I cant send the second numbers for jurney.

st39651:
ITS send me 1end2end etc. So I cant send the second numbers for jurney.

The second program in Reply #8 seems specifically designed to do just that.

Have you tried the examples that I linked to in Reply #2 and the accompanying suggestions?

...R

Ok I tried that. And it doesn’t work. When I tried send numbers from matlab via bluetooth or serial port it showed me only . I didn’t receive any data into matlab, they didn’t even show in serial monitor in arduino. But when I write numbers into serial monitor (in arduino) I recieved

This just in … 123

So it doesn’t work with matlab and still after that I need send “end” so I have to put it somewhere into program. When I tried to put at the end of the program (Serial.println(“end”);)It sends me again end many times not only once.

st39651:
Ok I tried that. And it doesn't work.

If you don't post the program that does not work, how can I help?

...R

I tried Receiving several characters

const byte numChars = 32;
char receivedChars[numChars];	// an array to store the received data

boolean newData = false;

void setup() {
	Serial.begin(9600);
	Serial.println("<Arduino is ready>");
}

void loop() {
	recvWithEndMarker();
	showNewData();
}

void recvWithEndMarker() {
	static byte ndx = 0;
	char endMarker = '\n';
	char rc;
	
	// if (Serial.available() > 0) {
           while (Serial.available() > 0 && newData == false) {
		rc = Serial.read();

		if (rc != endMarker) {
			receivedChars[ndx] = rc;
			ndx++;
			if (ndx >= numChars) {
				ndx = numChars - 1;
			}
		}
		else {
			receivedChars[ndx] = '\0'; // terminate the string
			ndx = 0;
			newData = true;
		}
	}
}

void showNewData() {
	if (newData == true) {
		Serial.print("This just in ... ");
		Serial.println(receivedChars);
		newData = false;
	}
}

I also tried program which receive and send all numbers and tried add there function sendstring but it waits 20 sec and sends me. But I need to receive in matlab numbers first. Can I add somehow condition for example if serial write = successful send (end)?
end
123

end

void setup (){
  Serial.begin(9600);
  Serial.flush();
  digitalWrite (13, HIGH);      //turn on debugging LED
}

void loop (){

  int i=0;
  char commandbuffer[100];

  if(Serial.available()){
     delay(100);
     while( Serial.available() && i< 99) {
        commandbuffer[i++] = Serial.read();
     }
     commandbuffer[i++]='\0';
  }

  if(i>0)
     Serial.println((char*)commandbuffer);
     
sendstring();
}

void sendstring(){
  
  delay(20000);
  Serial.println("end");

  
}

You seem to have posted two different programs in Replies #15 and #16 but in neither case have you told us what those programs actually do and what you want them to do that is different.

Please try and think about the information that I require in order to help you. Don't be afraid of providing too much info.

As soon as I post this I am going to look at another problem so I don't remember all the stuff about your problem.

...R

#15 Is program from your Replie #3. #16 Is my new program which reads and sends whole data together. I tried add there only another function which send me string (end). But it sends me frist end a then the data which I need. I need it vice versa.

In the program in Reply #15 try changing the function showNewData() to the following and tell me what happens and whether that is what you want to happen. There is just one extra line.

void showNewData() {
	if (newData == true) {
		Serial.print("This just in ... ");
		Serial.println(receivedChars);
		Serial.println("end");  //  <===========NEW
		newData = false;
	}
}

This is what I suggested you should do in Reply #2

…R