07-17-2014 06:31 AM
Hi all,
I want to develop a vi which does the following operation :
- The RS232 port of PC is connected to a PIC microcontroller hardware.
- The VI has to send a byte from PC to the PIC and then it has to wait for an echo of the same byte which is sent back to PC by the PIC controller to ensure that the byte is sent properly.
- If the echo is matched with the byte sent, it has to send the next byte and again wait for its echo and repeat the process for other bytes to be sent or else if the echo doesnt match/ no echo received, it has to give an error message/wait for a predefined time to make sure a timeout has occured respectively.
- Once all the bytes are sent from VI, and all corresponding echoes are received from the PIC, the vi has to wait for the output series of bytes from the PIC which can come immediately after the last echo was sent or after some time.
This should work for all baud rates over RS232 serial port.
Can anyone here guide me in implementing this as I am new to labview?..All suggestions are welcome.
07-17-2014 06:49 AM
Look in the examples, under HARDWARE INPUT and OUTPUT - SERIAL for examples of Serial port access (start with SIMPLE SERIAL and go from there).
It's an unusual situation where you have to wait for an acknowledgement of every. single. byte... Are you sure you can't do that a better way?
I would recommend a state-machine architecture, if you don't want to tie of the whole computer waiting for this handshake process.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
07-17-2014 08:58 AM
07-17-2014 12:47 PM
Hi mnx,
Please try following
1. create standard state machine
2. Have 1 start button to start sending bytes
3. have case structure with cases send,read,error
4. Now when user clicks start button go to send case and send 1st byte (assign that byte to some variable to use later), after sending go to read, once read is complete, check received byte is same as sent byte if not go to error and indicate error of mismatch.
5. send all bytes one by one. when you send last byte read response related to it but this time wait in reading for some extra time, check whether it is 1 byte or more bytes? if it is 1 byte that means you have received only response and not the other part.
The tricky part is while sending initial bytes you have to read for small time but while reading response of last byte you have to wait for extra time. I had done same things in past, I am sure you will fet it right.
07-17-2014 12:51 PM
And one other suggestion, which sadly adds to the complexity, if the PIC is actually decoding and then sending back the "echo", think of reversing the byte sent. The reason for this is that if xmit and receive wires are shorted anything sent by the vi will be looped back. Reversing the byte means you can be sure it is the PIC sending it back.
Post your code, we don't have much to work with when you say "the vis lack something" as the possibilities, while not actually infinite, are asymptotically close to infinite! 😉
07-17-2014 11:31 PM
Please find the attached application vi which I developed. Are there any betterment which I can do for this?
07-18-2014 01:01 AM - edited 07-18-2014 01:01 AM
Hi mnx,
- use wires instead of all those local variables and "value" property nodes. THINK DATAFLOW!
- use FOR loops for loops with known iteration count.
- remove all those RubeGoldbergs (like x := TRUE OR x)
- don't use BytesAtPort when all you need is a proper TimeOut setting
- why do you reinit Tx to default when you set a new value one millisecond later anyway?
- use a proper statemachine instead of that big flat sequence!
07-18-2014 01:13 AM
Hi GerdW,
What function can I use to receive bytes efficiently instead of "bytes at port"?
07-18-2014 01:40 AM
Hi mnx,
when you send your single byte commands and want to receive a single byte answer you need to read exactly one byte. No need for BytesAtPort here…
When you want to read the devices measurement data later on it seems you don't know the exact number of bytes, but you provide a timeout. So you need to read bytes in a loop until your time out is reached: BytesAtPort isn't needed for that!
07-18-2014 01:45 AM
Hello GerdW,
can you please post a demo vi for this? thanks