Communication via USB

Hi all,

I am new to this and I hope I am not posting in the wrong channel :)

I read the reference documentation and stumbled upon the serial class where it says there is a way to communicate via usb. I am having an arduino nano/328. As you all know it already has a usb port. Can anybody tell me/give me links to read on how to establish a connection for communication between my pc and arduino?

Thanks in advance

micha

Using what programming language or application?

The Arduino can do a Serial.print to send data to the serial port. There are many ways on the PC to read data from the serial port.

The Arduino connects to the computer using a USB cable, with an onboard USB to serial converter.

There are examples provided with the Arduino IDE that show how to communicate with the Processing application (also free):

http://www.processing.org/download/

I´m having the same problem.
Trying a simple on/off LED, works just fine with Serial Monitor (from arduino app).
But when I try to send through PHP script it doesn´t.

Arduino Code:
int ledPin13 = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {

Serial.begin(9600); // initialize serial communication

pinMode(ledPin13, OUTPUT); // initialize the green LED pin as an output
}
void loop()
{
// see if there’s incoming serial data:
if (Serial.available() > 0)
{

incomingByte = Serial.read(); // read the oldest byte in the serial buffer
//Preform the code to switch on or off the leds
if (incomingByte != ‘1’)
{
digitalWrite(ledPin13, HIGH); //If the serial data is 1 turn the LED on
}
if (incomingByte == ‘0’)
{
digitalWrite(ledPin13, LOW); //If the serial data is 0 turn the LED off
}

}
}

PHP Code:

<? if ($port = fopen("COM11", "w+")) { if (fwrite($port, '2')) { echo "OK "; } else { echo "NOT OK "; } } else { echo "Not connected"; } ?>

If someone have any clue, please tell me.

Problem solved.
I have to change my COM11 to COM3 or COM1 in the Hardware Manager (windows xp).

I dont know why, but it does not work with COM10 and higher.
It just work with COM1 to COM9.

Now here is working.

Another tip is that we can not send a value like this:
fwrite($port, ‘2’);

If I want to send the number 2, I have to write the code as folow:
fwrite($port, chr(50));

The “chr(50)” is the number 2 in ASCII table.

Updating my PHP code (This is working just fine):
<?
//recebe os dados via GET:
if (!$_GET[‘caracter’])
{
$valor = 48; // coloquei esse caracter para deixar o led desligado ao iniciar
}
else
{
$valor = $_GET[‘caracter’];
}
//configuração da conexão serial
mode com11: BAUD=9600 PARITY=N data=8 stop=1 xon=off;
$fp = fopen (“COM1”, “w+”);
if (!$fp)
{
** echo “Porta fechada”;**
}
else
{
** $string = chr($valor);**
** echo "caracter enviado: ".$string;**
** fputs ($fp, $string);**
** fclose ($fp);**
}
?>

Ligar LED


Desligar LED

Translating to english. Sorry about tho flood…

<? //receive data from GET: if (!$_GET['caracter']) { $valor = 48; //this is just to keep the LED turned off when it starts the first time } else { $valor = $_GET['caracter']; } //config serial connection `mode com11: BAUD=9600 PARITY=N data=8 stop=1 xon=off`; $fp = fopen ("COM1", "w+"); //put your port from COM1 to COM9 if (!$fp) { echo "Uh-oh. Port not opened."; } else { $string = chr($valor); echo "caracter: ".$string; fputs ($fp, $string); fclose ($fp); } ?>

Turn LED ON

Turn LED OFF

I dont know why, but it does not work with COM10 and higher.

Try opening "\.\COM10".

Thank you for posting about your experience.

Try opening "\.\COM10". Thank you for posting about your experience.

I have tryed this before, and did not wort too. I dont know if there is any relationship to my system: Windows + Apache + PHP. I tested this path "\.\COM10" on two computers, but it did not work at all. When I chanme to COM1 or COM2, both starts to work.

Thank you.

I tested this path "\.\COM10" on two computers, but it did not work at all.

Dang it! My brain isn't working too well. If you're still interested, try one (or both of these)...

  "\\\\.\\COM10"  /* double-quotes */
  '\\.\COM10'     /* single-quotes */

What a big trick, man!!! :) Thak you very much!!! I will try this tomorow and after I return here to tell you.

Dang it! My brain isn't working too well. If you're still interested, try one (or both of these)...

"\\.\COM10" /* double-quotes / '\.\COM10' / single-quotes */

I tryed this now. Dont work too :(

The only solution for me is change the serial port fro COM1 to COM9. Thank you.

I'm truly stumped. I tried it here (PHP Version 5.2.5) and...

  • The DOS name "COM10" can be opened
  • Neither the device name "\.\COM10" nor "\\.\COM10" can be opened
  • Writing (fputs, fwrite) does not seem to work

I'm truly stumped. I tried it here (PHP Version 5.2.5) and...

The DOS name "COM10" can be opened Neither the device name "\.\COM10" nor "\\.\COM10" can be opened Writing (fputs, fwrite) does not seem to work

Yes, dont work. The solution is to change the COM in the Hardware Manager.