user11576: I have a file with numbers called fginputs.txt. For example it could be something like this:
4958
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4958

I want python to send each number to arduino via serial port. After each number is received, arduino should print back an acknowledgment number, indicating that it got a valid number, and then store that number in a dynamic array, because I could create larger files. When there are no more numbers left, send a '-1' to finish transmission.
Here's my arduino code:
// save some unsigned ints 
uint16_t SIZE, *inputList, cont = 0;
boolean inputsReady = false;

void setup()
{
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
  
 //free dynamic array memory
 free(inputList); 
 //counter for how many numbers I've received starts at 0
 cont = 0;
 //This is true when there are no more numbers to receive, meanwhile false
 inputsReady = false;
 setupInputList();
 }

/* If there's not enough space, resize the array by one unit and store the number 
   */
void growAndInsert(int currentSize, int newInt){
    if(currentSize > SIZE)
        inputList = (uint16_t *)realloc(inputList, (currentSize + 1)*sizeof(uint16_t));
    inputList[currentSize] = newInt;
}
/**
  init inputList with 100 blocks
 */
void setupInputList(){
  SIZE = 10;
  inputList = (uint16_t *)malloc(sizeof(uint16_t) * SIZE);
}

void clearBuffer(){
while(Serial.available() > 0)
    Serial.read();
}

/**
Listens in serial port for an integer that represents a new input and returns it.
If it doesn't get anything useful from serial, return 0
*/
  int getNewInputFromSerial(){
   if (Serial.available() > 0) {
    delay(100);
    // look for the next word 
    int cmd = Serial.parseInt();
    clearBuffer();

    if(cmd == 4958)
        Serial.write("4");
    else if(cmd == 4154)
        Serial.write("5");
    else
        Serial.write("0");
    return cmd;
   }
  return 0;

}

void loop()
{
     if(!inputsReady){
        int newInput = getNewInputFromSerial();
        if(newInput == 0)
            return;
        if(newInput != -1)
            growAndInsert(cont++, newInput);
        else{
            inputsReady = true;
            //initTimer(); 
        }
    }


}

and the python script:
global arduino
PORT = '/dev/ttyACM0'
FILENAME = "fginputs.txt"
#Read file with inputs
with open(FILENAME) as f:
        content = f.readlines()
#init serial port
arduino = serial.Serial(PORT, 9600, timeout=1);
time.sleep(2);

#write
for input in content:
    arduino.flush()
    arduino.write(input)
    time.sleep(.1);
    resp = arduino.read();
    print "i got " + resp

#Finish transmission with -1
arduino.flush()
arduino.write("-1")
#done
arduino.close();

The fist time I execute the script I get this:
i got 4
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 4

Which is great. But if I run it a second time I get this:
i got 0
i got 4
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0

Which is terribly wrong because the file hasn't changed. I don't know what is going on here. If I unplug and plug back in the USB cable, transmission works flawlessly again.

user7856: You could have a look at this.
It's a python program which does record/playback of key-presses, through an UNO.
It already implements:

serial communication with error checking
ACK: each message has its own ID, so you can keep track of what was lost, should anything bad happen
synchronization, in case your Arduino is powered up after the python program is started


user10794: // save some unsigned ints 
uint16_t SIZE, *inputList, cont = 0;
boolean inputsReady = false;

void setup()
{
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);

 //free dynamic array memory
 free(inputList); 

You are freeing a NULL pointer which is an undefined operation not necessary here, since it was never allocated.

  if (Serial.available() > 0) {
    delay(100);
    // look for the next word 
    int cmd = Serial.parseInt();
    clearBuffer();

This is going to just throw data away, I'm not surprised stuff goes missing.
Read this: How to process incoming serial data without blocking - you need to get rid of the delay, and the clearBuffer() for a start.

Example code for reading from Serial without blocking and without using delay:
// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;

void setup ()
  {
  Serial.begin (115200);
  } // end of setup

// here to process incoming serial data after a terminator received
void process_data (const char * data)
  {
  // for now just display it
  // (but you could compare it to some value, convert to an integer, etc.)
  Serial.println (data);
  }  // end of process_data

void processIncomingByte (const byte inByte)
  {
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line);

      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch

  } // end of processIncomingByte  

void loop()
  {
  // if serial data available, process it
  while (Serial.available () > 0)
    processIncomingByte (Serial.read ());

  // do other stuff here like testing digital input (button presses) ...

  }  // end of loop

In process_data in my example is where you would call atoi to convert the incoming string into an int (or atol if it is going to be longer).

11576: <p>I have a file with numbers called <code>fginputs.txt</code>. For example it could be something like this:</p>
<pre><code>4958
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4958
</code></pre>
<p>I want python to send each number to arduino via serial port. After each number is received, arduino should print back an acknowledgment number, indicating that it got a valid number, and then store that number in a dynamic array, because I could create larger files. When there are no more numbers left, send a '-1' to finish transmission.</p>
<p>Here's my arduino code:</p>
<pre class="lang-cpp prettyprint-override"><code>// save some unsigned ints 
uint16_t SIZE, *inputList, cont = 0;
boolean inputsReady = false;

