LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Write ... end of transmission indication

I have a simple requirement... I want to know when all the bytes in the buffer have been sent during a Serial Write.  Refer the simplified VI below showing only the basics for this requirement. 

If I can somehow know the end of transmission , I can use that signal to change teh State of QSM and get back to the INIT state and wait there.  I searched the Serial property nodes but could not locate a suitable one for this .. Any ideas ??

( I tried to alter the loop delay ... low values means some bytes dont get sent; high values means a repeated trasnmission; difficult to control based on this )

Serial Write.png

 

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 1 of 11
(2,690 Views)

why would some bytes not get sent?  If you are able to feed the bytes to the VISA Write buffer, then they all ultimately should be written.

 

You could estimate how long it takes to send them based on the number of bytes and the baud rate..

 

But why do you need to know?

0 Kudos
Message 2 of 11
(2,686 Views)

@RavensFan wrote:

why would some bytes not get sent?  If you are able to feed the bytes to the VISA Write buffer, then they all ultimately should be written.

 

You could estimate how long it takes to send them based on the number of bytes and the baud rate..

 

But why do you need to know?


At a baud of 9600 , each bit needs about 0.104ms. This means each byte will need about 10x0.104 which is 1.04ms and for 400 bytes this should be about 400ms. 

 

True... its unnecessary effort to get to know this.  Actually my impression was that once I load the Serial buffer it should be sent out in totality. But what I am not clear is the role of the while loop … does the Serial Write over ride the delay set for that [or] irrespective of the transmission state the while loop cuts in ?  I tried both the Synch and Asynch mode of the Serial Write,  to no luck .

 

And just to know if the external Serial device receives my data OK,  I tried with about 25 bytes and of course it went through fine. 

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 3 of 11
(2,670 Views)

The Write function returns when it's done writing to the VISA buffers, which is as low-level as I think you can get. Once it's in the VISA buffers it's getting sent to your device. I think your code may have some other issues if a low loop delay means some values don't get sent- your "SEND TO MCU" case will fully send the "Profile data" out the serial port each time it's called. You only need to call it once.

 

Also, a few thoughts on your coding in general:

-I don't think your Loop Delay is helping you here. Do you have a reason you need to clock it to 50 ms? (By the way that's a Wait Until Next ms Multiple function, not a Wait, so the delay in each case execution will vary from 0 to 50 ms.) The Queue will be your timing speed limiter, as it will block execution until you get a new message.

 

-Your structure is actually a QMH (Queued Message Handler) not a QSM (Queued State Machine). It's a fine architecture for what you're doing, just wanted to point it out in case you were looking for further reading on the subject.

Message 4 of 11
(2,667 Views)

I don't know if it's possible with whatever device you are using, but when I've had a similar situation (sending to an instrument and wanting to know if it's "done"), I would send the information, and then follow that up with the simplest query possible (something like asking for its serial number).  Then, when I got that reply, I would know that all the information sent before the query has made it there.  Can you do anything like that?

Message 5 of 11
(2,663 Views)

@MogaRaghu wrote:

 

And just to know if the external Serial device receives my data OK,  I tried with about 25 bytes and of course it went through fine. 


What is your external serial device?  Do you have control in how it works?

 

Most devices will send back a reply saying something like "OK", once it has received the message.  Perhaps if you are sending it a command to change the setting, it will reply back with the value of the setting that was changed.  You can then do a VISA Read and see if it replied.

Message 6 of 11
(2,658 Views)

Ok going by what most of you have replied I think I will implement a "OK" response for the Microcontroller which gets the data. And since I am writing that code also, this is no issue. 

 

There was one remark about the way I had created the delay for the while loop. I was under the impression that the function "Wait until next ms Multiple " will provide a fixed 50ms delay.  In fact I just checked this by measuring the loop timing and it was between 49 to 50ms. And the CPU was using just 0.2% resource.  Whereas if I remove the delay function and let the while loop run, the timing is 0 ms and the CPU jumps to 13% … I guess I need some clarity on this. 

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 7 of 11
(2,642 Views)

Wait until Next ms Multiple will wait until the next time the system clock hits a multiple of 50 ms. So if you call it when the system clock is at xxxxxxx34, it will wait another 16 ms before returning.

 

Your issue is that your Init state keeps putting another Init back into the queue. In other words, when Init is called, it itself calls Init, which calls Init, and so on repeatedly. This is a mistake. Remove the Enqueue function from your Init case and the loop will now only execute the states that you DO call. When you enqueue Send to MCU, it will call that case once, then wait at the while loop before doing anything else. Right now, the only option is Send to MCU, so it will do nothing until you tell it to Send to MCU again.

 

Also, I'd suggest getting rid of the Flush Buffer in your Event handler. That's guaranteed to cause you headaches in the future. This function will get rid of everything in the queue, and should generally be used in the "shutdown" or "abort" sequences.

Message 8 of 11
(2,638 Views)

@BertMcMahan

 

Thanks. That INIT in INIT is a spill over from the QSM to QMH !! 

 

And the Wait until… function. I agree that the first iteration is going to be flawed but subsequent ones should be "reasonably" accurate within +/- 1ms ?  Or no ??

 

And why do I flush the Queue buffer .. most or all machine control cases are one at a time stuff. In fact if I had a case lurking somewhere in the pipeline , its dangerous. 

 

Or maybe I am missing something which you are warning about ? 

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 9 of 11
(2,613 Views)

The bottom loop will not execute at 50 ms. It will execute each time the Dequeue function gets a message. If you are sending repeated INIT's, it'll hit every 50 ms, but if you take that out, the Wait until... will wait its 50 ms, but then the loop will wait on the Dequeue function, since there's nothing in the queue.

 

"Cases lurking in the pipeline" is a sign of poor programming architecture- if you're getting cases you don't need, eliminate the source of the extra cases. If you have enqueued cases that you've decided you don't actually need, you can discard anything already queued up (hence why they're used in Abort or Stop scenarios).

 

There is a possibility you could hit the "Send" button for some piece of code multiple times resulting in multiple triggers, but a better way to handle that is to prevent the bad user interface from happening in the first place. Set Busy will make the cursor unable to click anything until Unset Busy is called, and it gives your user an indication that "something's happening".

0 Kudos
Message 10 of 11
(2,595 Views)