Lost serial port error

Just starting out? Need help? Post your questions and find answers here.
Cezary
Enthusiast
Enthusiast
Posts: 108
Joined: Sun Feb 12, 2017 2:31 pm

Lost serial port error

Post by Cezary »

Dear forum users,

I'm looking for some way to signal that the serial port has been lost (for example by unplugging the USB-SERIAL converter). The recommended function IsSerialPort() does not return an error (zero value) if the port was previously opened correctly (I tested it in Windows). So how can I determine that something happened while the program is running?

Best regards
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: Lost serial port error

Post by HwyStar »

I know "zilch" about USB/Serial ports. So...

What about a window timer that checks the status of the port (GetSerialPortStatus())?
Cezary
Enthusiast
Enthusiast
Posts: 108
Joined: Sun Feb 12, 2017 2:31 pm

Re: Lost serial port error

Post by Cezary »

HwyStar,
thanks for your answer.
I am not using any port input line other than RxD, so GetSerialPortStatus() always returns 0 for all lines (RI, DCD, DSR and CTS), but...
Serial port initialized with parameter #PB_SerialPort_NoHandshake after calling the GetSerialPortStatus() function with the parameter #PB_SerialPort_XonCharacter or #PB_SerialPort_XoffCharacter returns 17 or 19. And if I disconnect USB plug this character codes becames 0!
Maybe that's a good way? I have to check if it will be repeatable for different converters.

Thank you!
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: Lost serial port error

Post by HwyStar »

I can't say I helped. But, timers do come in really handy!
Cezary
Enthusiast
Enthusiast
Posts: 108
Joined: Sun Feb 12, 2017 2:31 pm

Re: Lost serial port error

Post by Cezary »

In my application serial ports are read periodically in separate threads, which is equivalent to using a timers.
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Lost serial port error

Post by HeX0R »

Here is a windows-only possibility:

Code: Select all

Procedure CallBack_NewDevice(WindowID, Message, wParam, lParam)
	Protected Result, a$
	Protected *db.DEV_BROADCAST_HDR

	Result = #PB_ProcessPureBasicEvents
	Select Message

		Case #WM_DEVICECHANGE
			If wParam = #DBT_DEVICEARRIVAL
				*db = lParam
				If *db\dbch_devicetype = #DBT_DEVTYP_PORT
					a$     = PeekS(*db + SizeOf(DEV_BROADCAST_HDR))
					Debug "New Port Connected: " + a$
				EndIf
			ElseIf wParam = #DBT_DEVICEREMOVECOMPLETE
				*db = lParam
				If *db\dbch_devicetype = #DBT_DEVTYP_PORT
					a$ = PeekS(*db + SizeOf(DEV_BROADCAST_HDR))
					Debug "Port Disconnected: " + a$
				EndIf
			EndIf

	EndSelect

	ProcedureReturn Result
EndProcedure


OpenWindow(0, 0, 0, 300, 200, "Test Ports", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowCallback(@CallBack_NewDevice(), 0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Cezary
Enthusiast
Enthusiast
Posts: 108
Joined: Sun Feb 12, 2017 2:31 pm

Re: Lost serial port error

Post by Cezary »

HeXOR,
impressive knowledge.
Thank you.
Post Reply