element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
NI LabVIEW Community
  • Products
  • Dev Tools
  • NI LabVIEW Community
  • More
  • Cancel
NI LabVIEW Community
LabVIEW Challenge Blogs Learning LabVIEW 9: Additional Material - Interacting with Arduino Uno using Hardware Abstraction Framework
  • Blog
  • LabVIEW Challenge Blogs
  • Forum
  • Documents
  • Quiz
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join NI LabVIEW Community to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Andrew J
  • Date Created: 2 Nov 2023 4:21 PM Date Created
  • Views 839 views
  • Likes 7 likes
  • Comments 5 comments
  • test automation
  • labview 2023
  • ni
  • labview
Related
Recommended

Learning LabVIEW 9: Additional Material - Interacting with Arduino Uno using Hardware Abstraction Framework

Andrew J
Andrew J
2 Nov 2023
Learning LabVIEW 9: Additional Material - Interacting with Arduino Uno using Hardware Abstraction Framework

Introduction

I felt I was missing an example with the Hardware Abstraction Framework write up because I didn't cover off a Serial device.  The existing examples all use a USB connection, but it is just as easy to use Serial, given the right application running on the attached instrument.  Here then is a further example interacting with an Arduino Uno; this post describes an interaction with LINX, but it's just as easy to use the VISA Write that the HAF is based on.

Note: this is not part of my final blog which remains the 7th in the series.  

Table of Contents

  • Introduction
  • Getting Started
    • Arduino Application
    • Create the Instrument
  • Running

Getting Started

Plug in the Arduino Uno and note the COM port.  Launch NI-MAX, identify the Arduino (from the COM port - on my PC, the COM port was actually named Arduino so was easy to spot) and give it an alias, I chose ArduinoUnoR3.

Arduino Application

This simple sketch should be loaded onto the Arduino via the Arduino IDE:

#define RST "*RST"
#define CLS "*CLS"
#define OPC "*OPC"
#define ESR "*ESR?"
#define IDN "*IDN?"

#define IDENTITY "Arduino,Uno R3,0123456789,1.1.0\n"
#define COMPLETE "1\n"
String cmd;
String out;
char terminator = '\n';

void setup() {
  // Start a serial channel at the correct Baud rate defined in the Hardware Abstraction Framework for Arduino Serial device
  Serial.begin(115200);
}

void loop() {
  // loop looking for commands.  This example only deals with a small subset of commands, as used by the framework, plus *IDN?
  // Most are ignored; *ESR? is a request to ask the device if all previous commands have finished - if so, return 1.  The framework
  // sends this command if the actual command must "wait for completion" so here, we return 1.

  if (Serial.available() > 0) {
    cmd = Serial.readStringUntil(terminator);
    if (cmd == RST) {
    }
    if (cmd == CLS) {
    }
    if (cmd == OPC) {
    }
    if (cmd == ESR) {
      Serial.print(COMPLETE);
    }
    if (cmd == IDN) {
      Serial.print(IDENTITY);
    }
  }
}

To give a quick run through:

  • The commands it recognises are defined at the start - these are core SCPI commands that I want to read although in this code most of them do nothing.
  • I've defined an identity string to return if asked
  • A serial port is opened at a fast speed 115200 baud.  This should match the speed defined for the instrument in the HAF.
  • The code in loop() then waits on the serial port, reads data from it and if it matches a command processes it.  The current code only responds to the commands *ESR and *IDN? but you could see how extensible it could be.  A more robust firmware for handling commands could be written of course.

That's it, nothing complicated on the Arduino side.  I chose these simple SCPI commands but there's no reason why it couldn't respond to custom commands if you define them in HAF as well.

Create the Instrument

In the Framework, a Driver (Instrument) is required to represent the Arduino.  Actually, the Framework already has this so really nothing must be done for this but if you want to follow this example with a different Serial device then this step is necessary.  The Arduino Driver can be used for a pattern:

Image showing the methods required for an Arduino Driver

