element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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
Test & Tools
  • Technologies
  • More
Test & Tools
Blog SCPI on a Linux Board - Part 4: TCP/IP SCPI and Instrument Service
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Test & Tools requires membership for participation - click to join
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 21 Jul 2018 7:46 PM Date Created
  • Views 3543 views
  • Likes 6 likes
  • Comments 3 comments
  • piface digital
  • pi
  • raspberry pi
  • diytestinstruch
Related
Recommended

SCPI on a Linux Board - Part 4: TCP/IP SCPI and Instrument Service

Jan Cumps
Jan Cumps
21 Jul 2018

I'm building a SCPI electronics lab instrument for Linux.

This post connects the C SCPI parser service together with the C++ Instrument service.

image

I've split up the SCPI interpreter and the instrument control for my own learning. I'm trying to use daemons on a linux SOB that talk together.

I could have used named pipes, messages or file structures to let them interact but I've used sockets. Critique in the comments please.

 

What Came Before and Linking Things Together

 

In the previous posts I've shown the SCPI parser (a C program) and the PiFace controller (object oriented). The two worked on their own but not together.

Both programs are TCP/IP servers. They listen on a port and react on traffic arriving.

The SCPI server reacts on SCPI commands it receives over the network.

The Instrument server reacts on GPIO commands it receives.

 

I've added a TCP/IP socket client to the SCPI server. Whenever it receives SCPI commands that have to be executed by the PiFace Digital, it translates them and sends instructions to the Instrument server's port.

It waits for the instrument's feedback and returns SCPI replies based on that.

 

image

 

Currently it knows how to control the outputs of the PiFace Digital.

The SCPI parser listens for this SCPI command:

DIGItal:OUTPut# TRUE|FALSE

If it retrieves such a command, it sends the Instrument service the instruction to set the requested output pin high or low.

If you send DIGI:OUTP0 1, it will send write01 to the Instrument port.

This part is written in C:

 

    {.pattern = "DIGItal:OUTPut#", .callback = SCPI_DigitalOutput,},

 

static scpi_result_t SCPI_DigitalOutput(scpi_t * context) {


  scpi_bool_t param1;
  int32_t numbers[1];

  // retrieve the output. Can be 0 - 7
  SCPI_CommandNumbers(context, numbers, 1, 0);
  if (! ((numbers[0] > -1) && (numbers[0] < 8) )) {
    SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX);
    return SCPI_RES_ERR;
  }

  /* read first parameter if present */
  if (!SCPI_ParamBool(context, &param1, TRUE)) {
    return SCPI_RES_ERR;
  }

  memset(instrument_payload, '.', sizeof(instrument_payload));
  instrument_payload[0] = 'w';
  instrument_payload[1] = 'r';
  instrument_payload[2] = 'i';
  instrument_payload[3] = 't';
  instrument_payload[4] = 'e';
  (instrument_payload[5] = '0' + numbers[0]); // only works if the 0 - 7 characters in the character set are consecutive. todo: Sue me.
  instrument_payload[6] = param1? '1' : '0';


  sendToInstrument(instrument_payload, sizeof(instrument_payload));


  return SCPI_RES_OK;
}

 

 

int sendToInstrument(char *buffer, size_t size) {
  //Send instruction to instrument


  buffer[size-1] = '\n';
  if( send(cs_socket_desc , buffer , size , 0) < 0)
//  if( send(cs_socket_desc , "hello, world!\n" , 14 , 0) < 0)
  {
      puts("Send to instrument service failed");
      return 1;
  }

  //Receive a reply from the server
  if( recv(cs_socket_desc , buffer , size , 0) < 0)
  {
      puts("Receive from instrument service failed");
      return 1;
  }

  return 0;
}

 

The Instrument server will know (because I've programmed it) that when it retrieves a write command, that the next two characters are the pin and value.

The Instrument server is written in C++:

 

      while (stream >> x) {
        // process the commands
        if (x.compare(0, 4, "read") == 0) {
          // todo implement me
        } else if (x.compare(0, 5, "write") == 0){
          pfd_write(std::stoi(x.substr(6, 1)), std::stoi(x.substr(5, 1)), PifaceDigital::OUTPUT, pf); // todo: parse argument
        }

(the alert reader will see that the code doesn't know yet what to do when receiving a read command. That's on the todo list.)

 

The actual write command is forwarded to the PiFace class that we've designed a few blogs ago.

 

void pfd_write(uint8_t value, int bit_num, uint8_t reg, PifaceDigital *pf) {
  pf->open();
  if (bit_num >= 0) {
    pf->writeBit(value, bit_num, reg);
  } else {
    pf->writeReg(value, reg);
  }
  pf->close();
}

 

related blog
SCPI on a Linux Board - Letter of Intent

SCPI on a Linux Board - Part 1: Proof of Concept

SCPI on a Linux Board - Part 2a: PiFace Digital C programming

SCPI on a Linux Board - Part 2b: PiFace Digital C++ programming
SCPI on a Linux Board - Part 3: TCP/IP Socket C++ programming
SCPI on a Linux Board - Part 4: TCP/IP SCPI and Instrument Service
SCPI on a Linux Board - Part 4b: TCP/IP SCPI and Instrument Service 100% Working
SCPI on a Linux Board - Part 4c: TCP/IP SCPI and Instrument Service - Run as a Daemon
SCPI on a Linux Board - Part 5a: LabVIEW Driver for LAB Switch: Open, Close and Switch functions
  • Sign in to reply

Top Comments

  • 14rhb
    14rhb over 6 years ago +2
    Jan, How very timely - a friend mentioned SCPI to me just this week, so I'm looking forward to reading through this series you've been posting in depth to try and learn some details. Rod
  • Jan Cumps
    Jan Cumps over 6 years ago in reply to 14rhb +2
    If you want to dig deeper in SCPI, the electronic load we're building here on element14 may give a completer overview: Programmable Electronic Load . That one has a rich set of SCPI commands. The source…
  • genebren
    genebren over 6 years ago

    Very nice update.  I love the multi-color sketches.

    Gene

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 6 years ago in reply to 14rhb

    If you want to dig deeper in SCPI, the electronic load we're building here on element14 may give a completer overview: Programmable Electronic Load.

    That one has a rich set of SCPI commands. The source code is attached to that post.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • 14rhb
    14rhb over 6 years ago

    Jan,

     

    How very timely - a friend mentioned SCPI to me just this week, so I'm looking forward to reading through this series you've been posting in depth to try and learn some details.

     

    Rod

    • Cancel
    • Vote Up +2 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