Faceplanting When Using the Hardware Serial Ports - Teensy 3.2

Status
Not open for further replies.

Sn3akyP3t3

New member
I'm new to Teensy. I came to this platform out of need of additional hardware serial connections due to limitations of Arduino while keeping a small profile. I figured that HW Serial on Teensy should work out of box, but I'm not getting that experience at all! I'm seeing USB serial just fine, but nothing seems to be flowing to or from anything Serial1 through Serial6. I'm using Teensy 3.2. Help appreciated.

I've stripped out any complexity and working with PC USB using Teensyduino straight to the Teensy via a micro USB cable. My computer is running Windows 10. Arduino version is 1.8.6. I've tried running the example code, with minor refactored changes, from https://www.pjrc.com/teensy/td_uart.html and didn't see any output from UART. I modified the code slightly to the current version I'm testing with.

I think it should be passing data from USB Serial to HW Serial and then printing out USB Serial. I'm not sure yet if I should be able to see in the Serial monitor anything from HW Serial. I don't fully understand what HW Serial is capable of. This is the modified code:
Code:
void setup()
{
	Serial.begin(9600);
	Serial1.begin(9600);
}

void loop()
{
	byte incomingByte;

	if (Serial.available() > 0)
	{
		incomingByte = Serial.read();
		Serial.print("USB received: ");
		Serial.println(incomingByte, DEC);
		Serial1.write(incomingByte);
	}
	if (Serial1.available() > 0)
	{
		incomingByte = Serial1.read();
		Serial.print("UART received: ");
		Serial.println(incomingByte, DEC);
	}
}

This was my Arduino settings used to upload and view via serial monitor:
TeensyArduinoSettings.png

This was the input/output via the Arduino serial monitor:
TeensyArduinoSerialMonitorOutput.png

I expected to see data in the serial monitor containing "UART received:".
 
Why? You did not send anthing to the UART. The UARTS are hardware UARTs, that means you may want to connect a serial device to the Teensy pins (pin 0 and pin 1 for Serial1).
USB is "hardware", too, but it can speak way more protocols, and the arduino monitor shows the protocols USB-Serial and USB-HID.

Unlike the simple Arduinos, the Teensy-chip uses a real USB connection - simple Arduinos use hw-serial and a additional special converter chip which translates their serial to USB, because the Arduino have no native USB interface.
 
For something like that to work jumper pin #0 to #1 and anything sent out Serial1 will feedback into Serial#1

As Frank notes Serial is separate hardware from Serial1 on Teensy - both independently usable at the same time.
 
Ok, I know this is kind of an obvious question, but have you actually connected anything to the hardware serial port? The results in your screenshot are exactly what should happen if no serial device is connected to the pins.

I ran your program just now on a Teensy 3.2, with a wire between RX1 & TX1, so anything that's transmitted on Serial1 should be immediately received back. Here's the result I see in the Arduino Serial Monitor.

sc.png

Here's a photo of how I tested, so you can make sure to try it the same way.

DSC_0285_web.jpg

Before you go any farther with whatever hardware serial device you're trying to use, do this simple test with just a wire. (edit: looks like Defragster wrote this same advice faster....)
 
Well, that was staring me in the face and I didn't see it! I think I'm suffering from a massive case of Gross Conceptual Error when dealing with UART then. I didn't think about the need for the wire. I got stuck at first trying to figure out why this line never works:
Code:
	if (Serial.available() > 0)
	{
		incomingByte = Serial.read();
		// Serial.print("USB received: ");
		// Serial.println(incomingByte, DEC);
                Serial1.print("USB received:");
                Serial1.println(incomingByte, DEC);
	}

Then modified that to what I posted initially. So without shorting the pins so they can TX and RX why does Serial1.print not output or is that not something the Arduino IDE serial monitor has access to? I think this is what I got hung up on and probably should have been my initial question before it evolved.
 
So without shorting the pins so they can TX and RX why does Serial1.print not output

When you use Serial1.write(), Teensy does indeed change the voltage on the TX1 pin (pin 1), in a time-sequence pattern for the data you transmitted. But if there's nothing connected to the TX1 pin, think of the philosophical question "if a tree falls in the forest and no one is around to hear it, does it make a sound?"

Hardware serial is all about connecting serial devices like GPS modules, DMX lighting controllers, MIDI, etc. It's not useful without actual hardware.
 

So without shorting the pins so they can TX and RX why does Serial1.print not output or is that not something the Arduino IDE serial monitor has access to?
...

Was hoping post #3 would clarify that from initial post.

On Teensy No, Serial USB has no access to Serial1 unless Hardware and software are configured to associate those two wholly separate pieces of hardware
 
Compared to hardware serial, modern protocols like USB are very complicated. Your computer (technically the "USB host") knows whether a USB device is connected to each port. It knows when you plug in a hub, how many ports the hub has, and whether a device is on each port. How USB does this is incredibly complicated. Just the base USB 2.0 spec is over 600 pages, and the host controller specs, device class specs and details on the drivers adds on top of all that.

Hardware serial is very simple. The pin simply changes voltage for a predetermined amount of time. Unlike USB, from the code on Teensy you have no way of actually knowing whether anything is really connected to the TX1 pin. Well, unless the thing you're using responds somehow. For example, traditional modems hear "AT" commands and reply with "OK" or various error codes. So what you send the "AT", you don't know at that moment it anything really heard it. You find out later, only if something send you data by changing the voltage on the RX1 pin. If nothing is connected, you conclude that by realizing nothing sent you a reply. You have decide when you give up waiting for the reply.

Many serial protocols, like MIDI, could be described as "fire & forget". With MIDI, you transmit "note on" and "note off" messages when the musician pressed and releases a key. You don't know if the MIDI cable is actually connected to a synth that will make sound or do something else. Likewise, GPS modules usually start ending NEMA messages with coordinates & tons of other info, without any way of knowing whether something is listening and using that data for navigation.

The same could be said for showing the info on screens. Nearly all user interfaces are made this way - prompts shown on a screen, then later the user presses a button based on what they see on the screen. But a very real possibility is nobody is looking at the screen that button press will never happen.

Modern protocols like I2C & USB have acknowledgements built in, so you find out immediately if the receiver is connected and got your messages. But simpler protocols don't have automatic acknowledgement built in.

Hardware serial is a very simple protocol, dating all the way back to the earliest teletype machines. The "start bit" change in voltage would release a mechanical clutch and the timing of the voltage change for each bit would control a solenoid that pressed pins as a wheel rotated, and then at the end of its cycle the position of those pins would determine which key of the typewriter to strike the paper. When I think of serial protocol, I like to think back to those old teletype machines. That's how old and how simple asynchronous serial is.
 
Last edited:
I think I understand. I hope I don't run into too many other platform specific gotchas for awhile, but I'm pretty sure that just comes with the learning process.
 
Status
Not open for further replies.
Back
Top