Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: plcnut on January 16, 2013, 09:00:31 AM

Title: STREAMIN Serial in a stage
Post by: plcnut on January 16, 2013, 09:00:31 AM
I am using a STREAMIN for a serial port on a H2-SERIO-4 in a stage. Does it tie up resources when I close the stage before the STREAMIN can complete (by recieving a delimiter or timing out)? Or is the STREAMIN instruction merely monitoring a buffer for data?

Title: Re: STREAMIN Serial in a stage
Post by: franji1 on January 16, 2013, 09:47:45 AM
The STREAMIN instruction must continue to execute until it completes (either successfully or in error).  Hence, you should keep its SG ENABLED until the STREAMIN is done.

If you jump out of a Stage prematurely, the "termination" of the Stage includes CANCELING the STREAMIN operation.  This "termination" is the same mechanism that turns OUT coils OFF when you transition out of a Stage.

This is one reason why we added the Jump To Stage options for the On Success and On Error events in asynchronous instructions like STREAMIN.  You don't HAVE to transition out of the stage when its done, but typically, the completion of an asyncronous operation like STREAMIN fits well in a typical stage-flow diagram.
Title: Re: STREAMIN Serial in a stage
Post by: franji1 on January 16, 2013, 09:52:30 AM
I may have mis-understood your question.  Yes, you CAN JMP out (or SGRST or even EXIT/HALT the PROGRAM code-block that contains the Stage containing the STREAMIN), and the STREAMIN instruction will properly "terminate" and clean up any resources it was using (specifically the serial port).
Title: Re: STREAMIN Serial in a stage
Post by: plcnut on January 16, 2013, 11:05:24 AM
I may have mis-understood your question.  Yes, you CAN JMP out (or SGRST or even EXIT/HALT the PROGRAM code-block that contains the Stage containing the STREAMIN), and the STREAMIN instruction will properly "terminate" and clean up any resources it was using (specifically the serial port).

Thanks Franji1, This is what I needed to know. In my testing it does set an error whenever I JMP out of that stage, but I can live with that.
Title: Re: STREAMIN Serial in a stage
Post by: franji1 on January 16, 2013, 11:35:31 AM
However, the physical port may continue to receive characters from the remote device, so you want to do a DEVCLEAR to flush any "straggling characters" if you ever "prematurely" cancel a STREAMIN.  There is also an option on a STREAMOUT to "Flush INPUT Device First".
Title: Re: STREAMIN Serial in a stage
Post by: BobO on January 16, 2013, 11:41:47 AM
I think this has already been explained...but...a triggered STREAMIN should complete normally before leaving the stage that it is in. We added some exception handling that will clean it up if you do not let it finish, but that really is bad form and will give you a warning, which is what you are seeing.

As for resources, yes, the instruction locks the device and holds it until complete. This is what eliminates the external interlocking that used to be a big issue with DL CPUs.
Title: Re: STREAMIN Serial in a stage
Post by: plcnut on January 16, 2013, 07:55:04 PM
I have a device that may take 2 seconds to respond, or it may take only 400msec. to respond, all depending upon a variable conveyor speed. How do I properly terminate a streamin that needs to have a variable timeout?
Title: Re: STREAMIN Serial in a stage
Post by: franji1 on January 16, 2013, 08:24:27 PM
If you know the speed immediately before needing to use the STREAMIN, you will need to use two instances of the STREAMIN instruction. One speed use the "fast" STREAMIN, the other uses the "slow" STREAMIN.
Title: Re: STREAMIN Serial in a stage
Post by: BobO on January 16, 2013, 09:08:10 PM
Simple. Don't invoke the STREAMIN until the data is available. Look in the port device's structure. There is a member that contains the number of bytes currently in the input queue. Wait for one or more bytes to show up, applying whatever timing control you wish in that logic, then invoke the STREAMIN after the data is coming in. I rarely use the built in timeout for protoccol timing...I normally verify that the data I'm expecting is already available before I call it.
Title: Re: STREAMIN Serial in a stage
Post by: plcnut on January 17, 2013, 07:39:53 AM
Thank you Bobo!

I remembered seeing that you did that in the HttpGET example, but had since forgot about it. You guys thought of about everything! I will make the change to my program this morning and see how it goes.
Thanks again.  ;D
Title: Re: STREAMIN Serial in a stage
Post by: plcnut on January 17, 2013, 10:43:44 AM
What about OPENDEV and CLOSE with a serial device? Do I need to open and close my serial port?
It works fine and I haven't programmed an OPENDEV for the port, so why and when are these instructions necessary?
Title: Re: STREAMIN Serial in a stage
Post by: BobO on January 17, 2013, 10:50:42 AM
You do not.

OPENDEV is for creating a device handle...an indirect reference to a device...for cases where you would like to develop a protocol in a generic way and then specify the device at runtime.

CLOSE is for closing any device opened with an OPENXXX call like OPENDEV, OPENTCP, or future handle centric devices like files.
Title: Re: STREAMIN Serial in a stage
Post by: plcnut on January 17, 2013, 02:09:26 PM
Cool, Thanks Bob.