Problem during Serial communication

I have two problems

  1. The first character I write on Serial is not backed properly and is ~4seconds long
  2. After long communication (in this example after “+”) characters are not backed properly (Arduino returns empty character “”) and also it takes ~4s for every next character.



.ino file:

#include <Servo.h>

typedef struct {
  //int ascii_key;
  byte servo1_pos;
  byte servo2_pos;
} keyStat;
keyStat keyStats[1] = {
  {0,0}, // ASCII 0 - NULL
  {0,0}  // ASCII 1 - SOH
const byte N = 126;
keyStat keyStats[N];

Servo servo1; Servo servo2; Servo servo3;

void setup()
  for(byte i=0; i<N; i++)
    keyStats[i] = {0,0};
  servo1.attach(9);  // ramie1 - zdefiniowanie do którego portu podłączone jest serwo
  servo2.attach(10); // ramie2
  servo3.attach(11); // siłownik


void loop()
  if (Serial.available())
    byte ch =;
    print_key_info(ch, 0);
    move_arm( keyStats[ch] );
    print_key_info(ch, 1);
    //Serial.println("Arduino:: Waiting for key...");

void move_arm(keyStat key){
  move_servo(servo1, key.servo1_pos);
  move_servo(servo2, key.servo2_pos);

void move_servo(Servo servo, byte key_pos){
  byte pos =;
  if(key_pos == pos) return;
  if(pos > key_pos){
    for(pos; pos >= key_pos; pos--){
    for(pos; pos <= key_pos; pos++){

void move_servo3(void){

void calibrate(void){

void print_key_info(byte ch, bool done){
    //Serial.print("Arduino:: Key pressed: ");
    //Serial.print("Arduino:: Processing to key: ");

void print_key_state(int ch){
    //Serial.print((char) ch);
    //Serial.print(" // code: ");


.python script:

#!/usr/bin/env python
# Send data to an Arduino and report responses.
from time import time, sleep, asctime, localtime
from serial import Serial
import sys, tty, termios

print 'Opening serial port'
ser = Serial('/dev/ttyACM0', 9600, timeout=3) #Where /dev/ttyXYZ is port to which Arduino connects
print 'Opening text file'
sleep(1)                      # Let board initialize  
with open("text_file", "r+") as f:
        for line in f:
                for c in line:
                    txt = c    
                    #txt = raw_input('>>') # Used for file_sender < text_file
                    if '\n' in txt:
                        print 'Sending ENTER'
                        print ord(txt)
                    elif '\t' in txt:
                        print 'Sending TAB'
                        print ord(txt)                      
                    elif ' ' in txt:
                        print 'Sending SPACE'
                        print ord(txt)  
                        print 'Sending  "{}"'.format(txt)
                        print ord(txt)
                    s =          # Get result from arduino (DEBUG)
                    print 'Readback "{}" at {}'.format(s, asctime(localtime()))

What wrong here?

Opening serial port
Opening text file
Sending  "a"
Readback "" at Tue Dec  6 15:48:42 2016
Sending  "b"
Readback "b" at Tue Dec  6 15:48:43 2016
Sending  "c"
Readback "c" at Tue Dec  6 15:48:44 2016
Sending  "d"
Readback "d" at Tue Dec  6 15:48:45 2016
other characters from text_file received properly (1 per second)
Sending ENTER
Readback "
" at Tue Dec  6 15:50:07 2016
Sending  "~"
Readback "~" at Tue Dec  6 15:50:08 2016
Sending  "_"
Readback "_" at Tue Dec  6 15:50:09 2016
Sending  "+"
Readback "" at Tue Dec  6 15:50:13 2016
Sending  "{"
Readback "" at Tue Dec  6 15:50:17 2016
Sending  "}"
Readback "" at Tue Dec  6 15:50:21 2016
Sending  "|"
Readback "" at Tue Dec  6 15:50:25 2016
Sending  ":"
Readback "" at Tue Dec  6 15:50:29 2016
Sending  """
Readback "" at Tue Dec  6 15:50:33 2016
Sending  "<"
Readback "" at Tue Dec  6 15:50:37 2016
Sending  ">"
Readback "" at Tue Dec  6 15:50:41 2016
Sending  "?"
Readback "" at Tue Dec  6 15:50:45 2016
Sending ENTER
Readback "" at Tue Dec  6 15:50:49 2016
Sending ENTER
Readback "" at Tue Dec  6 15:50:53 2016

Look on the beginning and “a” and every character starting from “+”. Big delay and readback “”.

Thanks for help.

I could be missing something, but it does not appear that the keyStats array every holds anything but 0s. So, every call to move_arm() is going to do the same thing, which is waste 45 milliseconds. Plus, you waste another 50 milliseconds every time there is serial data/you call move_arm().

For someone concerned about speed, your code is just littered with unnecessary delay()s.

Opening the serial port resets the Arduino, so a delay before the first character is responded to is not unexpected.

sleep(1)                      # Let board initialize

The Arduino takes longer than that, while to bootloader looks for incoming serial data that looks like a program, to be assured that it is run the existing sketch.

Dumping your results in the rubbish bin is rarely a good idea. There IS a mechanism for posting code, pictures, and data HERE that you should use if you want help HERE. The mechanism is described in the stickies that you were supposed to read before posting here.

I know there are not needed delays and keyStats is initialized do {0,0} on every index. But it isn't the problem here. I am not yet using it and I don't have correct data to initialize it properly. I put output to pastebin because it is long and I add it for visualization. I desribed the output so looking there is not needed but I make it possible for someone who want to.

After maning delay longer in script I solved the first problem but the second one still exists and it is after bootloading phase.

After maning delay longer in script I solved the first problem but the second one still exists and it is after bootloading phase.

Seeing your output might help with solving the problem. But, I can't access pastebin.

No problem I fixed my first post if it is helpful for you.

The Arduino has a 63 byte buffer; once that is full, you will loose data. With your delays, there is a very good chance that that happens quite quickly.

Your send at 9600 baud, so it takes 1 ms to transmit a byte. You add 1ms between bytes.

So in 95 ms (the delays in the Arduino code after receiving a character) you will have received 47 or so other bytes. While the Arduino is processing the second byte that it received, the buffer will be full.

Okay. Any advices how to setup, manage it correctly? I have deleted delays but still I encounter it for different text in different situations.

Have a look at the examples in Serial Input Basics - simple reliable, non-blocking ways to receive data.