Note that only three methods are required.  Read Serial Port Attributes is an override of the parent method and I use it to up the baud rate from the default 9600 to 115200 and to give a wait time of 2000ms to allow the Arduino to reset (from a default of 0ms.)  This method, then, only needs overriding if you need to change the Serial attributes.  Note that the Driver does NOT need to specify the COM port as the Framework picks that up automatically.

Running

To interact with the Arduino then, I just ran the Instrument Identification example - I didn't change anything in the Framework at all.  This example just queries the selected device and asks for its identity and has been seen in the main blog with the PI Pico.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

There's no commentary on this.  I ran it, selected the Arduino in the drop down box (actually, it was pre-selected by the Framework as the first in the list of devices) and clicked Get ID.  You'll notice a slight pause: when a serial channel is opened to an Arduino it resets so it's necessary to wait a short period of time for that to happen.

How Does it Work?

I've gone through this in some detail in post number 7 in the series (link above) and there's nothing more to add really.  The HAF is sending commands to the device, so there must be a firmware implementation running on it that can respond to those commands.  There is a need for a Driver, subclassed from SerialDriver, that represents the attached device and this necessitates two method overrides to be created, "Get Instrument Alias()" and "Build Driver Settings Mappings()" but that is all.

Summary

If I had to choose between using the HAF or using LINX, which would be better?  I think they serve different purposes, and I'd just add that I did not set out to create a LINX rival:

  • HAF expects a Firmware to be in place, it doesn't provide one, but it can handle custom commands that are identified to it.  LINX can download a basic, working firmware to the device; that firmware can be extended with custom commands and used if the custom commands are identified to it.  
  • LINX is just another tool in the LabVIEW set, so creating a wider ranging application involving multiple instruments requires work to be done on the LabVIEW side.  HAF already has that foundation requiring a more simple setup and run application to be created.

LINX is not integrated into the HAF so it is definitely a case of choose one or the other.  Both require a level of work in terms of creating a non-basic firmware that runs on the device, although the HAF has no foundation for this unlike LINX.  Both have the communications mechanisms built-in.  The HAF does allow multiple instruments to be co-ordinated in an Interacting Application with a mix-and-match of USB and Serial as needed; this is additional work to pull LINX into an application which does the same thing.

  • Sign in to reply
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to Andrew J

    I just reread the whole series, and saw that you are the one that submitted the memory leak defect Slight smile.
    Didn't remember that anymore. It's been almost 6 years Open mouth

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Andrew J
    Andrew J over 1 year ago in reply to Jan Cumps

    Something I found too, which is why I eventually off-loaded onto the ESP32.  Although that still meant a somewhat simpler and lighter custom interchange between the two.  I think the Arduino would be better served by custom commands unless the additional logic is really lightweight.  In any case, once built it can be re-used in other projects, although unlikely to be of much use to anyone else.  If you look at the command set for LINX it is very custom and very lightweight: https://www.labviewmakerhub.com/doku.php?id=learn:libraries:linx:spec:start

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to Andrew J

    The Jan Breuer lib that I normally use, can be configured to fit on a UNO. But it's a tight fit with not much room for the instrument logic.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Andrew J
    Andrew J over 1 year ago in reply to Jan Cumps

    Yes, that may work: it still has an unfixed memory leak and I've noticed that others have posted issues as well.  Doesn't appear to be maintained, but I did use it briefly when I was trying out an ESP32-side web server SCPI handler.  That was part of a 4DUINO which had an Arduino and ESP32 on one PCB that could talk to each other.  I used the ESP32 to handle SCPI commands and pass them over to the Arduino for processing, which passed the result back to the ESP32 and then back to the web page.

    I should think there are better handlers out there.  I tried to work out the code for this parser but I'm no natural C programmer and the way these programs are often written I struggle.  I never understand why the source can't be clearer but hey, ho.  I guess that comes from having a SmallTalk and Java background rather than C and C++.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago

    I made a Arduino SCPI project a while ago, for project14:  Arduino in Test Instrumentation - Intro: SCPI Programmable Switch 

    It uses a lighter Arduino SCPI parser - not the same as in the Pico (code).
    For a reason I can't remember, I chose 38400 baud. Should plug into your framework...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube