Go Down

Topic: USB communication with Leonardo (Read 12972 times) previous topic - next topic

dhering

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!

pylon

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.

dwyer2bp

...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

dhering

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!

pylon

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

Code: [Select]
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

Code: [Select]
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.

dhering

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.

Grumpy_Mike

Quote
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.

Grumpy_Mike

#7
Jun 15, 2012, 06:25 pm Last Edit: Jun 15, 2012, 06:28 pm by Grumpy_Mike Reason: 1
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?

dhering

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.

Grumpy_Mike

Quote
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.

dhering


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?

Grumpy_Mike

Quote
You just copy and pasted the code?

Yes I just added the
Code: [Select]
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.

dhering


Quote
You just copy and pasted the code?

Yes I just added the
Code: [Select]
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.

Grumpy_Mike

I put it here:-
Code: [Select]
void setup()
{
  Serial.begin(9600);
  while (! Serial);
  pinMode(ledPin, OUTPUT);                                                // Set the LED pin as output
  digitalWrite(ledPin, LOW);                                              // Initialize the LED to off
}



dhering

Thanks.  That's what I had as well.

Hmmm.

Go Up