Write to serial port doesn't work without arduino app and serial console running

Hello!

I have pretty basic configuration with Arduino Uno + LED Screen, connected to Mac (MacOS v 10.9.1), arduino IDE v 1.0.5.

Arduino client receives character data by serial port and put this data to LED screen

#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 10, 11, 12, 13); 

const int numRows = 2;
const int numCols = 16;

int col = 0;
int row = 0;

char ch;

void setup() {
  lcd.begin(numCols, numRows);
  lcd.clear();
  Serial.begin(9600);
}


void loop() {
  lcd.setCursor(col, row);  
  
  if (Serial.available()) {
    ch = Serial.read();
    if(!isSpecialCharacter(ch)) {
      lcd.write(ch);
      calcColRow();
      
      lcd.setCursor(col, 0);  
    } 
  }
  
  delay(500);

}

// check if character is special
boolean isSpecialCharacter(char ch) {
  return (ch==0x0a)||(ch==0x0d);
}

// where to move cursor
void calcColRow() {
  col++;
  if (col>15) {
    if ((row==0)) {
      col = 0;
      row = 1;
    } else {
      col = 0;
      row = 0;
      lcd.clear();
    } 
  }
}

also I have ruby/sinatra code, which puts data to serial port directly:

require 'rubygems'
require 'sinatra'
require 'haml'
require 'serialport'
require 'logger'
require 'selenium-webdriver'

set :haml, { format: :html5, attr_wrapper: '"'}

get '/' do
  haml :index
end

get '/value' do
  driver = Selenium::WebDriver.for :firefox
  driver.navigate.to "http://localhost:4567/"
  element = driver.find_element(:id, "list")

  jam_value = element.text
  write_to_sp(jam_value)
end

def write_to_sp(value)
  logger = Logger.new(STDOUT)
  logger.level = Logger::DEBUG

  port_str = "/dev/tty.usbmodem1411" 
  baud_rate = 9600
  data_bits = 8
  stop_bits = 1
  parity = SerialPort::NONE
   
  sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
  logger.debug(sp)
  sp.write(value + " ")
  logger.debug(value)
  sp.close 
end

The problem is I can only see result if Arduino IDE is running and serial console is open. Otherwise I couldn't see even leds on Arduino board blinking while receiving data. Could please somebody who faced such troubles already help me?
THnks!

In short, you need to keep the serialport open (open occurs when creating new serial port object in ruby) by not causing your program to finish. Running the serial monitor keeps the serialport open.

I came across this same issue with my arduino uno + LCD screen, and only seeing messages displayed on the LCD with the "serial monitor" (from the arduino sketch software) open.It was a logic flaw with my (ruby) code communicating via the serial port. My code created serialport object, wrote a message, then finished execution. Because it finished running, it closed the serial port. This would effectively reset the arduino and happen too quickly to see any message.

I modified my code to have an infinite loop of user input by keyboard. When it got a new line (chomp) of characters, it would write this to the serial port. This worked. In later programs, I simply open the serial port and make sure the program has some sort of loop to keep it from finishing entirely, like a user prompt to exit the program. As long as the serial port is open, the arduino/LCD correctly responded and displayed messages I sent to it.

(EDIT: I realize this is an old post, but I came across it more than once with a google search when researching this. Figured I would respond)