void setup()
{
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
  
 //free dynamic array memory
 free(inputList); 
 //counter for how many numbers I've received starts at 0
 cont = 0;
 //This is true when there are no more numbers to receive, meanwhile false
 inputsReady = false;
 setupInputList();
 }

/* If there's not enough space, resize the array by one unit and store the number 
   */
void growAndInsert(int currentSize, int newInt){
    if(currentSize &gt; SIZE)
        inputList = (uint16_t *)realloc(inputList, (currentSize + 1)*sizeof(uint16_t));
    inputList[currentSize] = newInt;
}
/**
  init inputList with 100 blocks
 */
void setupInputList(){
  SIZE = 10;
  inputList = (uint16_t *)malloc(sizeof(uint16_t) * SIZE);
}

void clearBuffer(){
while(Serial.available() &gt; 0)
    Serial.read();
}

/**
Listens in serial port for an integer that represents a new input and returns it.
If it doesn't get anything useful from serial, return 0
*/
  int getNewInputFromSerial(){
   if (Serial.available() &gt; 0) {
    delay(100);
    // look for the next word 
    int cmd = Serial.parseInt();
    clearBuffer();

    if(cmd == 4958)
        Serial.write(&quot;4&quot;);
    else if(cmd == 4154)
        Serial.write(&quot;5&quot;);
    else
        Serial.write(&quot;0&quot;);
    return cmd;
   }
  return 0;

}

void loop()
{
     if(!inputsReady){
        int newInput = getNewInputFromSerial();
        if(newInput == 0)
            return;
        if(newInput != -1)
            growAndInsert(cont++, newInput);
        else{
            inputsReady = true;
            //initTimer(); 
        }
    }


}
</code></pre>
<p>and the python script:</p>
<pre class="lang-python prettyprint-override"><code>global arduino
PORT = '/dev/ttyACM0'
FILENAME = &quot;fginputs.txt&quot;
#Read file with inputs
with open(FILENAME) as f:
        content = f.readlines()
#init serial port
arduino = serial.Serial(PORT, 9600, timeout=1);
time.sleep(2);

#write
for input in content:
    arduino.flush()
    arduino.write(input)
    time.sleep(.1);
    resp = arduino.read();
    print &quot;i got &quot; + resp

#Finish transmission with -1
arduino.flush()
arduino.write(&quot;-1&quot;)
#done
arduino.close();
</code></pre>
<p>The fist time I execute the script I get this:</p>
<pre><code>i got 4
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 4
</code></pre>
<p>Which is great. But if I run it a second time I get this:</p>
<pre><code>i got 0
i got 4
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
</code></pre>
<p>Which is terribly wrong because the file hasn't changed. I don't know what is going on here. If I unplug and plug back in the USB cable, transmission works flawlessly again.</p>
 7856: <p>You could have a look at <a href="https://github.com/01org-AutomatedFlasherTester/pem" rel="nofollow">this</a>.</p>

<p>It's a python program which does record/playback of key-presses, through an UNO.</p>

<p>It already implements:</p>

<ul>
<li>serial communication with error checking</li>
<li>ACK: each message has its own ID, so you can keep track of what was lost, should anything bad happen</li>
<li>synchronization, in case your Arduino is powered up after the python program is started</li>
</ul>
 10794: 

<pre class="lang-C++ prettyprint-override"><code>// save some unsigned ints 
uint16_t SIZE, *inputList, cont = 0;
boolean inputsReady = false;

void setup()
{
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);

 //free dynamic array memory
 free(inputList); 
</code></pre>

<p>You are freeing a NULL pointer which is <s>an undefined operation</s> not necessary here, since it was never allocated.</p>

<hr>

<pre class="lang-C++ prettyprint-override"><code>  if (Serial.available() &gt; 0) {
    delay(100);
    // look for the next word 
    int cmd = Serial.parseInt();
    clearBuffer();
</code></pre>

<p>This is going to just throw data away, I'm not surprised stuff goes missing.</p>

<p>Read this: <a href="http://www.gammon.com.au/serial" rel="nofollow">How to process incoming serial data without blocking</a> - you need to get rid of the <code>delay</code>, and the <code>clearBuffer()</code> for a start.</p>

<hr>

<p>Example code for reading from Serial without blocking and without using <code>delay</code>:</p>

<pre class="lang-C++ prettyprint-override"><code>// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;

void setup ()
  {
  Serial.begin (115200);
  } // end of setup

// here to process incoming serial data after a terminator received
void process_data (const char * data)
  {
  // for now just display it
  // (but you could compare it to some value, convert to an integer, etc.)
  Serial.println (data);
  }  // end of process_data

void processIncomingByte (const byte inByte)
  {
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line);

      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos &lt; (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch

  } // end of processIncomingByte  

void loop()
  {
  // if serial data available, process it
  while (Serial.available () &gt; 0)
    processIncomingByte (Serial.read ());

  // do other stuff here like testing digital input (button presses) ...

  }  // end of loop
</code></pre>

<p>In <code>process_data</code> in my example is where you would call <code>atoi</code> to convert the incoming string into an int (or <code>atol</code> if it is going to be longer).</p>
