LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Work with array and serial communication

Solved!
Go to solution

Hello,

I have the following VI that I am developing, it's a game similar to Battleship, where through serial communication I send a coordinate (row, column; example A5), and my rival, (also through serial communication answers) "1" if I hit it or a "0" if I didn't hit it, once it was confirmed to fill in the respective box, (which is an array) with red, if it hits and a different colour if it doesn't match, and this is where I don't know how to continue. I appreciate your suggestions.

best regards!

0 Kudos
Message 1 of 8
(2,940 Views)

First, consider making radio buttons.   What happens if someone hits A B 3 4 5 then hits send.  The other PC is going to receive AB345.  Rather than a D4 or something simple. How should a VI decode that?  If you had 2 groups of radio buttons one for letters (rows) and one for the digits (columns), then you can only press one button at a time.  You should also have some input checking to make sure a letter and a number have been pressed before sending.

 

Why are you receiving 5 bytes?  It seems your expected message would only be 2 bytes long  (letter,number).  How do you know when a read occurs that you've received the whole message?  It's possible when the one PC reads, it has only received the letter.  The number won't arrive until the next read.  (Well maybe/maybe not, it all depends on the loop rates, and timeout values, and random chance in the event a timeout occurs before the 2nd character arrives.)  Also, right now you are receiving 5 bytes which means it is going to read two and half messages (A1B2C) before it returns anything.  You should use a better protocol that lets you know you've received a complete message.  Try sending a termination character.  Then read a large number of bytes and the message will wait until it gets the termination character and you know you have a complete message.

 

I would also use an event structure so that you aren't sending messages, or no messages, every 5 milliseconds.

 

You should also lay out the program flow on a piece of paper, either a flow chart or pseudocode before you ever start laying down wires on the block diagram.

 

You really haven't defined what a hit is or a miss.  How similar is this supposed to be to Battleship.  So far it seems like you have half a game and no definitions of the ships.

 

As for storing existing positions and colors, you need to put your array into a shift register so that it gets updated anytime it decodes a message.

0 Kudos
Message 2 of 8
(2,905 Views)
Solution
Accepted by topic author WILSTi

So to start, I'd like to suggest something like the following:

Example_VI.png

Here I replaced your 10 separate booleans with a cluster of two radio button sets, each containing the 5 appropriate buttons. You can adjust the aesthetics to try and make it look nicer like your original if you care to take the time 🙂

 

Beyond the advantages offered by Radio Buttons (only one selected in a group), the additional advantage is that I can easily control the string formatting, and it also takes much less block diagram space. I used an Event Structure to control when I want to "send" data.

 

I'd also like to point out that you're using the Termination Character for your serial read (good!) but you're not sending one ( 😞 ). Try using a LF constant on the end of your concatenate string (or with my code and a concatenate string) before writing to the VISA Write node.

 

In terms of populating the array, with the current setup (array of Rings) you probably want to use Replace Array Subset after determining your indices.

A modified example here shows a way to determine the indices. Two methods are given for the number, but there are probably cleaner ways to get the Letter Index. This is perhaps just the simplest.

Example_VI.png


GCentral
Message 3 of 8
(2,900 Views)

Hello, thank you very much for your help, it has helped me a lot!
However, I havent been able to fill the ring matrix with the column and row indices. I have used Replace Array Subset, but when making the changes the output matrix is not taking them.

Will you have any example or tutorial that can help me for this?

Thank you!

0 Kudos
Message 4 of 8
(2,855 Views)

I don't know what you mean by "Ring Matrix."  I don't see a matrix of any kind in your VI.

 

If you've progress further than the VI you attached in the original post, then please attach that so we can see where you are now.

0 Kudos
Message 5 of 8
(2,848 Views)

Hello,

1.) Yes, please excuse me it is a confusion of mine to call 2D arrayas as a matrix, they are indeed arrays.

2.) I enclose the VI that I have so far and request help since I have the following difficulties:

1. Serial communication is very slow, I would like to know if this aspect can be improved or due to something badly configured in my VI.

2. I have not been able to fill the Array with my opponent's ships, I can modify a position, but by changing the position, the result of the previous position returns to its original value.

According to the previous posts change some things like radio buttons and include line endings in each string.

VI operation:

1.) On the buttons my ships, I locate the position of my ships according to the rules of the game.
2.) Once located I press the button "send ships to board". Here, I send in a string that begins with an S, the positions of the ships to my development board.
3.) The game begins, the first turn belongs to my opponent, it gives me a position (row, column) and through my development board, I answer 1 if I hit or 0 if I fail.
4.) For my turn, using the radio buttons, I select the coordinate and press the SEND button. My opponent answers me with a 1 if I hit or with a 0 if I failed, this data is what I couldn't keep in the array.

Best regards,

0 Kudos
Message 6 of 8
(2,823 Views)

Take the LabVIEW tutorials.

LabVIEW Introduction Course - Three Hours
Learn LabVIEW

 

Learn about shift registers.

0 Kudos
Message 7 of 8
(2,810 Views)

Although I'd agree that some basic tutorials are likely to be helpful, I'll also suggest the following topic.

 

Since you have a specific flow that you want to go through (enter ship location, send ship locations, wait for opponent, return response, take your turn, wait for response, wait for opponent, ...) you should consider the use of a State Machine.

Ensuring that you use a typedef'd enum for your "State" control/constant will make it easier to modify if you want to change things in future.

 

An alternative structure that may be of interest to you is the Event Structure, which will respond only to specific events (without polling).

However, combining these two may be tricky in your situation, and you should avoid using more than one Event Structure on a block diagram until you have a little more time spent using LabVIEW (they can sometimes lock each other up if you have multiple, in not-so-trivial ways if in separate cases of a Case Structure, etc).


GCentral
Message 8 of 8
(2,778 Views)