LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview serial data from arduino

Solved!
Go to solution

Hi, i have 5 push buttons for which i have to make a GUI to display their states. I have connected 2 Arduino Unos with each other. The sender arduino is connected to the push buttons via pin 2,3 ,4,5 and 6. The Receiver arduino is connected to PC. Both the arduinos are connected via the RS485 TTL module. Have to create a serial RS485 communication prototype. The button status is displayed on the serial monitor and as well as on labview (picture attached) but i need to create labview VI in which there are 5 leds on the front panel and the data received from the arduino should turn on the led in labview. The labview VI is attached as well. I have taken the continuous read and write VI and removed the other blocks.

 

Arduino code for sender is:


bool button_state2=0;
bool button_state3=0;
bool button_state4=0;
bool button_state5=0;
bool button_state6=0;
int t =0;

void setup() {
  Serial.begin(9600);
 
  pinMode(7, HIGH);
 
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
 
    digitalWrite(7,HIGH);
 
}

void loop() {
  if(digitalRead(2) == 0){
    // wait 300ms to prevent debouncing
      delay(300);
   
      button_state2 = !button_state2;
      Serial.print("Button 1 : ");
      Serial.print(button_state2);
      Serial.println();
      Serial.write(button_state2);
   }

     if(digitalRead(3) == 0){
    // wait 300ms to prevent debouncing
      delay(300);
     
      button_state3 = !button_state3;
      Serial.print("Button 2 : ");
      Serial.print(button_state3);
      Serial.println();
      Serial.write(button_state3);
   }

     if(digitalRead(4) == 0){
    // wait 300ms to prevent debouncing
      delay(300);
     
      button_state4 = !button_state4;
      Serial.print("Button 3 : ");
      Serial.print(button_state4);
      Serial.println();
      Serial.write(button_state4);
   }

     if(digitalRead(5) == 0){
    // wait 300ms to prevent debouncing
      delay(300);
     
      button_state5 = !button_state5;
      Serial.print("Button 4 : ");
     Serial.print(button_state5);
      Serial.println();
      Serial.write(button_state5);
   }

     if(digitalRead(6) == 0){
    // wait 300ms to prevent debouncing
      delay(300);
     
      button_state6 = !button_state6;
      Serial.print("Button 5 : ");
      Serial.print(button_state6);
      Serial.println();
      Serial.write(button_state6);
   }
}

 

Receiver arduino code is:

bool button1 = false, button2 = false, button3 = false, button4 = false, button5 = false, button0 = false;

void setup() {
  // arduino default baud rate for serial communication
  Serial.begin(9600);
  //DE,RE Controlling pin of RS-485
  pinMode(7, OUTPUT);

  //DE,RE = 0, RS485 Receive Enabled
  digitalWrite(7, LOW);
}

void loop() {
  // wait untill all the bytes receive from data buffer
  //if (Serial.available() > 0) {

  // take the integer value from buffer (0 or 1)
  int button_state = Serial.parseInt();
  {
    switch (button_state) {
      case 0:
        button0 = !button0;
        break;
      case 1:
        button1 = !button1;
        break;
      case 2:
        button2 = !button2;
        break;
      case 3:
        button3 = !button3;
        break;
      case 4:
        button4 = !button4;
        break;
      case 5:
        button5 = !button5;
        break;
    }
    //Serial.println(button_state);
  }

  printButtonStates();

  //}
  //printButtonStates();
  delay(3000);
}

void printButtonStates() {
  Serial.print("Button 1 : " + String(button1 ? "ON " : "OFF "));
  Serial.print("Button 2 : " + String(button2 ? "ON " : "OFF "));
  Serial.print("Button 3 : " + String(button3 ? "ON " : "OFF "));
  Serial.print("Button 4 : " + String(button4 ? "ON " : "OFF "));
  Serial.print("Button 5 : " + String(button5 ? "ON " : "OFF "));
  Serial.println();
}
 
0 Kudos
Message 1 of 5
(1,457 Views)
Solution
Accepted by topic author AbdullahWasif

1. Let's simplify things on the data going to the PC.  I would recommend changing it to this:

 

void printButtonStates() {
  Serial.print(String(button1 ? "1 " : "0 "));
  Serial.print(String(button2 ? "1 " : "0 "));
  Serial.print(String(button3 ? "1 " : "0 "));
  Serial.print(String(button4 ? "1 " : "0 "));
  Serial.println(String(button5 ? "1" : "0"));
}

 

