2

Arduino send data to PC each iteration. When I'm trying to send data to Arduino while receiving data from it, it takes too long. Serial.available() returns false even I sent data from PC. But if I add delay like 20-30 ms, problem fixed. Can't I use serial simultaneously?

PS: It's Arduino mega and I'm using http://www.teuniz.net/RS-232/ library for serial communication.

Example code:

void loop(){
 if(Serial.available())
 Serial.write("goodbye", 7);
 Serial.write("hello", 5);
 }

In this situation, it takes long time to write "goodbye" after I sent data to Arduino.

asked Dec 21, 2016 at 0:18
5
  • Please edit your question and post your code. Format the code using the code formatting markdown (four leading spaces). For help see Markdown help. You should be able to do this by selecting the code and pressing Ctrl+K to have your browser do this for you. Commented Dec 21, 2016 at 0:33
  • @Nick Gammon, done. Commented Dec 21, 2016 at 0:44
  • 1
    That's not all your code is it? What baud rate are you using? In any case, since you are sending "hello" every time around loop the output buffer will be filled with hello before you get a chance to insert goodbye. Your "delay" solution probably gives the output buffer time to empty. Commented Dec 21, 2016 at 4:25
  • @Nick Gammon 9600. Commented Dec 21, 2016 at 9:47
  • And I lid a led when receive message. I forget to write it. Ill add full code when Im open PC. I dont have full code by now. Commented Dec 21, 2016 at 9:49

2 Answers 2

1

Following up on what Nick Gammon says, don't try to use the serial output to debug serial. Perhaps try using the built-in LED instead; you can use an oscilloscope to find out how much time it is spending reading characters.

The following is how I've been reading a line at a time using non-blocking operations:

const int SERIAL_BUFFER_LENGTH = 100;
// The +1 assures we'll always have room for null termination
char g_serial_buffer[SERIAL_BUFFER_LENGTH+1];
int g_char_count; // index into serial buffer
void setup() {
 Serial.begin(9600); // or 115200 if your host supports it
 pinMode(LED_BUILTIN, OUTPUT);
 digitalWrite(LED_BUILTIN, LOW);
 g_char_count = 0;
}
void loop() {
 while(Serial.available() > 0) {
 digitalWrite(LED_BUILTIN, HIGH);
 char c = Serial.read(); // won't block because Serial.available() > 0
 if (c == '\n') {
 g_serial_buffer[g_char_count] = '0円'; // null terminate
 process_buffer(g_serial_buffer);
 g_char_count = 0;
 } else if (g_char_count < SERIAL_BUFFER_LENGTH) {
 g_serial_buffer[g_char_count++] = c;
 }
 }
 digitalWrite(LED_BUILTIN, LOW);
 Serial.print(".");
}
// process_buffer: do whatever it is you intended to do with a 
// line of received text...
void process_buffer(char *buffer) {
}
answered Dec 21, 2016 at 6:38
0

Try this:

 if(Serial.available())
 while(Serial.read());
 Serial.write("goodbye", 7);
 Serial.write("hello", 5);
 }

I think you have to read what you get in the serial port.

answered Sep 8, 2017 at 10:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.