Java error w/ Arduino 1.6.7 Serial Monitor, Raw HID, OSX 10.9.5, Teensy 1.27

duff

Well-known member
Hi All,

I was wondering if anyone has seen this issue with using RAW HID and the Arduino Serial Monitor and trying to send characters through it. I get an error from the IDE. The Teensy prints to the Serial Monitor fine though.

The code that is compiled on the Teensy 3.2 at 96 MHz is this:
Code:
elapsedMillis time;
int count;
bool stop;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(230400);
  Serial1.begin(230400);
  time = 0;
  count = 0;
  stop = false;
}


void loop() {
  if (time > 20 && !stop) {
    Serial1.print((char)0x00);
    //Serial1.print((char)0x00);
    //Serial1.print((char)0x00);
    //Serial1.print((char)0x00);
    //Serial1.print((char)0x00);
    Serial1.flush();
    delay(2);
    Serial1.printf("Hello, Count: %i\n", count++);
    digitalWriteFast(LED_BUILTIN, HIGH);
    Serial1.flush();
    digitalWriteFast(LED_BUILTIN, LOW);
    time = 0;
  }
  if (Serial1.available()) {
    char c = Serial1.read();
    Serial.print(c);
    time = 0;
  }


  if (Serial.available()) {
    char c = Serial.read();
    if (c == 's') stop = true;
    if (c == 'r') stop = false;
    Serial.print(c);
    time = 0;
  }
}

And the error that comes up when I try to send characters to through the Serial Monitor is this:
Code:
Build options changed, rebuilding all


Sketch uses 34,276 bytes (13%) of program storage space. Maximum is 262,144 bytes.
Global variables use 4,884 bytes (7%) of dynamic memory, leaving 60,652 bytes for local variables. Maximum is 65,536 bytes.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at processing.app.Serial.write(Serial.java:191)
    at processing.app.Serial.write(Serial.java:211)
    at processing.app.TeensyMonitor.send(TeensyMonitor.java:95)
    at processing.app.TeensyMonitor.access$100(TeensyMonitor.java:36)
    at processing.app.TeensyMonitor$2.actionPerformed(TeensyMonitor.java:72)
    at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
    at javax.swing.JTextField.postActionEvent(JTextField.java:721)
    at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2882)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2929)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2845)
    at java.awt.Component.processEvent(Component.java:6312)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
    at java.awt.Component.dispatchEventImpl(Component.java:4762)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
The Arduino Serial Monitor won't even try to send them it seems, I don't remember this being a problem from before? This fresh install of both Arduino and Teensyduino, I have restarted the IDE many times so I don't know what else to try?
 
@duff - SerMon has been generally failing with IDE 1.6.7 for me so I've been appreciating TYQT more than ever.

I have no idea what or why RAW_HID is or if TYQT likes it in place of USB_Type / Serial . . . so . . .

I just recompiled my current sketch as 'Raw HID' and TYQT picked it up for Serial output and input just the same.

instead of a COM# port - it is connected as::

\\.\HID#VID_16C0&PID_0486&MI_01#9&1189022D&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

And looking at the IDE ports - it is not listed.
 
can you try this sketch compiled as RAW HID and use the Arduino Serial monitor. Just try to send it some data from the Serial Monitor and see if it gives you the same error as me?
Code:
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(230400);
}


void loop() {
  if (Serial.available() > 0) {
    char incomingByte = Serial.read();
    Serial.print(incomingByte);
    digitalWriteFast(LED_BUILTIN, HIGH);
    delay(10);
    digitalWriteFast(LED_BUILTIN, LOW);
  }
}
 
I have two Teensy's with Raw HID and NOTHING in PORTS on IDE to connect to. One is running your post #3 code.

When I use TYQT both are fully functional for input and output - no errors and no grief.

Try TYQT - the grief goes away and everything works better - you can RESET a Teensy - give it a friendly name (to ID within TYQT) - replace Teensy Loader for directed uploads (under Help). Faster better serial support, monitor multiple devices at once. Also Koromix - while busy and only doing TYQT as a part time project - is very responsive to issues and errors and improvements!
 
I have two Teensy's with Raw HID and NOTHING in PORTS on IDE to connect to. One is running your post #3 code.
You should be able to open the Serial Monitor even if doesn't show up under "Tools -> Port" with the Arduino IDE by clicking the button?

I will take a look TYQT but I just wanted to see if I'm alone trying to use the Arduino IDE Serial Monitor with RAW HID.
 
