LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Questions regarding serial port read and write simultaneously

Solved!
Go to solution

Hi I am creating a UI for serial port communication where it has basically 2 front panels, one where the user enters commands and the other where the prints from UART comes. I had initially thought of using a state machine but the read and write maybe independent at times and hence I cannot rely on states. I have searched quite a bit on the forum and it has left me even more confused. Please help .

 

1) In one thread duplicate visa Sessions was used for simultaneous read and write, is that recommendable ? how does it affect the performance?

 

2) Basically when the vi is reading data is should constantly display it as well , however someone said that it takes up too much memory to use shift registers, then how should I go about it? if use a queued state after the read loop does it affect the read loop and become sequential?

Moreover is there anyway to move the cursor the latest data in the indicator

 

3) For command input from the user, suppose the user enters some command in the control and presses enter , then write visa is envoked, but if he enters some other string and presses enter then write should be invoked again.. is that possible? wihout clearing the previous commands in the control?

 

4)According to my understand the wait for event does not hog resources and write can go about parallely, am I correct?

 

Thank you. I have attached a very basic vi which I have developed but I want to make it more robust. Please help specially in the UI part.

0 Kudos
Message 1 of 7
(4,144 Views)
Solution
Accepted by topic author su_a

su_a,

 

1) You can only have one session for one port. Many UARTs can handle full duplex so perfomance is not affected.  At high data rates and large amounts of data, buffering and OS latencies may become an issue.

 

2) Who told you that shift registers use too much memory? Generally shift registers are the preferred way to pass data from one iteration to the next. Concatenating strings inside a loop (shift register or not) causes the string to grow and may require re-allocation of memory.  Your VI never clears the string so its length cold get quite large.

 

You generally do not have an active cursor on an indicator.  If you want to always display the most recently received characters, turn on the vertical scrollbar and use a property node to keep it scrolled to the bottom.  This may be annoying to users if they try to move the scrollbar manually and find that the program keeps moving it back automatically.

 

3) If the user has changed the value in the Command string, when he hits enter the Value Changed event fires.  Simply hitting enter again does not change the value and does not fire the event.  The control need not be cleared but the value in it must change.  If you want to send the same command again, having a Send Command button might be a better choice.

 

4) Write is inside an event case.  It is not in parallel with anything.  The event structure does not hog resources.  The other loop will run while it waits.

 

The event loop will not stop when the STOP button is pressed. Likely it wll take two Command: Value Change events after STOP before the lower loop stops.  Replace the Timeout event (which never times out) with a STOP: Value Changed event and wire a True from that case to the termination terminal.  Remove the local variable.  Make the mechanical action Latch When Released.

 

Lynn

0 Kudos
Message 2 of 7
(4,131 Views)

Lynn

 

Thank you for the detailed answers and clearing so many of my doubts.

 

Who told you that shift registers use too much memory? Generally shift registers are the preferred way to pass data from one iteration to the next. Concatenating strings inside a loop (shift register or not) causes the string to grow and may require re-allocation of memory.  Your VI never clears the string so its length cold get quite large.

 

yes that is intentional. the string is not sleared so that the user can see all the prints fron the UART and not only the most recent one.. However you are correct it might become too large after a time. Hence I was worried about using shift registers. Any suggestion as to how I should go about it?

I had initially thought of sending the data from UART to a queue so that data is received immediately and then once string exceeds a certain size it should only keed the last say 200 bytes. Would that be a good approach? How do i extract n bytes from a string?

 

You generally do not have an active cursor on an indicator.  If you want to always display the most recently received characters, turn on the vertical scrollbar and use a property node to keep it scrolled to the bottom.  This may be annoying to users if they try to move the scrollbar manually and find that the program keeps moving it back automatically.

 

Yes you are correct. I will surely give that some thought

 

If the user has changed the value in the Command string, when he hits enter the Value Changed event fires.  Simply hitting enter again does not change the value and does not fire the event.  The control need not be cleared but the value in it must change.  If you want to send the same command again, having a Send Command button might be a better choice.

 

No the command changes. I tried various options, including disabling limit to one line, then tab is the data eliminator that triggers the event. I would discuss with others. Maybe we coud just read it fron an array or an excel file one row at a time...

 

Thank you for the suggestion, I have modified the vi and removed the local variable.

 

 

0 Kudos
Message 3 of 7
(4,115 Views)

Heres what I have till now. I added a producer consumer palette for read.

 

1)In the consumer loop I have to only keep say the last 200 bytes and rest dump in a file. How to extraxt the n bytes?

 

2)The whole write/read operation stops with the stop button.

 

3)The boolean in write model is basically a dummy write now. Actually the consumer loop will search for a pattern ,once found it triggers an event in the write loop so that the next elemnt from file is read Any advice?

Please help

0 Kudos
Message 4 of 7
(4,108 Views)

To extract a portion of the string use the String Subset function in the String palette.  If you want to keep all the data, write it to a file before extracting the subset.

 

Why not just put the Write in the consumer loop? Read the data from the file initially as you do now.  Pass the data into the loop.  When the pattern is found, use Index Array to get the next element.  Since the consumer loop will iterate more often than you want to write an element from the file, keep the next index in a shift register and increment the value when you write.

 

Lynn

0 Kudos
Message 5 of 7
(4,089 Views)

To extract a portion of the string use the String Subset function in the String palette.  If you want to keep all the data, write it to a file before extracting the subset.

Oh! how did I miss that one. Thank you so much

 

Why not just put the Write in the consumer loop? Read the data from the file initially as you do now.  Pass the data into the loop.  When the pattern is found, use Index Array to get the next element.  Since the consumer loop will iterate more often than you want to write an element from the file, keep the next index in a shift register and increment the value when you write

 

Yes its a valid suggestion but I think my mentor wants me to keep the read and write modules separate. I will surely give it a try and check the efficiency after integrating with the hardware. Thanks for all the help 🙂

0 Kudos
Message 6 of 7
(4,068 Views)

I have removed the producer consumer model since I have been advised to tryand design without it. Any suggestion as to how I can separate the read and write loops??

0 Kudos
Message 7 of 7
(4,062 Views)