USB communication with Leonardo

Greetings,

I recently purchased a Leonardo and have the keyboard, mouse, and other functionality working. I can't seem to send/receive serial data to/from the Leonardo and have it work with anything but the built in serial monitor of the Arduino 1.0.1 IDE. I'd like to use Processing or a terminal program to send and receive serial data from the Leonardo. I've looked around, but don't see this listed as a problem anywhere.

Does anyone have Processing or something other than the serial monitor successfully sending/receiving serial data from the Leonardo via Serial.write or Serial.read?

Here's some example code that works with the serial monitor:

if (Serial.available() > 0)
{
bytSerialReceived = Serial.read(); // Get the byte from the serial port
if(bytSerialReceived == 'n') digitalWrite(ledPin, HIGH); // If the byte received is an n, turn on the led
if(bytSerialReceived == 'f') digitalWrite(ledPin, LOW); // If the byte received is an f, turn off the led
Serial.write(bytSerialReceived + 1); // Echo back the byte + 1
}

Thanks for any help!

Yes, my serial port terminal (on a Ubuntu Linux box) works perfectly with my Leonardo. If you describe what problems you have with your Leonardo, maybe we can help. Don't you find the serial port on the PC? Please describe as much details as possible.

...what have you attempted to use serial communication with? If processing, include your code and we can help you out.

Ps. include the entire arduino code you used with the code snippet that you included, I could whip up a quick processing sketch to turn on your LEDs...just need the COM port# and baud rate

Thanks for the reply. I tested the Arduino sketch on the Uno and the Leonardo using the serial monitor tool in the Arduino IDE, using a simple Processing sketch, and using a terminal emulator I have (I am using Windows 7).

With the Arduino, all works as expected. Receive an 'n', turn on the LED, receive an 'f', turn off the LED. Add one to whatever was received and send back. The Arduino serial monitor works, Processing works, and the terminal emulator works.

With the Leonardo, the Leonardo receives the serial data just fine and controls the LED. However, only the serial monitor of the Arduino IDE receives the data sent back from the Leonardo. Processing and the terminal emulator do not.

Here is the full code for the simple Arduino test sketch:

byte bytSerialReceived = 0; // Byte received from the serial port
int ledPin = 13; // LED connected to digital pin 13

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
digitalWrite(ledPin, LOW); // Initialize the LED to off
}

void loop() {
// Using serial port bytes to control the mouse
if (Serial.available() > 0)
{
bytSerialReceived = Serial.read(); // Get the byte from the serial port
if(bytSerialReceived == 'n') digitalWrite(ledPin, HIGH); // If the byte received is an n, turn on the led
if(bytSerialReceived == 'f') digitalWrite(ledPin, LOW); // If the byte received is an f, turn off the led
Serial.write(bytSerialReceived + 1); // Echo back the byte + 1
}
}

Here's the Processing sketch:

import processing.serial.*; // Import the serial library

Serial myPort; // The serial port
int intvalue = 0; // The fill value for the rectangle on screen
char chSerial = 'f'; // The character to send via serial
byte bytReceiveByte = 0; // Holds byte received from serial

void setup()
{
size(300,300);
println(Serial.list()); // List all the available serial ports
myPort = new Serial(this, Serial.list()[0], 9600); // Define the serial port we are using
}

void draw()
{
fill(intvalue); // Set the fill value for the rect
rect(25, 25, 50, 50); // Draw the rect
if (myPort.available() > 0) // If there serial data is available
{
bytReceiveByte = byte(myPort.read()); // Read the byte
println(char(bytReceiveByte)); // Print the byte
}
}

// Process mouse click--toggle the fill value and the character to send
void mouseClicked()
{
if(intvalue == 0)
{
intvalue = 255; // Set the fill to white (on)
chSerial = 'n'; // Set the character to the on value
}
else
{
intvalue = 0; // Set the fill to black (off)
chSerial = 'f'; // Set the character to the off value
}
myPort.write(chSerial); // Send the character
}

Any assistance in understanding why the Leonardo's sent data isn't received would be greatly appreciated.
Thanks!

For the Leonardo you have to include this code line into your setup() routine:

while (! Serial);

Looks stupid but this waits for the serial emulation to come up on the USB bus. It's only useful for the Leonardo but doesn't disturb on the other boards.

Also include a

Serial.flush();

after your Serial.write() to be sure the USB packet is sent.

The Serial.write() returns the number of characters written. If the above does not work, check for that return value and react on the LED if the returned value is not 1.

Thanks. I put the while (! Serial); in setup and Serial.flush after the Serial.write. I put a loop in to blink the LED for the number of bytes sent by Serial.write. The result is the same as before--when using the serial monitor within the Arduino IDE, a byte is received and the LED blinks one time. When using Processing or a terminal emulator, no byte is received from the Arduino and the LED does not blink (indicating that the number of bytes sent is 0).