So HID works SerMon with no port selection? How does it work with two active devices? TYQT shows and monitors both. I didn't try that and have un-HID my devices.

I'll put one back - because I'm finding on a pair to T_3.1's - BOTH can set pin 3 output and it is LOW great, but once I set it HIGH - it stays high ?????? I have 3 T_3.2's running the same code and there is No Problem with with working as it should - I use this pin (though different # on the T_3.2's) to tell the ESP8266 in setup not to run any beyond OTA safe code - in case I put a fault in the code. On the T_3.1's once I set it high - I have to reset the Teensy to turn the pin off ?????

I'll put my set code in your sample and see what SerMon does - then go back to TYQT - BTW - TYQT is Awesome - it makes Teensy better and complete.

OH - BTW >>> When I had HID only devices - the IDE will not show my IP PORT to my ESP's for OTA upload. As soon as one came back USB so did the IP PORT - so HID causes the IDE to act in bad ways with regard to this I can say!
 
@duff - indeed I see errors trying to get IDE SerMon to work with a Raw HID Teensy. My errors below when using the included sketch as Raw HID.

The LOSS of the IP PORT for ESP OTA upload is also back. I think you have found an unsupported interface for the Arduino IDE - the fact that it tries to 'anonymously' open a HID port is wrong - if you had TWO devices it would never work to specify which one even if it did work on one, which it does not.

Once again TYQT proves itself to be the tool you need to fully enjoy your Teensy - it just keeps giving! - Thank you koromix!!!

Now back to my problem ... to avoid a blatant cross post I'll go start a new thread once I minimize my sketch and see it repro . . . but my sketch on pin 17 works for the code below but on pin 3 it sets the pin that can never be dropped - but only in the context of my other ESP directed sketch on a T_3.1 or T_3.2 . . . odd . . . maybe following the forum rule and minimizing the sketch I'll find it before I post and save myself public scorn and shame . . . <edit> my problem was #(re)define setting an output to input and then doing a write to what I thought was an output - it took the HIGH - but not a LOW - Odd

Code:
#define ESP8266_ESTOP     3 // GPIO5

void setup() {
  pinMode(ESP8266_ESTOP, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(ESP8266_ESTOP, LOW);
  Serial.begin(230400);
}

int lastESTOP = 2;

void loop() {
  if (Serial.available() > 0) {
    char incomingByte = Serial.read();
    Serial.print(incomingByte);
    if ('E' == incomingByte) digitalWrite(ESP8266_ESTOP, HIGH);
    if ('e' == incomingByte) digitalWrite(ESP8266_ESTOP, LOW);
    digitalWriteFast(LED_BUILTIN, HIGH);
    delay(10);
    digitalWriteFast(LED_BUILTIN, LOW);
  }
  if ( lastESTOP != digitalRead( ESP8266_ESTOP ) ) {
    lastESTOP = digitalRead( ESP8266_ESTOP );
    Serial.print( "\nESTOP PIN change to:");
    Serial.println( lastESTOP);
  }
}

Uploading to board 'RTC1bp_467030-T_3.1' (Teensy 3.1)
Triggering board reboot
Firmware: RawHID_feb08a.ino.hex
Flash usage: 17 kiB (6.4%)
Uploading...
Sending reset command
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at processing.app.Serial.write(Serial.java:191)
at processing.app.Serial.write(Serial.java:211)
...
 
Last edited:
Any chance you could give this a try on your machine with Arduino 1.0.6 and Teensyduino 1.27?

The serial monitor has changed several times in the last year. It's quite possible a bug has been added somewhere along the way. Knowing whether or not it works on your computer with the old serial monitor from 1.0.6 would really help.
 
@duff maybe? I never tried HID before this as noted - also found the SerMon functionality has degraded since I started here with 1.6.0 - maybe it's Win 10 - or my machine ?

I'm still puzzled how it could have purposeful support without selecting a device in 'Tools / Port' - unless they all just are supposed to work and it broadcasts out to all HID devices?
 
Any chance you could give this a try on your machine with Arduino 1.0.6 and Teensyduino 1.27?

The serial monitor has changed several times in the last year. It's quite possible a bug has been added somewhere along the way. Knowing whether or not it works on your computer with the old serial monitor from 1.0.6 would really help.
Just downloaded Arduino 1.0.6 and Teensyduino 1.27 and compiled the code I provided earlier and the Serial Monitor works as expected when using "USB Type" -> RAW HID. So its something with new Arduino and RAW HID. If USB Type is "Serial" this works like expected with the Serial Monitor with Arduino 1.6.7 and Teensyduino 1.27.
 
I'm working on this problem now. It's definitely a bug in Teensyduino's patches to recent versions of Arduino. Hope to have a fix soon...
 
Thanks, I use RAW HID alot and having it work with the Arduino Monitor is nice to have again when the new release comes.
 
Hi,
I have exactly the same issue, but I don't know how to incorporate your java source fix in my 1.6.7 Arduino ide install (could someone help me on that ?)
However I also have the same issue trying to send directly to the HID seremu interface with hidapi in the c++ application I'm developing on Windows (which is a software to control my forcefeedback device).
I'm using a slightly modified version of hidapi, which let me choose which interface I open, and by opening the seremu interface raw hid device, I can get all the characters sent by my Teensy 3.2 with "Serial.print".
But when I try to send data from the PC to the teensy, the "hid_write" function of hidapi fails, with an "Invalid parameter" error.
Any idea of what I'm doing wrong ?

About my project :
I've successfully managed to modify the Leonardo core to have a working forcefeedback wheel, but the Atmega32U4 is not powerful enough to do it properly, so I'm now looking for a more powerful MCU that can handle quadrature decoders in hardware. That's why I tried with the DUE, with no success so far (I'm stuck at the "Acquire device" phase on the PC, despite I could get a joystick device with all the ffb info in the descriptor properly installed on windows). I'm totally new to USB development. I'm currently using Teensy 3.2 in Raw Hid mode, and feeding a vJoy device. Ideally I would like to to turn the Teensy 3.2 in a pure HID gaming device (like I did on the Leonardo), but that's another step. I use the seremu to configure the board (inputs, type of pwm, etc), so I need communications to work on both direction. I might also add a second Raw Hid interface if I can't get the sermu work in the Host->device direction.

Here's my modified hid_open function :

Code:
HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, unsigned short interface_id, wchar_t *serial_number)
{
	// TODO: Merge this functions with the Linux version. This function should be platform independent.
	struct hid_device_info *devs, *cur_dev;
	const char *path_to_open = NULL;
	hid_device *handle = NULL;
	
	devs = hid_enumerate(vendor_id, product_id);
	cur_dev = devs;
	while (cur_dev) {
		if (cur_dev->vendor_id == vendor_id &&
			cur_dev->product_id == product_id &&
			cur_dev->interface_number == interface_id)
			{
				if (serial_number) 
				{
				if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
					path_to_open = cur_dev->path;
					break;
				}
			}
			else {
				path_to_open = cur_dev->path;
				break;
			}
		}
		cur_dev = cur_dev->next;
	}

	if (path_to_open) {
		/* Open the device */
		handle = hid_open_path(path_to_open);
	}

	hid_free_enumeration(devs);
	
	return handle;
}

