LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview serial data from arduino

Hi, I have connected 2 Arduino Unos with each other. The sender Arduino is connected to push buttons and the receiver Arduino is connected to PC. Both the arduinos are connected via the RS485 TTL module. I have to create a Labview VI for data acquisition from the push buttons i.e the states of the buttons should be displayed on Labview front panel via the RS485 serial communication protocol. I am able to send 20 bytes of data (160 bits) from the sender to the receiver and the same is displayed on Labview. The push buttons are mapped to specific bits of the 20 bytes, the mapping is as follows:

 

1st bit of 5th byte

Numeric push button “1”

2nd bit of 5th byte

Numeric push button “4”

3rd  bit of 5th byte

Numeric push button “7”

4th bit of 5th byte

Push button “MAN”

1st bit of 7th byte

Numeric push button “2”

2nd bit of 7th byte

Numeric push button “5”

3rd bit of 7th byte

Numeric push button “8”

4th bit of 7th byte

Numeric push button “0”

1st bit of 9th byte

Numeric push button “3”

2nd bit of 9th byte

Numeric push button “6”

3rd bit of 9th byte

Numeric push button “9”

4th bit of 9th byte

Push button “ENT”

1st bit of 11th byte

SQL Switch -OFF

2nd bit of 11th byte

SQL Switch -ACKN

1st bit of 14th byte

Mode Selection Switch -SET

3rd bit of 14th byte

Mode Selection Switch -ADF

2nd bit of 15th byte

Mode Selection Switch -ZRO

3rd bit of 15th byte

Mode Selection Switch –OFF

4th bit of 15th byte

Mode Selection Switch –TR+G

 

In the response window of my VI, the specific bits are changing with the press of the buttons but i need to display LED's on the front panel corrresponding to the buttons. I have converted the incoming string to array but when i attach an LED indicator to the 33, 34th bit then the LED doesn't turn on/off with the button push. Any help would be appreciated.

 

The arduino code for sender is:

// Initialize the button pins
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 10;
const int buttonPin7 = 11;
const int buttonPin8 = 12;
const int buttonPin9 = 13;

// Size of the boolean string
const int stringSize = 160;

// Define initial button states
bool button_state1 = 0;
bool button_state2 = 0;
bool button_state3 = 0;
bool button_state4 = 0;
bool button_state5 = 0;
bool button_state6 = 0;
bool button_state7 = 0;
bool button_state8 = 0;
bool button_state9 = 0;

// Boolean array to store values
bool booleanArray[stringSize];

// Flag to indicate when to print the string
bool printString = false;      

// Define the DS-RE pin
#define DE_RE 7

SoftwareSerial rs485(8, 9); // RX, TX

void setup()
{
  // Initialize serial communication
   Serial.begin(9600);
   rs485.begin(9600);
   pinMode(DE_RE, OUTPUT);

 
  // Configure the button pins as inputs
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  pinMode(buttonPin6, INPUT_PULLUP);
  pinMode(buttonPin7, INPUT_PULLUP);
  pinMode(buttonPin8, INPUT_PULLUP);
  pinMode(buttonPin9, INPUT_PULLUP);
}

void loop()
{
  bool anyPressed = false;
  // Check if any of the buttons is pressed
         if (  digitalRead(buttonPin1) == LOW )
         {button_state1 = !button_state1;}
          if (  digitalRead(buttonPin2) == LOW )
         {button_state2 = !button_state2;}
         if (  digitalRead(buttonPin3) == LOW )
         {button_state3 = !button_state3;}
         if (  digitalRead(buttonPin4) == LOW )
         {button_state4 = !button_state4;}
         if (  digitalRead(buttonPin5) == LOW )
         {button_state5 = !button_state5;}
          if (  digitalRead(buttonPin6) == LOW )
         {button_state6 = !button_state6;}
          if (  digitalRead(buttonPin7) == LOW )
         {button_state7 = !button_state7;}
          if (  digitalRead(buttonPin8) == LOW )
         {button_state8 = !button_state8;}
          if (  digitalRead(buttonPin9) == LOW )
         {button_state9 = !button_state9;}
       
  {
    // Update the boolean array with the state of the buttons
    updateBooleanArray();

    // Set the flag to true to print the string
    printString = true;
  }

  if (printString)
  {
    // Convert the boolean array to a string
    String booleanString = booleanArrayToString();
 
    // Reset the flag
    digitalWrite(DE_RE, HIGH);
    rs485.println(booleanString);
    digitalWrite(DE_RE, LOW);
    Serial.println(booleanString);
    printString = false;
  }
 
}

