Multiple scripts talking to a single arduino in C++

I am using a python script that sends a single character to the arduino to make a LED light up, blink, or stop. I am able to open many of these scripts and each can signal to the arduino whitout problem. However, in C++ using libserial, i am unable to replicate this, only one script can access the arduino at a time (even while many python scripts are still accessing it!). I am curious as to what i am not getting, or the difference between the two, and how i can change my C++ program to also have them be able to signal the arduino simultaneously.

Arduino code:

const int ledPin =  4;
int ledState = LOW;
unsigned long previousMillis = 0;
long interval = 100;     // ************ removed const

char state;
boolean newData = false;
boolean ledBlink = false;
boolean runLed = false;

void setup()
{
   Serial.begin(9600);
   Serial.println("LED_READY");
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   recvOneChar();
   if (newData == true)
   {
      if (state == 2) //run led
      {
         runLed = true;
         newData = false;
         ledState = HIGH;  // turn led on
         ledBlink = false;
      }
      else if (state == 3) //run blink
      {
         runLed = true;
         newData = false;
         ledBlink = true;
      }
      else if (state == 1) // turn off led
      {
         runLed = false;
         ledState = LOW;  // turn led off
         newData = false;
         ledBlink = false;
      }
   }
   unsigned long currentMillis = millis();
   if (currentMillis - previousMillis >= interval && runLed == true && ledBlink == true)
   {
      previousMillis = currentMillis;
      if (ledState == LOW)
      {
         ledState = HIGH;
         interval = 100;   // ********** sets on time
      }
      else
      {
         ledState = LOW;
         interval = 100; // ********** sets off time
      }
   } //end if
   digitalWrite(ledPin, ledState);
}

void recvOneChar()
{
   if (Serial.available() > 0)
   {
      state = Serial.parseInt();
      newData = true;
   }
}

Python code example:

try:
    ser = serial.Serial(
    port=serialdevice,\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
    timeout=0)
    line = []
    for c in ser.read():
        if c != '\n':
            line.append(c)
        if c== '\n':
            strline=''
            for i in line:
                strline=strline+i
            print strline
    print 'LED connected'
except Exception as e:
    serial_alert = False
    print 'No serial device found'
    print e

while(something_or_other):
   #some code
   if condition:    ser.write(b'1\0')

#many minutes or hours later, when finished or told to shut down
ser.close()

C++ code segment

#include <SerialPort.h>
my_serial_port.Open( "/dev/ttyUSB0" ) ;
my_serial_port.SetBaudRate( BaudRate::BAUD_9600) ;
my_serial_port.SetCharacterSize( LibSerial::CharacterSize::CHAR_SIZE_8 ) ;
my_serial_port.SetFlowControl( LibSerial::FlowControl::FLOW_CONTROL_NONE ) ;
my_serial_port.SetParity( LibSerial::Parity::PARITY_NONE ) ;
my_serial_port.SetStopBits(LibSerial::StopBits::STOP_BITS_1) ;
const int BUFFER_SIZE = 9;
string input_buffer;
my_serial_port.Read( input_buffer, BUFFER_SIZE );
if (input_buffer == "LED_READY") {
    cout << "Serial Alarm Connected" << endl;
}
while (something_or_other) {
    if (condition) {my_serial_port.Write("1\0");}
}
//when finished, many minutes or hours later
my_serial_port.Close();

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags, not quote tags.

The way that I read your post, your Arduino needs more than one serial port. Is that right? How many serial ports do you need?

Arduinos do not do more than one things simultaneously. They can do thing after another so quickly that it looks, to a human, to be simultaneous.

A serial port can't be shared on a PC; only one application can use it at a time. So I'm very surprised that you state that you can have multiple python scripts connecting to the Arduino at the same time.

I'm not a python programmer; what happens if you put the equivalent of a delay(10000) just before your ser.close() in your python scripts and next try to run them simultaneously? It would keep the serial port open for 10 seconds in one python script and during that time other python scripts should fail.

Please edit your post and replace the quote tags ([quote] and [/quote]) by code tags ([code] and [/code]).

Sorry, forgot the code tags.

I'm not a python programmer; what happens if you put the equivalent of a delay(10000) just before your ser.close() in your python scripts and next try to run them simultaneously?

Nothing. On my python script, all 4 (they're camera motion detectors based on openCV, usually i run 4 copies) scripts are all using /dev/ttyUSB0 at the same time and in over a year of use with them running for hours or days i haven't ever had an error or crash from one blocking the others. ser.close() is only called when the program is told to shut down, to cleanly close the port, it's never called inside while running. Although i don't think more than one have tried to send at the exact same milisecond so maybe that could cause a crash, but in this application it has never happened.

The way that I read your post, your Arduino needs more than one serial port. Is that right? How many serial ports do you need?

Arduinos do not do more than one things simultaneously. They can do thing after another so quickly that it looks, to a human, to be simultaneous.

Well i don't know, it's been working fine with one serial port for the python version of my program. I can't explain why this has been possible in python. Could it be the python pyserial library actually not opening the port until it's actually reading or writing, while the C++ libserial library does? I don't know, i'm not experienced with serial programming.

heckingarduinerino:
Could it be the python pyserial library actually not opening the port until it's actually reading or writing, while the C++ libserial library does? I don't know, i'm not experienced with serial programming.

I suspect that's the case. A lot of times when you open a handle to a device, you're locking out other threads from accessing it until you let it go. The python library is probably only locking the serial handle when it's transmitting and releasing it right away, while the C++ library is holding it open for the entire duration of it's lifetime.

How can multiple scripts read though? Does the data get sent to all of them, or randomly to different scripts?

Jiggy-Ninja:
I suspect that’s the case. A lot of times when you open a handle to a device, you’re locking out other threads from accessing it until you let it go. The python library is probably only locking the serial handle when it’s transmitting and releasing it right away, while the C++ library is holding it open for the entire duration of it’s lifetime.

How can multiple scripts read though? Does the data get sent to all of them, or randomly to different scripts?

They only read when the port is opened to confirm it’s open, otherwise it’s a one-way signal to make the LED flash or stop