3
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ I'd try not put putting the \n in the received buffer in case the LCD doesn't like it. I'm not familiar with AppleScript but the second thing I'd try is using say Z instead of \n just in case it handles it differently (maybe linefeed instead etc). \$\endgroup\$ – PeterJ Mar 15 '13 at 4:05
  • \$\begingroup\$ Are you adding the \n in the quotes? \$\endgroup\$ – Passerby Mar 15 '13 at 5:51
  • \$\begingroup\$ Also, try this. Change if (received == '\n') to if (inData == 'text') and send just "text" in your applescript. Does it work then? Essentially the only thing I see is that you are not correctly sending the newline \n for some reason. \$\endgroup\$ – Passerby Mar 15 '13 at 5:55
  • \$\begingroup\$ You may want to try the /dev/cu* devices instead of the /dev/tty* ones. \$\endgroup\$ – Chris Stratton Mar 15 '13 at 12:35
  • \$\begingroup\$ I tried using /dev/tty and nothing worked at all \$\endgroup\$ – Chris James Champeau Mar 15 '13 at 18:11
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Trying this out, it says that ascii is not declared...which it is not, just curious how I should declare that to get the proper results your looking for \$\endgroup\$ – Chris James Champeau Mar 15 '13 at 18:14
  • \$\begingroup\$ How foolish of me, you are right I forgot the declaration. 8 bytes is a bit oversized, 4 should be sufficient, but I copied it from a working program I wrote a while ago. 8 is safe. \$\endgroup\$ – jippie Mar 15 '13 at 19:42
  • \$\begingroup\$ Cool I actually got it working tho, if you re-read the question, I am now just having issues with the serial port...I may ask a new question though for the new issue \$\endgroup\$ – Chris James Champeau Mar 15 '13 at 19:53
0
\$\begingroup\$

I was able to get this working, as I explained in an edit above I did the following...

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

That seemed to work for me

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.