void updateBooleanArray()
{
  // Update the boolean array with the state of the buttons
  booleanArray[32] = (button_state1);
  booleanArray[48] = (button_state2);
  booleanArray[72] = (button_state3);
  booleanArray[33] = (button_state4);
  booleanArray[49] = (button_state5);
  booleanArray[73] = (button_state6);
  booleanArray[34] = (button_state7);
  booleanArray[50] = (button_state8);
  booleanArray[74] = (button_state9);
}

String booleanArrayToString()
{
  String booleanString;

  // Convert boolean values to characters ('0' or '1') and append them to the string
  for (int i = 0; i < stringSize; i++)
  {
    char valueChar = (booleanArray[i]) ? '1' : '0';
    booleanString += valueChar;
  }

  return booleanString;
}
 

The arduino code for receiver is:
#include <SoftwareSerial.h>

#define DE_RE 7

SoftwareSerial rs485(8, 9); // RX, TX

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

void loop() {
  if (rs485.available()) {
    String buttonStates = rs485.readStringUntil('\n');
    Serial.println(buttonStates);
  }
}
Download All
0 Kudos
Message 1 of 7
(1,089 Views)

Sigh.  I got a reputation in the Forums a few years ago for "whining" (yes, I admit it) about Forum posts that had pieces of LabVIEW block Diagrams posted, but no VIs.  I remember saying "Suppose I asked you for help with a C++ program and sent you a picture of the 1000 lines of code -- would you say "Thank you", or yell at me and say "Why didn't you send the program, itself" (I don't use C or C++, so I can't remember if its a .c file or what).

 

Well, you've sent effectively a "picture" of Arduino code, double-spaced, that takes about 150 lines.  Why not send the files themselves, which we can print or "condense" (by getting rid of 75 blank lines)?

 

I looked at your LabVIEW code.  Have you read any of the numerous posts on the Forum (or posts on the Web) from @crossrulz on the proper way to set up LabVIEW to do communication using VISA?  In response to a command to "Send me some data", your device generally sends a string of characters that you read "all at once" (by reading, say, 500 characters, more than you expect because you have (correctly) configured VISA to use a Termination character), and then parse the String (I suggest using the Scan from String function to locate the "numeric data of interest" and read it as a decimal, a float, a Boolean, whatever it is, and ignore the rest.  Your VISA string probably has only 4-8 "real" data elements, probably most of the decimals, so your Scan from String will probably produce 4 I32s. rather than the monstrous Index Array function you used.

 

Bob Schor

0 Kudos
Message 2 of 7
(1,043 Views)

@AbdullahWasif wrote:

 

I have converted the incoming string to array but when i attach an LED indicator to the 33, 34th bit then the LED doesn't turn on/off with the button push. Any help would be appreciated.

 

Have you confirmed that there is actually a "1" in these two positions?

 

Can you think of a way to code this that makes it easier to identify what is going on? I guess that solving the problem will be easier when you remove the giant index array block.

0 Kudos
Message 3 of 7
(1,026 Views)

You have two Posts that have the same poorly-designed sub-VI trying to do the same thing -- get a (text, I presume) response from an Arduino (though you fail to show the commands that, when doing VISA communication, you usually send to a Serial port to get it to send you strings of information that you subsequently parse and interpret).

 

Why don't you provide us with more information on your Project?  Show us the String you expect to get from the Arduino, including what you need to do to get this string (it may be that you "do nothing", it just spits a string out once every N seconds, but do tell us that!).  Parsing a String to get out what LEDs it says to turn on should be fairly easy, especially if you learn how to use "Scan from String" -- it would be a single Function following the VISA Read, and would give you (as its output) however many Booleans to tell it to read.  No need for Index Array.

 

But you need to tell us what you are trying to do, and provide examples of your expected data strings.

 

Bob Schor

0 Kudos
Message 4 of 7
(1,022 Views)

Yes, it does change to a "1" when i press the button. I've attached a screenshot. As you can see in the picture, there are "1" on 33, 34, 36 and 48th bit when i press a button but state of the LED doesn't change

Download All
0 Kudos
Message 5 of 7
(999 Views)

Sorry about the arduino code. I don't know what i was thinking. I've attached the arduino codes here (as a zip file) cause .ino format is not supported. I've tried the Scan from string function and set it's scan sequence to scan binary integer. Can't seem to make it work.

Download All
0 Kudos
Message 6 of 7
(991 Views)

@AbdullahWasif wrote:

As you can see in the picture ...


I'm sorry, but I can't see it in the picture. I could go out of my way to manually, painstakingly count the zeroes in your screenshot, three times at least to make sure I did not miscount. I am, however, very bad at these kind of tasks and don't like doing them. Can you help me seeing it at a glance, without making me count?

 

Try adding an indicator to the string-to-array function. I think your string-to-array function does something different to what you think it is doing.

 

LLindenbauer_0-1686594530576.png

 

0 Kudos
Message 7 of 7
(981 Views)