When I first open a serial connection using gnu-screen. It does not respond to any command I stuff until I have attached to the session and then detached from it again. Then the device receives the characters I am trying to send and functions as expected. The goal is to script opening the port and sending characters to it without needing the user bring the session to the foreground before the connection is live.
Open serial connection using screen
screen -S serialarduino -dm "/dev/cu.usbmodem101"
Then trying to send a character to the device does not work
screen -S serialarduino -X stuff H
Until I attach to it
screen -x serialarduino
Then I can either just close the window or detach from it properly
screen -d serialarduino
Now I am able to stuff characters using the same command as before and everything works
Edit:
I found a roundabout solution but it works. If I open the port as an attached session then detach from it, it works. But since opening attached is blocking, I start a 100ms timer then detach from the screen session in a backgrounded sub shell before opening the connection. It seems like the connection does not accept input for 2 seconds after detaching so we will wait 2 seconds before sending any characters. The down side is that you will see the full-terminal interface to the serial port (looks like you ran the clear command) and a message that you have detached. If you are running this as a process from a UI you can just ignore stdout to discard all of that.
( (sleep .1
screen -d serialarduino
) & ) &> /dev/null
screen -S serialarduino "$serialport"
sleep 2
screen -S serialarduino -X stuff H
Of course once the port is opened subsequent interactions are immediate. Then when you are finished, close the connection with screen -S serialarduino -X quit