Looking for help on how to detect the host PC closing a serial port

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Hi,

I'm using a PSoC5LP USBFS in a UART-style communication.

I can detect the host PC opening the COM port.   I can even see the host PC signaling the device that the port have closed.   However I cannot distinguish the difference between the two.

Here is a link to the the other forum thread with a little more detail on the issue: How-do-I-detect-that-the-host-PC-has-closed-a-serial-port-using-the-PSoC5LP-USB 

In other words: I'm trying to understand what communication the host PC must provide to the device when a serial port is closed.  I've looked in the USB 2.0 specification and didn't find this information.

Once I understand what is required, I hope to be able to decode it on the device end and inform the device SW that the port is closed and doesn't allow further comm until reopened.

Thank you in advance.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
18 Replies
pacr_284376
Level 5
Level 5
100 replies posted 10 solutions authored 50 sign-ins

Hi Len,

I had the same problem and solved it by enabling DTR - on the HOST side. Example of C# code for USB Host [mind that I'm not an experienced C# programmer] :

serialPort = new SerialPort(SerialPortName, 115200, Parity.None, 8, StopBits.One);
serialPort.WriteTimeout = 2000;
serialPort.ReadTimeout = 5000;
serialPort.Handshake = Handshake.RequestToSend;
serialPort.DtrEnable = true;
try
{
serialPort.Open();
}
catch (Exception e)
{
Trace.WriteLine("ERROR Opening Port");
throw new System.IO.IOException("Port opening failed : " + e.ToString());
}

On the Embedded side you can now use :

if ((C_CycUsb_GetLineControl()&0x01)==0) { CodeWhenUsbGetsClosed(); }

Hope this helps, it works for me

Patrick

 

Edit : Mind that I remember not all terminals set this DTR signal as they should, I am using TeraTerm and that works.

@pacr_284376 

When I created my own UART PC terminal program I used the DTR signal and yes it works by looking at the _IsLinesChanged() function when I get a EP0 isr signal.

However as you pointed out, not all terminal programs use the DTR signal.   I'm trying to improve my Term component to work with ALL PC terminal programs if possible.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Hi Len,

I can understand you want to support any terminal program. However that might be difficult if they do not follow the RS232 standard on this. 

By the way I found another thread on this that might give more information :

https://community.infineon.com/t5/PSoC-5-3-1/flow-control-of-USBUART/td-p/145985

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Len,

I believe that the TX line must go HIGH once the COM port is open. 

As I remember long time ago I used to send notification (ping) on port open and on before close on both sides. 

@odissey1 

You said:


... I believe that the TX line must go HIGH once the COM port is open. 

 


Are you speaking about the TX line being the DP or DM line?

You said:


... As I remember long time ago I used to send notification (ping) on port open and on before close on both sides. 


On the EP0 ISR I get multiple notifications on port "open" (about 12 to 20 of them).  I get about 2 to 4 notifications on EP0 ISR on a "close".    I'm trying to decipher the notification content to determine whether an open or close has occurred.   No real luck.

If I send a 'ping' from the device to the host on an open I should get a response.   If I send a 'ping' on a close I don't expect a response.  Any suggestions for the "pings"?

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Len,

In one case i had to detect if COM port is present, and the TX line (RX on PSoC side) supposed to be High if UART is open. My guess this is how it was checked in old times. I would read RX pin on psoc's uart, and if it is High, then maybe UART is still there. 

 

If, however, you are coding both sides of the UART (PSoC and PC), i would add some uart command from PC, signaling disconnection. 

Other thing could be a ping timer (say, once in 2 sec), to check if port still exist

0 Likes

@odissey1 

I'm trying to detect port open or port closed status using the USBUART component.

I'm not sure the RX on the PSoC side applies.

I'm only trying to code the PSoC-side.  I'm trying to get the USBUART to work will all PC terminal programs if possible.   In my own PC terminal program I use the DTR signal to "solve" this issue.   Most other PC terminal programs don't bother with the DTR.

I can explore a "ping".  However what kind of message can I send to the host side that will invoke a response from the host without being seen in the Terminal data stream?

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
lock attach
Attachments are accessible only for community members.

Hello,

When the USB device is CDC spec compliant, the host will send SET CONTROL LINE STATE CDC control request with  wValue as 2 or 0. This request from the host can be used to detect closing of terminal on host. Please handle the request on PSOC.

Regards,
Rashi
0 Likes