Here is the code in my application using hidapi :
Code:
uint32_t cDeviceInterface::Open ()
{
	hid_init();
	mRawHid = hid_open(mVendorId, mProductId, 0, NULL);
	if (mRawHid == NULL) { printf("unable to open device"); return(FALSE); }
	hid_set_nonblocking(mRawHid, TRUE);

	mSerEmu = hid_open(mVendorId, mProductId, 1, NULL);
	if (mSerEmu == NULL) { printf("unable to open device"); return(FALSE); }
	hid_set_nonblocking(mSerEmu, TRUE);
}

void cDeviceInterface::SendData (uint8_t *val, uint32_t len)
{
	hid_write(mSerEmu, val, len);
}

@ Paul : thank you so much for you hardware/software work. Teensy are just great. I've been getting headaches trying to modify the USB stack on the Due, and it's so well done with teensy. Maybe you are interested to add the "Gaming HID device" functionality to the Teensy 2.0 (and maybe later to the other boards).
 
Last edited:
Ok I found out why I couldn't send to seremu : I had to send a full 64 bytes buffer. Here's the corrected code :

Code:
int32_t  cDeviceInterface::SendData (uint8_t *val, uint32_t  len)
{
	uint8_t  buf[64];
	len = min(len,sizeof(buf));
	uint32_t i = 0;
	for (; i < len; i++)
		buf[i] = val[i];
	for (; i < sizeof(buf); i++)
		buf[i] = 0;
	int32_t res = hid_write(mSerEmu, buf, sizeof(buf));
	return(res);
}

But I'm still interested to know how to patch the arduino IDE in order to have the serial monitor working in both directions :rolleyes:
 
Back
Top