This will output a simple space delimited string with each button's value corresponding to a single character.

 

2. You are using all the defaults to the serial port, so get rid of the cluster handling the serial settings.

3. You should be constantly getting data, so no need for the Read button or the wait in the loop.

4. With the format I recommend above, you can just use the Spreadsheet String To Array primitive to get an array of numeric values and the Not Equal To Zero to turn it into an array of Booleans.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Download All
Message 2 of 5
(1,432 Views)

First off I recommend you watch this video: VIWeek 2020/Proper way to communicate over serial

 

Second I would rethink your approach as you are going to have to parse all that text in LabVIEW to determine the button status.

 

Unless the serial data must be "human readable" I would just send a 1 or 0 for each button.

 

10010 would be sw1 closed, sw2 open, sw3 open, sw4 closed, sw5 open.

 

That would be simple to parse in LabVIEW.

 

EDIT: forget the commas, look how easy it is to parse this string...

 

Screenshot 2023-06-08 124128.png 

========================
=== Engineer Ambiguously ===
========================
Message 3 of 5
(1,429 Views)

Thanks so much crossrulz. Your approach is working like a charm. One more problem that i have is that i have to send 20 bytes of data from sender arduino to receiver arduino. And there are 3 push buttons, 1 three position toggle switch, 1 potentio knob and one 5 position switch. It's basically an aircraft radio control panel (let me attach a picture). the buttons and switches are mapped to specific bits of the 20 bytes sent. the mapping is is attached as well. What approach should i follow for this?

 

Download All
0 Kudos
Message 4 of 5
(1,324 Views)

The arduino code for the sender is:

#include <SoftwareSerial.h>

// define pin numbers
#define BTN_1_PIN   2
#define BTN_2_PIN   3
#define BTN_4_PIN   4
#define BTN_7_PIN   5
#define BTN_MAN_PIN 6

SoftwareSerial rs485 (10, 11); // RX, TX

void setup() {
  rs485.begin(9600);
  pinMode(7, HIGH);
 
  pinMode(BTN_1_PIN, INPUT_PULLUP);
  pinMode(BTN_2_PIN, INPUT_PULLUP);
  pinMode(BTN_4_PIN, INPUT_PULLUP);
  pinMode(BTN_7_PIN, INPUT_PULLUP);
  pinMode(BTN_MAN_PIN, INPUT_PULLUP);
 
  digitalWrite(7,HIGH);
 
}

void loop() {
  byte data[20] = {0};

  // read buttons and store in data
  if (digitalRead(BTN_1_PIN)) {
    bitWrite(data[4], 0, 1);
  }
  if (digitalRead(BTN_2_PIN)) {
    bitWrite(data[6], 0, 1);
  }
  if (digitalRead(BTN_4_PIN)) {
    bitWrite(data[4], 1, 1);
  }
  if (digitalRead(BTN_7_PIN)) {
    bitWrite(data[4], 2, 1);
  }
  if (digitalRead(BTN_MAN_PIN)) {
    bitWrite(data[4], 3, 1);
  }

  // send data
  rs485.write(data, 20);
}


For the Receiver:
#include <SoftwareSerial.h>

SoftwareSerial rs485 (10, 11); // RX, TX

void setup() {
  rs485.begin(9600);
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
}

void loop() {
  if (rs485.available() >= 20) {
    byte data[20] = {0};
   
    rs485.readBytes(data, 20);

    // check the status of buttons
    if (bitRead(data[4], 0)) {
      Serial.println("Button 1 is pressed");
    }
    if (bitRead(data[4], 1)) {
      Serial.println("Button 4 is pressed");
    }
    if (bitRead(data[4], 2)) {
      Serial.println("Button 7 is pressed");
    }
    if (bitRead(data[4], 3)) {
      Serial.println("Button MAN is pressed");
    }
    if (bitRead(data[6], 0)) {
      Serial.println("Button 2 is pressed");
    }
  }
}
 
With rs485.print(data[20]), the serial monitor always shows Button 1 is pressed.
With Serial.print(data[20]) random buttons are shown as pressed.
0 Kudos
Message 5 of 5
(1,298 Views)