LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

RS232 event structure reading

Solved!
Go to solution

Hi, 

 

I am using the attached RS232 Read Write.vi as my starting point and am making RS232 - help.vi from it. the origional successfully writes 1 messages, then waits a bit, then reads 1 message. What I'm trying to do is to be able to read multiple messages. So, I would write 1 message and I would like to read 1, 2, or 3 messages back, or however many messages happen to come back I guess. 

 

Here is how I modified the origional vi to get where I am at now:

I tried adding an event structure around the "read" half of the RS232 program. My event structure would trigger when the bytes to read changes, this value could go from 0 to multiple bytes to read, or muliple bytes to read to 0. So half of the time I would trigger and not need it, so I then added the case strucuture which only operates when the bytes to read value is not zero, meaning that there should be something waiting to be read every time the program enters this case statement. (hopefully this is clear). 

 

Right now, my program wont read and I am not sure why. Using the first base program, I can read and write properly. (I just hooked up pins 2 and 3 on my cable so that I read whatever I write for testing purposes). So I know the problem is me messing up the 2nd attached program 😛 

 

Any help would be appreciated! Thanks.

 

EDIT: I added 2 .jpgs incase you guys are on a different labview version. 

0 Kudos
Message 1 of 10
(5,689 Views)

Sorry for double post, wouldnt let me edit and add this last .jpg. Thanks. 

0 Kudos
Message 2 of 10
(5,684 Views)

First, don't put the VISA close inside the loops.  (And certainly not twice, once in each loop).  You could be closing the serial port at the same time you might be reading data in on the other loop.

 

Second, read up on Event Structures.  The event structure for Value Change will occur when the user changes the value in the control, or when the code programmatically writes a value to the Value(Signalling) property node for that control.  Since your Serial "Settings:Number of Bytes at Serial Port" is an indicator, the first thing will never happen.  And since you aren't using the Value(Signalling) property node, the 2nd thing won't happen.  So your top event structure will never execute.

 

In your bottom event structure, you have problems as well.  If you the user enters information into the "string to write" control, the event structure will fire.  But if the Write switch is not on, nothing will actually happen in your event case.  If the user later turns the write switch to True, nothing will happen because the event structure won't fire again until the "string to write" control is changed by the user.  That would be very confusing to the user of your VI.

 

Why are you trying to use event structures like this for serial communication?

 

There are events you can wait on for VISA in the VISA >> Advanced palette, but I've never found a reason to use them.  I would just rely polling to monitor the Bytes at Port property node.

 

 

Message 3 of 10
(5,676 Views)

Thanks, I'll take your advice and try to fix things up a bit. I guess the reason for using event structures is because they kind of "automate" what happens without polling, but if there is a VISA wait command and polling on a VISA is not too bad, then this would be much better. As for the reason why I am doing this is because the origional .vi only will read 1 message for every 1 message sent, and in my application I may have multiple messages to read. 

0 Kudos
Message 4 of 10
(5,643 Views)

Alright I got it working using polling! Thank you sir. 

 

Here is what I did:

1) VISA closes are now outside the loops. I didn't think of closing the port and reading at the same time. Good call.

2) I took away the event structures so now I can poll for reads every 500ms (or any val). This is working well. 

3) Changed the bottom event structure and moved the case statement outside so that the toggle switch becomes more user friendly (I'll be the only person using this, but its good to make things better). 

 

Few more general follow up questions:

1) In this example, I have a few while loops and would like them all to be "synced" to stop at the same time. In general, what is the best/easiest/your favorite way to synchronize multiple while loop stop functions? Property nodes (value)?

 

2) Kind of a vague question... But this section of code will be used in a larger Vi I have made. In that other code, I have an event structure that checks operates when the read buffer has changed, then in a case structure, I make sure that the read buffer is also not empty. Basically, if read buffer gets a new value that's not black, I decode that value and do some other bit checks and stuff, not just constantly decode because after every message decode I store this in an array then later save to a file. This makes sure that my file output only has data that is useful, not a bunch of black reads. If anything sounds funny or inefficient here let me know. (Can't post the code but just wondering if anything sounds out of place). 

 

Again, Thanks. Everything else looks fine to me, but if you see anything feel free to critque. 

Message 5 of 10
(5,633 Views)
Solution
Accepted by topic author g_e_m_i_n_i

Better.

 

But I don't think swapping the event structure for the case structure actually really helped anything.  Actually it madet things worse.  You had the event case set to Lock Front Panel until Event Completes.  But with the event structure inside the case structure, there is a chance the event structure will never execute and thus the front panel will remain locked up forever.

 

1.  You only want 1 VISA close.

2.  Event structure has been fixed.  I also added an event for the write switch being switched.  So now it will write whether you change the string or when you flip the switch to True.  (Note, you won't be able to continually send the same string like the original example has because you need to either flip the switch or change the string.

3.  I used the event structure to stop the writing loop as well by trigger on the event that the stop button has been pressed.

4.  I don't know what you mean by a "black" read.

Message 6 of 10
(5,625 Views)

Oops - I meant "blank", not "black". That is my fault. I'll check out your code now. Thanks. 

0 Kudos
Message 7 of 10
(5,623 Views)

1) I do not need any other applications to use the serial port, so closing the port is more of a "good thing to do"? Also, I got it now. I only made 1 VISA port so closing it twice does not make sense. Thanks. 

 

2) This doesn't quite work. What you have makes sense and I think should work, but when I try your VI out I cannot manually update the string. I would have to toggle the switch every time, not just update the string to write. In my overall program I have a message creator with a send button, which will eliminate the need for that switch, so no need to worry about that. Either way, I'm getting rid of that switch so just disreguard it. 

 

3) I do not understand how the "newval" box works on the left side of the event structure. I found the event structure help file here, but this didn't seem to cover that little side box. Could you clarify how that works, or just link explaining it would be fine. I can't seem to find anything on it. Smiley Sad

 

4) blank not black 😛 sorry

 

Thanks again!

0 Kudos
Message 8 of 10
(5,619 Views)

@g e m i n i wrote:

 

2) This doesn't quite work. What you have makes sense and I think should work, but when I try your VI out I cannot manually update the string. I would have to toggle the switch every time, not just update the string to write. In my overall program I have a message creator with a send button, which will eliminate the need for that switch, so no need to worry about that. Either way, I'm getting rid of that switch so just disregard it. 

 

It works just fine for me.

 

3) I do not understand how the "newval" box works on the left side of the event structure. I found the event structure help file here, but this didn't seem to cover that little side box. Could you clarify how that works, or just link explaining it would be fine. I can't seem to find anything on it. Smiley Sad

 

Seach for "newval" in the help system.

 

 

 

Thanks again!


 

Message 9 of 10
(5,607 Views)

Ahh, thats the word I needed to search. Got it.

 

Thank you & have a great weekend! 

Message 10 of 10
(5,605 Views)