11-25-2013 07:50 AM
Hello,
I am writing a VI to control a microcontroller's PWM via a serial port. I wrote a simple code for the microcontroller that allows me to set the duty cycle ratio on a scale of 0-65535. This works fine with Compuphase's Termite (an RS-232 terminal); The microcontroller prompts me for an input, waits until I give one, updates, and waits for another input if and when I decide to send it. Now, I am trying to get the same functionality out of a LabVIEW vi but hit a snag.
When I tried sending my commands with a Basic Serial Read and Write example, everything worked fine; I was able to see the input prompt, write an input, and ,if I was quick enough, see the update message. Even when I was not quick enough, I was able to verify the update by monitoring the PWM pin with a logic analyzer. The problem with the basic example, however, is that I need to update the duty cycle ratio at will so I moved on to the Continuous Write an Read example. The problem I encountered; is that I have to toggle between Read and Write to get it to work. When I do this, either I don't hold the Write down long enough and nothing happens, or I get multiple updates when I switch back to the read. Some of these updates don't even match and produce faulty duty updates for example updated to; 16000, 16000, 16000, 16000, 6000, 600, 60, 16000, 1600, 60, 0, 0, 0, 16000, 60, 0, 0 etc.
I tried enabling the termination characters in the hope that the Write would end on termination and go to the Read where it would recieve a termination character and wait for me to enable the Write again but to no avail. I still have to toggle between the two and either get no response on the read of multiple updates to varying accuracy. I tried removing the Write/Read enables and their case structures in the hopes that the flow of the loop combined with termination characters would do the trick but then the updates don't get registered at all. The funniest thing is that I made a similar VI before with an Arduino and didn't even need to toggle between Write and Read (I'm now using a FRDM board from Freescale).
I've also been frequency getting timeout errors; Error -1073807339 at VISA Read, Possible Reason: VISA Hex 0xBFFF0015. I removed the option to stop the while loop if there is an error so I can run but the error still pops up on stop occasionally. Could the timing issue still be causing me problems?
Would really appreciate any and all advice, I am providing my slightly modified example VIs and can provide the microcontroller code on request although it's very simple.
Thanks is in advance,
Yusif Nurizade
Solved! Go to Solution.
11-25-2013 08:23 AM
That is, quite frankly, a poor example that you have based your code on. You also have the order of the write and read backwards in your modification if the instrument requires a command to return something. It should really be using an event structure. Put the event structure around a write and read and use a value change event. I've attached a quick modification. This mod always does a read after the write. If necessary, you could have separate events for the write and read.
11-25-2013 08:40 AM
Hi AKello,
Maybe look at:
http://digital.ni.com/public.nsf/allkb/F3E0621CB71AA16786256F970000FC57
Which has a possible solution to the error coming back.
Kind regards,
Rick Wagner
11-26-2013 08:48 AM
Rick,
Thank you for your suggestion but I was having a different issue. It occured at VISA Read and I would start the VI a few seconds before the microcontroller. Good article thoug, will keep it in mind for later.
Best,
Yusif Nurizade
11-26-2013 08:56 AM
Dennis,
Thank you so much for the suggestion. With a little modification of your VI, I got mine exactly where I wanted it to be.
I did get a curious issue during modification; in my D mod, everything worked right except there was a delay in the read value by one cycle. I'm wondering if the feedback node was the issue; even more curious it worked fine when I executed by steps. I stripped away the node in the E mod and got updates in the same cycle. Would be interested to hear what you think.
Thanks again,
Yusif
11-26-2013 09:12 AM
Your wiring of the return count of a VISA Write to the byte count of the VISA Read is just wrong. There is no relationship between the number of bytes that are written and the number of bytes that the device will return. With a term character enabled, you should just set the number of bytes to read to some high number. The read will automatically terminate when the term character is detected. Flusing the serial buffer after you configure the port would also be a good idea.
12-14-2013 02:18 PM
Dennis,
Forgive the late reply, I was on work travel and did not have regular internet access.
I followed your suggestion and everything worked great. I think I have a better understanding of reading and writing now but could you please elaborate on flushing the buffer?
Thanks again,
Yusif