MATLAB Answers

0

Controlling an SCPI instrument in Simulink

Asked by chris akins on 27 Jun 2017
Latest activity Edited by Marcelo Saboia on 19 Apr 2019
Hi all,
I’m working on interfacing a dSPACE-based controller with a power supply that speaks SCPI, and I’m trying to figure out how to use their Ethernet blockset to do it, which is much like Mathworks' TCP/IP Send and TCP/IP Receive blocks. Because of how compiled Simulink models interact with their Ethernet hardware and drivers, I cannot use the Instrument Control Toolbox.
An example query would be “volt:prot?\r\n” to query the overvoltage threshold, and “volt:prot 50\r\n” to set it. The query would receive a reply something like “50\r\n”, and the command would receive no reply at all.
I can compose outbound messages easily enough with array concatenation, but I’m at a loss as to how to parse variably-sized incoming replies, or to accumulate incoming bytes until a reply has completely come across the wire. For example, a command of “<> STA?\r\n” requests a status, and gets a reply like “, 1, 2, 3, 4\r\n”. If I requested a buffer of 100 bytes, I’d have to send the command several times, and I’d get a number of replies glued together; if I requested a buffer of 1 byte, I wouldn’t know what to do with it until I got the “\r\n” pattern. I could write a stream-oriented parser for incoming messages, but that wouldn’t know, for a reply of ‘1000’, if the first ‘1’ should be multiplied against 1, 10, 100, or 1000, so that’s not a great solution.
Pure-Simulink solutions are ideal, since I need to generate code from this model, but I may be able to modify function blocks to do what's needed - the restrictions of the environment and toolchain make it dicey.
Thanks in advance!

  1 Comment

Hello Chris,
I am trying to do almost the same thing you are doing (interfacing a dSPACE-based controller with a power supply that speaks SCPI) but in my case I need to use a Serial Communication (RS-232) and I would like to know how you send the SCPI Commands via SIMULINK.
I don't know how to convert a String Type variable (like your command “volt:prot?\r\n”) to ASCII to use as INPUT in the dSPACE Serial Communication blockset. My matlab is earlier than 2018a so a don't have the feature String to ASCII, and I am not pretty sure if this feature would be usefull.
Thanks in Advance,
Marcelo Saboia

Sign in to comment.

2 Answers

Answer by Vinod
on 30 Jul 2017
 Accepted Answer

Perhaps you can use the Simulink While Iterator to run a subsystem that reads in the data until a '/n' is received? In your subsystem you read one byte at a time and store it into a buffer that will then be used later in the processing chain.

  0 Comments

Sign in to comment.


Answer by chris akins on 31 Jul 2017

We found a workaround by parsing all returns as float64. It's not great, but it's good enough. I'll play with this, though, it might fix some other networking weaknesses we're fighting.

  0 Comments

Sign in to comment.