LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Arduino hex communication over serial

Hi all,

 

I am working on some arduino/labview and hit a brick wall.

 

Currently the plan is to send some hex code from an arduino to labview using serial.

That works however I cant read it correctly with labview.

 

Any Ideas / suggestions would be much appriciated

 

🙂

0 Kudos
Message 1 of 18
(2,726 Views)

@letmetry wrote:

That works however I cant read it correctly with labview.


A bit better description of your problem would be useful. From the image you posted i can tell that you are trying to read and write at the same time on the same COM port, that will not work (at least not reliably). You are also closing the same reference twice, which probably results in an error when you stop your VI.

 

To read hex value, you can also wire a string indicator to VISA Read output and set it to show hex values instead of text.

0 Kudos
Message 2 of 18
(2,720 Views)

Better description:

I am sending a command to the arduino using labview "a" or "b" that part is easy.

Depending on what the arduino receives it will turn on a bank of relays, read the status of all the relays then send back a hex representation of the 64 possible states eg relay1on relay2off etc...

 

Here's an example of the expected hex return string:

"3F"

In this example, the hex value "3F" represents the status of six relays. Each bit in the byte corresponds to the status of a relay, with "1" indicating ON and "0" indicating OFF.

To interpret the "3F" hex value:

  • The binary representation of "3F" is "00111111".
  • Each bit in the binary representation corresponds to a relay: the first bit to relay 1, the second bit to relay 2, and so on.
  • In this case, since all six bits are set to "1," it means that all relays are ON.

So, the expected interpretation of the hex return "3F" is that all six relays are ON.

 

Now the problem is I am not good at LabView Yet.

Honestly I have tried so many variations of this im getting lost 

 

 

0 Kudos
Message 3 of 18
(2,706 Views)

Please refer to the shipping example <LabVIEW>\examples\Instrument IO\Serial\Continuous Serial Write and Read.vi

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 4 of 18
(2,704 Views)

Thanks for the reply much appreciated,

 

The continuous read write example does not read the hex correctly it simply reads 3F no matter what 😞 

Also the more it is used the slower the arduino responds 

 

0 Kudos
Message 5 of 18
(2,689 Views)

Might I recommend a video on serial communications in LabVIEW.  The second half specifically handles the binary/hex/raw data.

VIWeek 2020/Proper way to communicate over serial


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
0 Kudos
Message 6 of 18
(2,659 Views)

First watch this video: VIWeek 2020/Proper way to communicate over serial

 

Pay attention to the first part that covers ASCII data with a Termination Character. 

 

But in general the Arduino is going to send the string "3F" 

 

So you need to convert that String that to a Hex Integer and then to a Boolean Array 

 

Screenshot 2023-05-18 072340.png

 

Use the Serial.PrintLn command to send your data from the Arduino as it adds a Termination Character for you. 

 

Serial.println()
Description
Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 7 of 18
(2,653 Views)

What exactly are you sending from the Arduino? Can you paste some code here. Or at least the Serial.print() part? What are you reading with the Serial Monitor of the Arduino IDE? Is it correct there?

0 Kudos
Message 8 of 18
(2,650 Views)

@Knight Of NI

 

Hi,

 

yes I have watched this but I cant find a way to apply it to my problem

 

Thanks

 

0 Kudos
Message 9 of 18
(2,619 Views)
Arduino UNO code :
 
// Define the pin connected to the relay and the number of external relays
const int relayPin = 4;
const int numRelays = 6;

// Define an array of pins connected to the external relays
const int relayPins[numRelays] = {A0, A1, A2, A3, A4, A5};

void setup() {
  Serial.begin(9600);

  // Set the relay pin as an output
  pinMode(relayPin, OUTPUT);

  // Set the external relay pins as inputs with pull-up resistors enabled
  for (int i = 0; i < numRelays; i++) {
    pinMode(relayPins[i], INPUT_PULLUP);
  }
}

void loop() {
  // Check if data is available on the serial port
  if (Serial.available()) {
    // Read the input character
    char c = Serial.read();

    // Check if the character is 'a'
    if (c == 'a') {
      // Turn on the relay connected to pin 4
      digitalWrite(relayPin, HIGH);
      delay(500); // Wait for relays to stabilize

      // Initialize variable to store the relay status
      unsigned long status = 0;

      // Check the status of each external relay
      for (int i = 0; i < numRelays; i++) {
        if (digitalRead(relayPins[i]) == LOW) {
          status |= (1 << i);
        }
      }

      // Print the status of relays in hexadecimal format
      Serial.println(status, HEX);
    }
    // Check if the character is 'b'
    else if (c == 'b') {
      // Turn off the relay connected to pin 4
      digitalWrite(relayPin, LOW);
      delay(500); // Wait for relays to stabilize

      // Initialize variable to store the relay status
      unsigned long status = 0;

      // Check the status of each external relay
      for (int i = 0; i < numRelays; i++) {
        if (digitalRead(relayPins[i]) == LOW) {
          status |= (1 << i);
        }
      }

      // Print the status of relays in hexadecimal format
      Serial.println(status, HEX);
    }
  }
}
 
Basically I send the letter a or b then the arduino responds with 64 possible outcomes that are convoluted into hex format for communication purposes.
 
I had an example in labview that worked but it was very slow and looked like this.
 
0 Kudos
Message 10 of 18
(2,602 Views)