Electronic – arduino – Applescript to Arduino LCD only works with Serial Monitor

arduinocharacter-lcdlcdoledserial

I spent all day trying to figure this out and finally got somewhere that atleast lets me know that I am doing something weird…

I am trying to send data to my LCD Screen on my Arduino Uno, but for some reason it only works if serial monitor is open first then I try to run my applescript…

So, this is my Arduino Code…

#include <Adafruit_CharacterOLED.h>
Adafruit_CharacterOLED lcd(6, 7, 8, 9, 10, 11, 12);

String inData;


void setup() 
{
  // Print a message to the LCD.
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
char TestData='X';
lcd.print(TestData);
}

void loop() {
while (Serial.available() > 0)
{
    char recieved = Serial.read();
    inData += recieved; 

    // Process message when new line character is recieved
    if (recieved == '\n')
    {
        lcd.setCursor(0, 0);
        lcd.print(inData);

        inData = ""; // Clear recieved buffer
    }
}
}

If I open my serial monitor, and type something, it shows up on my Screen…Great

So now if I run this script, from applescript with the SerialPort X Plugin installed…

set portRef to serialport open item 1 of (get serialport list)
if portRef is equal to -1 then
display dialog " could not open port "
else
serialport write "text  " to portRef
delay 1
serialport close portRef
end if

if I try to use the actual port name…it always says it cannot connect, it can only connect if I use item 1, no idea why but thats okay for now…

So if I do not have serial monitor open and I run my applescript this happens…

My LCD is reset to its initial state with an X in the upper left corner…now if I open my serial monitor…

and send something to the screen…it works again

and if I try to go back to the applescript, and run it…

again nothing happens…

but here is where it gets weird…if I again go to my serial monitor and type something and send it…

I get test something on my screen…obv getting the test from the applescript and the something from serial monitor…is there something really simple I am missing…ive tried to add \n to applescript, and also tried adding & return, neither seemed to work…what is the small detail I am missing here??

EDIT

So i did a good amount of research last night and this morning, and found the ASCII representations of both Newline and Carriage Return…Since the newline wasnt working before I started off with switching to a Carriage Return…

I replaced if (recieved == '\n'); with if (recieved == 13); and then in my applescript added set CR to (ASCII character 13) and then changed the write line to serialport write "really" & CR to portRef

This works…well Sometimes, I know it is an issue of opening and closing the serial port, which I have little knowledge on…but what is happening is that, if I start from scratch…open serial monitor then open applescript and it the script works…the only problem I am having is that then if I try to run it a second time…it says cannot open port

This is where I am confused, my script will be a repeat but what I want to know is why can it not connect to the port the second time?

Best Answer

I think you are bitten by CR-LF (carriage return [0x0d, '\r'], line feed [0x0a, '\n']). Sometimes CR comes first followed by a LF; sometimes the LF comes first, followed by the CR; sometimes only a CR is sent; sometimes only a LF is sent.

It really depends on the application and/or operating system you use. I'd advice to change your sketch in such a way that it doesn't print the actual characters, but prints the hexcodes. That will be the easiest way to troubleshoot this problem.

Don't filter the incoming data, so remove the '\n'-check:

if (recieved == '\n')

Replace:

lcd.print(inData);

by:

char ascii[ 8 ];
sprintf( ascii , "0x%02x " , inData );
lcd.print( ascii );

to view the data in hex.

Once you figured out what codes are arriving at your Arduino, you can change your code accordingly. Make your code robust as possible, as CR/LF comes in many variations. I'd even account for a NULL (0x00) character.