Comunication with instrument

Hi everyone, I started to using Julia a couple of weeks ago for a small project.

My goal is to build a HPLC system, and not only being able to work the data acquired but also to control some of the equipment.

I am facing some issues communicating with a knauer pump. I am using the following code:

julia> using Sockets

julia> s=connect(“10.1.1.10”,10001) #pump ip and port
TCPSocket(Base.Libc.WindowsRawSocket(0x0000031c) open, 0 bytes waiting)

julia> write(s,“FLOW 100 \r”) #write 100uL/min
10

this works ok and I am able to setup the desired pump velocity, however I would like to read the strings that the pump returns after issuing each command. This would be usefull to acquire a pressure reading through the command PRESSURE.

When I use putty to communicate by telnet I have the following output:

>“FLOW 100”
FLOW OK

>“PRESSURE”
PRESSURE 0.1

is there any way of having something similar in Julia?

thanks in advance

I don’t know your specific API but in my case I learned a good amount by looking through Instruments.jl and the Clang.jl generated wrapper around their C-API. Although they use the NI-VISA protocol, most of the overall methods should be relatively similar to what you are trying to do (in particular, the ‘query’ function sends a command and reads the resulting buffer). If your instruments have an easily accessible C/C++ interface it is so awesome to just let Clang.jl wrap that ugly C-code for you.

1 Like

Doing this kind of thing should be quite straightforward. Does the instrument respond to each command with a string followed by a '\r'?

In that case I think you can do something like

write(s, "PRESSURE\r")
response = readuntil(s, '\r')
m = match(r"^PRESSURE +([0-9.]+)$", response)
if m !== nothing
    pressure = parse(Float64, m[1])
end
2 Likes

By the way, you can test all this without your instrument by starting up a separate julia process (or a separate async Task within the same process), and listening on a local port:

using Sockets

server = listen(ip"127.0.0.1", 10001)

con = accept(server)

while true
    request = readuntil(con, '\r')
    if request == "PRESSURE"
        write(con, "PRESSURE $(rand())\r")
    elseif occursin(r"^FLOW +[0-9]+$", request)
        write(con, "FLOW OK\r")
    # elseif More stuff to simulate your instrument
    else
        write(con, "ERROR\r")
    end
end

Then connect to your newly mocked out instrument on 127.0.0.1 and you can do quick unit tests without having the physical hardware.

6 Likes

Thanks for the help, I will definitely look into both approaches and will let you know the outcome.