0

I am currently looking for different possibilities to communicate with SCPI-compatible devices via C#. I've written my first code in LabWindows/CVI (language: C) via a COM interface and did it quite "manually" as I would say:

ComOpen(ComId, "Com1",....);
ComWrite("SYST:...\r");
ComRead(&value)`

Something like this. The problem was for me that I don't like pure C and the lack of OOP was just stupid since I have real objects that want to communicate. Dear National Instruments, why not use OOP to control objects?

Now somebody gave me the hint, that I should look into the global SCPI-documentation since there is more information than in the manual of the device. And I've found a .NET-compatible set of commands. For example,

IIviDCPwr Ivi.DCPwr.Create(String resourceName,
                           Boolean idQuery,
                           Boolean reset,
                           LockType lockType,
                           String accessKey,
                           String options);

to build up an object-oriented communication with SCPI-devices. Since I really like C# I want to try this in the next weeks.

But I really can not see the connection settings since resourceName only seems to be a name to free the regarded device afterwards and the other settings were used when the connection is already set. How can an instance of IIviDCPwr know which connection (TCP/IP, COM or GPIB) should be used?

I've only seen solutions for TCP/IP by using sockets. But I think this is not applicable to COM and GPIB?

Another solution would be to use the National Instruments VISA-package. But I don't like the idea of using a proprietary package provided by NI (I think after using CVI I feel a newly developed repulsion towards NI).

Can you provide code snippets or documentations to handle this problem? Have you made any experiences by using VISA? Can I use sockets for COM/GPIB-communication?

BTW: How to create new lines in code snippets? Neither enter nor Shift + Enter, \r, \n, \ works for me...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matze Katze
  • 11
  • 1
  • 3
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 17 '13 at 22:48
  • Also, what "code snippets" are you referring to? – John Saunders Mar 17 '13 at 22:48
  • 1
    Also, wtf is the question? – Keith Nicholas Mar 17 '13 at 23:15
  • Well my whole title is full of tags. But it summarizes my whole text. Why haven't you deleted GPIB, RS232/COM too? Even SCPI would be a good tag... Than the title would be: Communication via and using. Would be much better – Matze Katze Mar 17 '13 at 23:17
  • Questions I asked: Can you provide code snippets or documentations to handle this problem? Have you made any experiences by using VISA? Can I use sockets for COM/GPIB-communication? Any information would be helpful. And by code snippets I meant the gray blocks that are currently one-liners in my example but would be better readably by using line breaks – Matze Katze Mar 17 '13 at 23:17
  • Don't want too flood.. but since I have forgotten to direct my answer and editing is not longer possible I have to create another post-> @John – Matze Katze Mar 17 '13 at 23:26
  • 1
    All you have to do to enter code is to indent the code by four spaces. – John Saunders Mar 18 '13 at 05:06

1 Answers1

0

You can use the .NET SerialPort and Socket (without using VISA) to communicate with your devices, by using their read/write methods. About GPIB, your hardware should have a .NET driver, with similar methods.

Some example of how to communicate with device using SerialPort (with some pseudo-SCPI)

        SerialPort port = new SerialPort("COM1", 9600);
        port.Open();
        port.WriteLine(":DEVICE:OUT 1"); //turn on
        port.WriteLine(":DEVICE:MEAS:VOLT?"); //measure voltage
        float v = float.Parse(port.ReadLine()); //reading from device
        Console.WriteLine("The measured voltage is " + v );

Now you can create classes of your devices, where every instance would have his own protocol (SerialPort, Socket, GPIB), and methods

public class SomeDevice
{
    private SerialPort _port;
    public SomeDevice(string  serialPortName)
    {
        // do the connection work...
    }
    public void SetVoltage(float voltage)
    {
        port.WriteLine(":VOLT " + voltage.ToSring("N2"));
    }
    public float GetVoltage()
    {
        port.WriteLine(":DEVICE:MEAS:VOLT?");
        return float.Parse(port.ReadLine()); //reading from device
    }

}

You can even improve your classes to have the ability to use any kind of connection (SerialPort, Socket, GPIB...) by creating an interface (lets say - GenConnection) of general connection (with the common read/write methods), and then create child of your connection types implementing this interface - then each of your devices would have only GenConnection instance without "knowing" which kind of connection it realy is (just overload the constructor for each kind of connection)

Few points to keep in mind:

  • The programming manual help avoiding common errors
  • Connection configuration (baud, parity ...)
  • End of line character, you should keep in mind to send and separate readings with the device end of line - usually <CR> or <CR><LF> ("\r" or "\r\n" in C#).

    The SerialPort's ReadLine & WriteLine is doing it for you using the specified SerialPort's NewLine property

  • You can play with your port and socket using TeraTerm to see everything as expected
idanp
  • 973
  • 12
  • 18