|
Post by msteffes on Jul 14, 2022 16:49:52 GMT -5
Hello All,
Is there a best practice code approach to recover a running serial communication session program after the virtual serial port is lost... serial/usb converter connection is severed. Currently my test program just crashes and takes LB down with it. oncomerror never has a chance to trap it. I want to stay with pure LB code if possible rather than any API calls.
oncomerror [handleIt]
[MainProg]
oncomerror [comLPtrap]
open "Com3":9600,n,8,1,ds0,cs0,rs" for random as #commHandle
timer 500, [sendStuff] wait
[sendStuff]
send$ = " junk "
#commHandle,send$;
WAIT
[comLPtrap]
'disable the com error handler
oncomerror
'print out the error and port
print "Error: "; ComError$
print "Port number: "; ComPortNumber
print "Error code: ";ComErrorNumber
end
|
|
|
Post by Rod on Jul 15, 2022 2:36:55 GMT -5
I would need to dig out my serial kit to experiment. You need to check the port is open before you send it data. It might just be a case of checking that #commhandle is not zero. Or reset and set oncomeerror just ahead of the send. Or it might be better to open and close the port for every send.
If I am reading a port in a timed loop I always program a watch timer. Every time you go looking for data from the port, increment a counter. If you get data set the counter to zero. If the counter gets to a certain number then you have had no data for a while so stop the timed loop.
You may need to use the API calls to check DTR or RTC ahead of the send.
What if anything is printed to the error.log orenich exact line does it fail on running in debug mode?
|
|
|
Post by msteffes on Jul 15, 2022 12:04:18 GMT -5
The actual program is a poll and response communication loop using a RS485 to usb adapter. Turning the port on and off would be problematic. I have to know when the response fails whether it is a bad packet or a broken RS485 line. But i would like to avoid the crashing of the program if the usb gets unplugged. It is a diagnostic tool that I am working on , so if I have to live with a possibility of a crash, it is not the end of the world.
Here is the error log from the example code when I pull the usb connection while running.
Error log timestamp Friday 07/15/22 10:30:28 AM
Runtime error: "isEmpty" not understood
MessageNotUnderstood>>defaultAction MessageNotUnderstood(Exception)>>activateHandler: <anUndefinedObject> MessageNotUnderstood(Exception)>>handle MessageNotUnderstood(Exception)>>signal MessageNotUnderstood class>>message: <aMessage> UndefinedObject(Object)>>doesNotUnderstand: <aMessage> BasicProgram>>terminateRun: <aMessageNotUnderstood> [] in BasicProgram>>errorHandlerBlock ExceptionHandler>>evaluateResponseBlock: <aBlockClosure> for: <aMessageNotUnderstood> [] in ExceptionHandler>>handle: ProtectedFrameMarker(BlockClosure)>>setUnwind: <aBlockClosure> BlockClosure>>invisibleEnsure: <aBlockClosure> ExceptionHandler>>handle: <aMessageNotUnderstood> ExceptionHandler>>findHandler: <aMessageNotUnderstood> MessageNotUnderstood(Exception)>>activateHandler: <anExceptionHandler> MessageNotUnderstood(Exception)>>handle MessageNotUnderstood(Exception)>>signal MessageNotUnderstood class>>message: <aMessage> UndefinedObject(Object)>>doesNotUnderstand: <aMessage> SerialDevice32(Object)>>osError: <anUndefinedObject> SerialDevice32>>write: <' junk '> SerialDevice32(SerialDevice)>>nextPutAll: <' junk '> BasicCommStream(BasicFile)>>writeItem: <' junk '> [] in PrintCommand>>device:handle: BlockClosure>>value: <aBasicProgram> value: <'#commHandle'> value: <aBasicStringContext> BasicTripleParameterContextHolder>>value [] in BasicProgram>>begin ExceptionHandler>>evaluateProtectedBlock: <aBlockClosure> [] in ExceptionHandler>>activateDuring: ProtectedFrameMarker(BlockClosure)>>setUnwind: <aBlockClosure> BlockClosure>>invisibleEnsure: <aBlockClosure> ExceptionHandler>>activateDuring: <aBlockClosure> ExceptionHandler class>>handle: <anError class> with: <aBlockClosure> during: <aBlockClosure> BlockClosure>>on: <anError class> do: <aBlockClosure> BasicProgram>>begin BasicProgram>>gotoAndIfStoppedBegin: <'[sendStuff]'> BasicProgram>>handlerName: <'[sendStuff]'> evaluate: <aBlockClosure> callParameters: <anOrderedCollection> BasicProgram>>submitHandlerName: <'[sendStuff]'> evaluate: <aBlockClosure> callParameters: <anOrderedCollection> BasicProgram>>submitHandlerName: <'[sendStuff]'> callParameters: <anOrderedCollection> TimerTopPane>>wmTimer: <722766> with: <0> NotificationManager>>notify: <aWinMessage> NotificationManager>>notifyRecursive NotificationManager>>recursiveMessage SystemDictionary>>recursiveMessage SystemDictionary>>launch NotificationManager>>readWinQueue NotificationManager>>runEventLoop Message>>perform Message>>evaluate Process>>safelyEvaluate: <aMessage>
|
|
|
Post by Rod on Jul 15, 2022 13:27:38 GMT -5
I wont be able to get to my serial kit for a while. You might try opening the port again while it is open. You will get an error but it will vary on whether the port is busy or has no connection. Not able to get to my pc but I have posted code in the past that lists available ports, that might get you some info.
maxPorts=40 dim port$(maxPorts) statictext #main.txt, "Select Port", 75, 55, 100, 20 combobox #main.portcb, port$(), [portDoubleClick], 75, 75, 100, 100 open "Get port example" for window as #main print #main, "trapclose [quit]"
' Populate the drop down list of available COM ports gosub [getPorts] wait
' Handle the combobox doubleclick event [portDoubleClick] print #main.portcb, "contents? Com$" open Com$;":9600,n,8,1,ds0,cs0,rs" for random as #com wait
[getPorts] for port = 1 to maxPorts port$(port)="" next index=1 ' now find all active ports for port = 1 to maxPorts oncomerror [trap] open "Com";str$(port);":9600,n,8,1,ds0,cs0,rs" for random as #com port$(index)="Com";str$(port) index=index+1 close #com
[trap] oncomerror next print #main.portcb, "reload" print #main.portcb, "selectindex 1" return
[quit] close #main end
Use this and see what the errĀ§ is for the established port plugged and unplugged.
|
|
|
Post by Rod on Jul 25, 2022 3:01:08 GMT -5
I just noticed this command which might help "watchtime" a port and warn you that info is not being sent.
|
|