I got teensy 2.0 USB serial example project compiled and flashed to the board, it worked as expected.
The question is: How do i know when the port is opened on PC? is there a variable or a function I can call to check if port is open? or there is a callback function to register?
No code is needed since it is the example project: "USB_Serial->HelloWorld"
I do not think you can see if the port is open, Arduino IDE already does this for you, it'll give you an error message if it:
A: can't connect to the board.
or B: can't get access from the COM port due to something else using that port.
if you'd like it to see if the serial monitor is open, you can use if(Serial){/*your code*/}
when using the "HardwareSerial.h" library, it's built-in, so you don't need to get the library if you're using Arduino IDE.
Just to make this question clear:
What I want to detect is when a software on PC(e.g. tera term, putty) has opened the COM port that tenssy board create. Is there any way to detect this event. The serial port can be opened without any error in my project code, and it does print things on teraterm. I'd like to execute some code on COM port open by PC, so I know communication is established.
I don't know, I'll see if I can get some information about it.
yeah, I'm reading the tenssy's library code now to see if there is any hint. So far nothing
i don't know what library you're using.
oh, it is the library come with tenssyduino. In hardware folder there are some headers and cpp files contains all the magic for arduino IDE to work with tenssy
Although I still haven't found the answer to the question, there are a work around that might solve this issue:
https://docs.circuitpython.org/en/latest/shared-bindings/usb_cdc/index.html#usb_cdc.Serial.connected
I'm going to try check the DTR signal and use the on-board LED to see if this work.
then you may use
Serial.dtr()
to check if some software has to COM port open, by default, it's low (0), if something has the COM port open, it's high (1), I hope this works!
please read this:
Teensyduino: Using USB Serial with Teensy on the Arduino IDE
haha I can't believe i didn't read the 9th post!
OK, I've done testing the method.
According to the circuit python doc on usb serial port, their connected attribute is using DTR signal for connected status. So I added these lines to my test code.
Here is what I did:
void setup()
{
pinMode(11, OUTPUT);
Serial.begin(115200); // USB serial
}
void loop()
{
digitalWrite(11, Serial.dtr()); **// show COM port status on LED**
}
When I open COM port on PC, the LED turns ON
>>> import serial
>>> sp = serial.Serial('COM6') #LED turns ON
>>> sp.close() #LED turns OFF
This work around is good enough for what I need.
great! I'm happy to hear that it works!