# Comunication with instrument

**URL:** <https://discourse.julialang.org/t/comunication-with-instrument/24544>\
**Category:** New to Julia\
**Created:** [May 23, 2019, 10:41pm UTC](https://discourse.julialang.org/t/comunication-with-instrument/24544 "2019-05-23T22:41:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsharp](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rjsharp](https://discourse.julialang.org/u/rjsharp)\
**Post date:** [May 23, 2019, 10:41pm UTC](https://discourse.julialang.org/t/comunication-with-instrument/24544/1 "2019-05-23T22:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jarrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarrison/32/5740_2.png) [@jarrison](https://discourse.julialang.org/u/jarrison)\
**Post date:** [May 23, 2019, 11:23pm UTC](https://discourse.julialang.org/t/comunication-with-instrument/24544/2 "2019-05-23T23:23:35Z")

</div>

I don’t know your specific API but in my case I learned a good amount by looking through [Instruments.jl](https://github.com/BBN-Q/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.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 23, 2019, 11:33pm UTC](https://discourse.julialang.org/t/comunication-with-instrument/24544/3 "2019-05-23T23:33:52Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 23, 2019, 11:45pm UTC](https://discourse.julialang.org/t/comunication-with-instrument/24544/4 "2019-05-23T23:45:20Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![rjsharp](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rjsharp](https://discourse.julialang.org/u/rjsharp)\
**Post date:** [May 24, 2019, 10:37am UTC](https://discourse.julialang.org/t/comunication-with-instrument/24544/5 "2019-05-24T10:37:57Z")

</div>

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