Applescript to Arduino LCD only works with Serial Monitor

Procesory, grafické karty, matičné dosky, pevné disky,...

Moderátori: psichac, Moderátori

zhousi
Okoloidúci
Okoloidúci
Príspevky: 1
Dátum registrácie: 16 Nov 2017, 09:17

Applescript to Arduino LCD only works with Serial Monitor

Príspevok od používateľa zhousi » 16 Nov 2017, 09:42

Ask some suggestions for my problem. :) Background for question: 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...And look at this:

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 http://www.kynix.com/Search/LCD.html 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?
Thanks all guys!
0

  • Podobné témy
    Odpovedí
    Zobrazení
    Posledný príspevok