LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending characters VISA serial connection

Hello.

 

I am working on a project where I have to write characters to a PIC in the style of a train controller that controls the direction of a train simulation.

 

I have the basic serial reading and writing working. Labview initially sends a 'S' to start the simulation on the PIC. Then it waits for the PIC to send characters relating to what station the train arrives at. Labview then lights the relevant LEDS on the front panel and then tells the PIC to move with a 'M' 

 

However, I need to add some extra functions. I want to be able to send additional commands to the PIC such as 'J' to jump a station. 'E' to end the simulation and 'R' for reverse, so the train goes back a station instead of forward. I need these commands to be sent to the PIC when the 'M' is considered.

 

So basically user sends a 'J' 'E' or 'R' then send 'M' however I am having trouble with this and not really sure where to put it.

I've attached my VI so if anyone could give me some useful hints I would be very grateful!

0 Kudos
Message 1 of 5
(2,833 Views)

Have a look on the code.

 

VI.png

0 Kudos
Message 2 of 5
(2,826 Views)

Puneet - The event structure is a good idea, but why did you take the case structure outside the loop?  It looks like that's supposed to happen continuously, until the simulation ends.

 

Getox - you'll need a more sophisticated approach to your program.  Eliminate the sequence structure and use a state machine instead.  It's more flexible, and it will remove the current problem that you send an 'M' even when you haven't received any response from the PIC.  One very simple approach would be a single loop with state machine.  In a "Waiting" case you could check if the user pressed a button to send the J, E, or R commands, and if so, take the appropriate action.  In the same case, check if there are any bytes waiting at the serial port (there's a VISA property for this).  If there are, read the data from the serial port, update the indicators, and send the 'M' command.

0 Kudos
Message 3 of 5
(2,820 Views)

Okay so replace the stacked sequence with a case structure. How will this effect my code?

 

Should I use Punneets idea of taking the VISA R and LED control outside the case structure and then make the default case as write 'M' unless user has entered 'R', 'J' and 'E' for the other cases? 

0 Kudos
Message 4 of 5
(2,803 Views)

Looking at your code more closely... why do you have your Start button wired to the "Enable Termination Char" input of VISA Configure Serial Port?  That doesn't make any sense, nor does the comment next to it.  Outside the while loop, why are you reading a local variable of each indicator and then writing that same value to the indicator terminal?  That's not doing anything.  It would be better to get rid of the local variables entirely.  For example you could do something like this:

indicator array.png

 

Now, on to your question.  What do you mean by "how will this affect my code"?  Try writing it and see.  Start by just duplicating the code you have now, but with a shift register, an enumeration, and 4 cases (matching the 4 states in your current sequence structure). Once that works, you can probably simply add some code to the first case that checks if any command button has been pressed, and if so, send the corresponding character to the serial port.

0 Kudos
Message 5 of 5
(2,793 Views)