The baud rate doesn't seem to make a difference to the Arduino or serial monitor. Is there a particular baud rate I should be using for Processing or the terminal emulator?

Thanks.

Is there a particular baud rate I should be using for Processing or the terminal emulator?

No because the data never gets to be serialised, any baud rate just gets ignored.

I just tried the code you posted and it works for me.

Clicking in the box makes the arduino light turn on and off.

Are you seeing the arduino show up as option [ 0 ] in the list of serial devices in the processing console?

The code works fine for turning the LED off and on from Processing, the Arduino IDE serial monitor, and anything else. Sending data to the Leonardo works just fine. The problem I am having is that outside of the Arduino serial monitor, I can't get the Leonardo to send data back to the host. I don't see the TX LED or get any blinking that indicates that the Leonardo has sent data via Serial.write to anything but the serial monitor. This means I can't use the Leonardo with Processing if I want to send data from the Leonardo to Processing.

I can't get the Leonardo to send data back to the host. I don't see the TX LED or get any blinking that indicates that the Leonardo has sent data via Serial.write to anything but the serial monitor.

Well when I tried that code on my Leonardo it all worked for me. TX & RX LEDs came on and the arduino sent the byte back ( plus one ) and it displayed in the processing window.

Grumpy_Mike:
Well when I tried that code on my Leonardo it all worked for me. TX & RX LEDs came on and the arduino sent the byte back ( plus one ) and it displayed in the processing window.

Interesting. I wonder what is different for me. I tried it on two Leonardos with the same result (they both came from SparkFun but are official boards). Strange. You just copy and pasted the code? Anything different in either the Arduino or Processing code?

You just copy and pasted the code?

Yes I just added the

while (! Serial);

to the arduino.

As I said before does the arduino show up as option [ 0 ] in the list of serial devices in the processing console?
It does on my machine but I am using a Mac.

Grumpy_Mike:

You just copy and pasted the code?

Yes I just added the

while (! Serial);

to the arduino.

As I said before does the arduino show up as option [ 0 ] in the list of serial devices in the processing console?
It does on my machine but I am using a Mac.

Yes, I see the com port show up as option[0]. Would you mind posting your Arduino code to make sure I put the while(!Serial) in the right place?

Yours works on a Mac. I wonder if anyone else with Windows 7 has this problem?

Thanks for the help.

I put it here:-

void setup()
{
  Serial.begin(9600);
  while (! Serial);
  pinMode(ledPin, OUTPUT);                                                // Set the LED pin as output
  digitalWrite(ledPin, LOW);                                              // Initialize the LED to off
}

Thanks. That's what I had as well.

Hmmm.

I'm with grumpy Mike, when I plug more than one arduino In (not at the same time) they are auto assigned different port numbers...I'm guessing your uno gets a different port number than the Leo right?

johnnyonthespot:
I'm with grumpy Mike, when I plug more than one arduino In (not at the same time) they are auto assigned different port numbers...I'm guessing your uno gets a different port number than the Leo right?

Yes, it does.

And you update your processing code with the COM port assigned to the LEO when trying to communicate to the LEO, correct?

myPort = new Serial(this, Serial.list()[0], 9600);  // Define the serial port we are using

I believe this line (in processing) assigns 'myport' to the first available serial port. I'm wondering if the correct port is getting assigned for the Leo; for example, say you have both boards plugged in at the same time, the one with the lower com# would be assigned to myport every time.

I could see that happening if I had two boards connected at the same time. I only have the Leonardo connected when I have the problem. For now I am using the UNO again for our project, which works fine.

Thanks!

It looks like I have the same problem in my eclipse plugin.
I have been modifying my eclipse plugin to work with Leonardo. I got the compile to work, I got the upload to work. But now the serial monitor seems to fail.

On my system the Leonardo always identifies itself as COM15. The port to upload is always COM16.
if I use the Arduino serial monitor on COM15 it works fine. (I get the writings from the Arduino sketch below)
If I use my serial monitor on COM15 nothing is received.

The code I'm running on Arduino is

//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
	Serial.begin(57600);
	while (! Serial);
}

// The loop function is called in an endless loop
void loop()
{
//Add your repeated code here
	static int count;
	Serial.print("eclipse leonardo: ");
	Serial.println(count++);
	delay(100);
}

The code in eclipse is huge so if interested look in GitHub - Sloeber/arduino-eclipse-plugin: A plugin to make programming the arduino in eclipse easy

I haven't tried sending data to Leonardo but I do not receive any serial output from the Leonardo in my plugin.
I've tested with my mega and everything works fine there. There must be a difference between the Leonardo and other Arduino boards.
Anyone has a clue?

Best regards
Jantje

I'm using Windows7 64 bit with 64 bit Java