Unable to send serial to Teensy 2.0 (Teensyduino)

Status
Not open for further replies.

sandman1687

New member
Hello all, I've worked with Arduinos in the past, recently got a Teensy 2 for another project I'm working on and I'm having trouble.

When I open up the Serial Monitor, I see serial.print text, but when I send something I get no reaction from the Teensy. Additionally, the window will freeze if I send a few commands, only unfreezing when I reset the device.

When I noticed this error, I wrote this simple program to troubleshoot:

Code:
char cmd = 'a';

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  Serial.print(cmd);
}

void loop() {  
}

void serialEvent() {
  cmd = Serial.read();
  Serial.print(cmd);
}

I get the LED indicator, and the Serial Monitor reads "a", but sending something does nothing. Am I missing something? Thanks for your help guys.
 
IIRC serial.read returns a byte, not a char. So change the type of cmd to byte and try changing the print line to the following to cast the ASCII byte back to a character.

Serial.print((char)cmd);
 
Thanks for the suggestion jsfetzik, I tried that but to no avail. I verified that the unit actually works by testing a simple program that turns on an LED with a button, so I don't think the board is bad.

When I unfreeze the Serial Monitor by resetting the Teensy I see an IOException:

java.io.IOException: Input/output error in writeArray
at gnu.io.RXTXPort.writeArray(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1124)
at processing.app.Serial.write(Serial.java:526)
at processing.app.Serial.write(Serial.java:549)
at processing.app.SerialMonitor.send(SerialMonitor.java:203)
at processing.app.SerialMonitor.access$100(SerialMonitor.java:35)
at processing.app.SerialMonitor$3.actionPerformed(SerialMonitor.java:92)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
at javax.swing.JTextField.postActionEvent(JTextField.java:705)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2886)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2814)
at java.awt.Component.processEvent(Component.java:6040)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Seems like it's just having trouble receiving serial commands. Any other suggestions would be greatly appreciated.
 
Your loop is empty. Try this instead:
void loop() {
if(Serial.available())
serialEvent();
}
 
Thanks guys, ian's code solved it. I was under the false impression that the Teensy was completely code-compatible with Arduino, but I suppose that's not the case.
 
serialEvent is on my to-do list.

It's relatively new in Arduino. Until recently, very few people have asked for serialEvent, so it's been a much lower priority than just about everything else on Teensy 3.0.
 
Status
Not open for further replies.
Back
Top