I am looking for help to continuously read/write an array from the serial port. I have a script on an Arduino that continuously read/writes arrays from the serial port which will then execute particular commands depending on conditional statements.
Attached are what I have so far. It quasi works, but I am currently making two separate arrays it seems. I can read in arrays from the serial port just fine, writing arrays (editing 1 element) is where I am struggling. I can also write to single elements in an array (right side of front panel) but it won't show up on my read in (left side).
Example End Goal:
(1) Labview reads array continuously, elements being things such as (PWM_1, Temp_1, PWM_2, Temp_2) (need help)
(2) User can then write to the array to change elements such as PWM_1 (need help)
(3) This changed array will then be read by Arduino, executed, and sent back over serial to be updated by LabView (complete)
This sounds like a homework assignment. You should at least post what code you have and what specific problems or question you have. No one here is going to write the code for you.
Also the middle panel, "Command Terminal" is something I was using earlier and can be ignored. Originally, I was using this area to write an integer (instead of complete arrays) to the Arduino which would then execute the command.
The right most read is a byproduct of an old build where I was sending a single command (not an array) at a time. I should have deleted it, but I haven't. Shouldn't negatively affect anything.
The feedback node iterates the array? But I'm not 100% sure, I only have it in there because that's how the example I found did it...
What is the protocol that the Arduino is expecting? You should have some documentation indicating what the Arduino is expecting to receive as well as what it is writing. Generally this data will be terminated with a line feed or some other termination character. Your reads and writes should include that. As configured now your read will read 1000 bytes or up to a line feed character, whichever happens first.
Do you have an example of the expected data from the Arduino?
Given that your front panel lists specific commands, how is each command formatted? Also, I doubt you want to continually write commands to the Arduino. I would think you only want to write the commands at specific times such as when you want to turn the laser on or off. You will continuously read for the status if the Arduino is indeed continually sending data. Or does it only send data in response to a command?
I don't have a formal protocol in place yet, that's for later down the road. As of now, I serial.read() any integer or "-" sign and set that as a variable, X. I then go through conditionals that "turn on laser" (changes PWM to 100) if X == 71 for example.
After the conditions are complete, I then iterate through an array and serial.print the new values of the array. So if the Arduino receives the 71 command it changes an example array from (var1=1, var2=2, PWM=0) to (var1=1, var2=2, PWM=100). The print function sends it as 1,2,100 \n and then Labview breaks out the numbers using "," and restarts using \n.
Expected example data from Arduino: 3.3, 24.00, 255.00, 0.00 /n (repeats continuously)
How each command was formatted: "71" *click write button* (no longer there, used to be to the left of Serial Read)
I do want to continuously write (unless there's a better way). This is because I am also using LabVIEW to process photo-detector data of the laser so that I can shut it off if things go poorly.
Arduino is always sending data because it is reading temperature that needs to be seen on Labview.
Hope that answers your questions,
Example expected data from Arduino: 3.3, 24.00, 255, 0 \n (continuously sending)
The commands can be ignored, that was from my first build. I would type in the command in a, now deleted, write terminal then click write. The Arduino would then read in the command and iterate through conditionals to execute it.
I do want to continuously write (unless there's a better way) because I am also using LabVIEW to process photo-detector data and turn the laser off if particular limits are met.
Arduino continuously sends data because I want to monitor temperature.
Hope that answers your questions.
Do you actually want to write continually or only when certain conditions occur? What you basically need is a state machine to control your logic and your operations. One of the actions you would take every iteration would be to read data from the Arduino. Others would check your conditions and then take action (write a command to turn off the laser, for example) if a condition was met. Take a look at some of the example code for state machine or the producer/consumer pattern. This will help to control the logic of the application.