Understanding the Serial Port and Data Transmission

Hello dear all.
I’ve been experimenting, trying, re-trying and banging my head against the wall trying to accomplish a small feat.

To summarize, I’ve been trying to communicate with the Arduino via the Serial port (COM3, for this matter, whilst using the standard USB cable). I am able to use the included Serial Monitor from the Arduino to ‘read from’ and ‘print to’ the device, without a problem. I would love to be able to use the data received from the Arduino and send it over to a web server to display it as an HTML file, as well as turning an HTTP request into an action in the Arduino.

  1. I tried using the phpSerial class to communicate with the Arduino (taken from a blog, available at http://blog.bsoares.com.br/wp-content/uploads/2009/05/arduino-php.zip).
    Issues: Whenever I ran the code, the LED (which was supposed to be ON) would blink around 5 times (in sync with the RX and TX LED) very rapidly, and then turned OFF. It looked as if the Arduino was resetting a couple of times until it gave up.

  2. I tried using Python to write a 1 into the Serial port while using the following program in the Arduino:

void setup(){
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop(){

  while(Serial.available() == 0);
  
  //read input
  int val = Serial.read() - '0';
  
  if(val == 1){
    lightup();
  }
  else if(val == 0){
    lightdown();
  }
  else{
    Serial.println("Invalid");
  }
  Serial.flush();
  
  //echo the input
  //Serial.println(val);
}

void lightup(){
  digitalWrite(13, HIGH);
  Serial.println("LED ON");
}

void lightdown(){
  digitalWrite(13, LOW);
  Serial.println("LED OFF");
}

The result would change depending on how I used the following .py script:

import serial
ser = serial.Serial("COM3", 9600)
while(True):
    ser.write(1)

…I would either have to wait around 15 seconds for the LED to light up, if I was to run the script via ‘python <script.py>’ OR it would light up immediately if I was to run it through the Python console.

Am I missing something here? Why is it so easy to talk to the Arduino via the Serial Monitor and it’s giving me such a hard time to do it while using ‘3rd party’ applications?

Thanks in advance. I’ll keep on tweaking with the settings/alternatives to get this going, meanwhile…

When you open the serial port Arduino resets.

Search the forum for the solution, i can't remember it right now, and i'm on my way out the door :-)

It is a while since I did this but if I remember it all went fairly smoothly. I wrote a sketch that sent and received data over the USB and I could communicate with the sketch using the Arduino IDE. Next instead of the IDE I opened hyperterminal and communicated with the sketch using that. Finally I used C# to write my own small program to send and receive data to a com port, I think the USB appeared as com25 or something like that. Unfortunately I don't have the source.

When talking about PHP and Arduino, it helps to say what OS you are using. On Linux and Mac (iirc), php can write to and read from the Arduino. On Windoze, it can only write to the Arduino.

  Serial.flush();

Unless you know exactly what this does, and have a compelling reason to use it, dumping random amounts of unread data is rarely a good idea.

PaulS: When talking about PHP and Arduino, it helps to say what OS you are using. On Linux and Mac (iirc), php can write to and read from the Arduino. On Windoze, it can only write to the Arduino.

  Serial.flush();

Unless you know exactly what this does, and have a compelling reason to use it, dumping random amounts of unread data is rarely a good idea.

Oops, you are right there. I used the Serial.flush() to clear out some strange random data that was showing up when I was printing back the value of what I had just sent to Arduino. It's useless on the current sketch, indeed.

I am running these tryouts on Windows 7. I think I may need to take a few steps back...so I'll just ask away: What would be the easiest way to integrate the Arduino with the web? As in, having a server-side script (running on my localhost) to trigger an action in the Arduino? As I said before, Python seems to be reading input data pretty well, but it kind of messes up when it's running as a script file (either to input or output data)...(any thoughts on why it takes about 15 seconds to say 'Hello World' to the Arduino, after running a script?)

If I could get Python to do it's 'thing' I think I could make it worth with the CGI, running a .py script from an HTTP request.

On a second thought, the Serial Monitor works just perfectly, as in flawless...since the IDE was written in Java...I could...?

Thanks for all the input.

You can get an ethernet shield for the arduino and have it do some web client/server operations. If you want just connect the arduino to a windows pc, then you will need to run a server like apache on the pc and have it communicate with the arduino via some cgi application. Note that if the cgi application open/closes the serial port, then the arduino reset will need to be defeated with something like a 100 ohm resistor between the arduino reset pin and +5v. In the past I’ve made an .exe cgi application using freebasic to use with apache to do the bi-directional serial port communication.