@Rashi_Vatsa

Thank you for your kind help in this matter.

What host tool are you using to see the USB messages?

I believe what you are seeing are the host requests for a line control change when (and this is important) the DTR signal is being controlled by the host terminal program.

When the host term program controls the DTR signal, I get a line control request that the DTR is active (value = 1) when the port opens.  When the port closes, I get another line control request from the host that DTR is inactive (value = 0).

I get these values from <instance>_GetLineControl().

When I use a host terminal program that doesn't control the DTR, I don't get these signals as best I can tell.    It might be possible that the wValue change is appearing but I'm not sure where to look for it.  Maybe the host line control request is still present but I don't see the results in <instance>_GetLineControl().

If I can use the same host tool you were using it might be another clue.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Hi,

I used Tera term to see the requests

Regards,
Rashi
0 Likes

@Rashi_Vatsa 

TeraTerm provides a mode to display USB messages?

I've used TeraTerm before and have never seen this feature.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Hello,

Please let me know which CDC driver are you using 

Is there a way for the PSoC device to ask the host if the CDC port is open?

>> The USB requests will come from host only, USB device cannot initiate a USB request

Regards,
Rashi

@Rashi_Vatsa ,

I'm using the Infineon-supplied CDC driver => USBUART_cdc.inf 

which uses VID=0x04B4 and PID=0xF232.  Device Release=1

Here is some info using the USB registers I have collected when the EP0 event triggers:

On port open:

USBUART_bmRequestTypeReg USBUART_bRequestReg USBUART_wValueLoReg Comment
0xA1 0x21 0x00 This only appears on a port open.  I can use this for "open"
0x00 0xC2 0x01 multiple occurrences.
0x21 0x20 0x00 4 occurrences
0x21 0x22 0x00 1 occurrence
0x21 0x22 0x0a

1 occurrence

a = 0 if flowcontrol is 'none'

a = 1 if flowcontrol is DTR or RTS.

 

On port close:

USBUART_bmRequestTypeReg USBUART_bRequestReg USBUART_wValueLoReg Comment
0x21 0x22 0x00 2 occurrence

 

You can see I can reliably detect a port open.  However, on a port close event I get two EP0 events but the signals are not different enough from the same signals in the port open event.


Is there a way for the PSoC device to ask the host if the CDC port is open?

>> The USB requests will come from host only, USB device cannot initiate a USB request


Good to know.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

@Rashi_Vatsa ,

Is there a way from the PSoC device to ask the host if the CDC port is open?

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

Some time ago, we had this discussion:

https://community.infineon.com/t5/PSoC-5-3-1/USBUART-component-Is-there-an-API-or-Global-variable-th...

Is this the same request, or something else?

0 Likes

@RodolfoGL 

It is.  However, I found soon after that detecting SOF ISR is not a reliable to determine the port is closed.

The SOF ISR  is always occurring with the port open or closed.   The SOF ISR events stop when the cable is disconnected.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
pacr_284376
Level 5
Level 5
100 replies posted 10 solutions authored 50 sign-ins

Probably not relevant, but maybe you could make use of the VT100 or VT52 emulation ? The are commands to identify e.g. what type of terminal it is... Might be used as a workaround....

https://www.csie.ntu.edu.tw/~r92094/c++/VT100.html

0 Likes

@pacr_284376 

Thank you for your interest in replying.

My goal is to make a significant improvement to my "Term" component.  This is a component I created a while back that allows the user to configure it as a UART-type or USBUART-type.  The API interface is basically identical to the UART v2.50 component so that the application designer can program for that type of API calls and with a simple reconfiguration of a parameter in the TopDesign change to using the USBUART. 

link: Terminal-Support-Component-Library 

My question involves knowing when the host opens or closes the serial port.   I've been able to determine when the host opens a port.  However, I cannot reliably tell when the host closes it.  I'm trying to do this reliably when the user wants to uses ANY terminal program on the host.

As to your suggestion:  I can do as you suggest, but this forces the user to use a specific terminal program on the host.  I'm trying to detect the host port closing on ALL terminal programs.

I even created my own host terminal program (link: PC-Terminal-Program-with-C-Source-Code )and with It I control the DTR signal.  When I open the serial port DTR is active and when I close DTR becomes inactive.   This works.  The trick is that all serial terminal programs have in common opening the port and closing the port.   Not all terminal programs control DTR, RTS. XON/XOFF or other flow control signals.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes