Communication Examples

Python Example (DLOG mode, TCP)

The following example shows how to open a TCP socket towards a CT-BOX unit using its factory-default IP address and port, configure a data-logger acquisition and read a few measurements:

import socket

CTBOX_IP = "192.168.0.10"
CTBOX_PORT = 10001

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((CTBOX_IP, CTBOX_PORT))


def command(cmd):
    sock.sendall(cmd.encode() + b"\r")
    return sock.recv(4096).decode()


# Query the firmware version
print(command("VER:?"))

# Select the data-logger mode and a 1 Hz output rate
print(command("MODE:DLOG"))
print(command("FREQ:1"))

# Read a single current value [A]
print(command("GET"))

# Read an array of 5 consecutive measurements
print(command("GETA:5"))

sock.close()

Python Example (OSC mode, UDP data stream)

In oscilloscope mode commands are still sent over the TCP connection, while the acquired samples are streamed out over UDP to port 10002. Each sample is 8 bytes long: a 24-bit little-endian sequence number, an 8-bit status code and a 32-bit little-endian IEEE 754 float (see Data Representation).

import socket
import struct

CTBOX_IP = "192.168.0.10"
CTBOX_PORT = 10001
UDP_PORT = 10002

tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.connect((CTBOX_IP, CTBOX_PORT))

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind(("", UDP_PORT))


def command(cmd):
    tcp.sendall(cmd.encode() + b"\r")
    return tcp.recv(4096).decode()


command("MODE:OSC")
command("TS:1000")      # 1000 us averaging interval -> 1 kHz output rate
command("PRINT:ON")
command("ACQ:ON")

data, _ = udp.recvfrom(65536)
for offset in range(0, len(data) - 7, 8):
    seq_lo, seq_hi, status, current = struct.unpack_from("<HBBf", data, offset)
    print((seq_hi << 16) | seq_lo, hex(status), current)

command("ACQ:OFF")

tcp.close()
udp.close()

Python Example (DMM mode, TCP)

The following example shows how to open a TCP socket towards a CT-BOX unit using its factory-default IP address and port, configure a DMM:DC acquisition and read a few measurements:

import socket

CTBOX_IP = "192.168.0.10"
CTBOX_PORT = 10001

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((CTBOX_IP, CTBOX_PORT))


def command(cmd):
    sock.sendall(cmd.encode() + b"\r")
    return sock.recv(4096).decode()


# Query the firmware version
print(command("VER:?"))

# Select the DMM:DC mode, a 50 Hz DMM:FREQ and a 10 DMM:NOP
print(command("MODE:DMM"))
print(command("DMM:DC"))
print(command("DMM:FREQ:50"))
print(command("DMM:NOP:10"))

# Read a single current value [A]
print(command("GETA:1"))


sock.close()

Terminal Example

The CT-BOX ASCII protocol can also be exercised interactively using any raw TCP terminal client (e.g. PuTTY on Windows, or nc -C on Linux), by opening a Raw (not Telnet-negotiated) connection to the unit’s IP address and port:

$ nc -C 192.168.0.10 10001
VER:?
CT-BOX VER: 3.1.0

CTBOX:SN:?
150012

MODE:?
DMM

DMM:?
DC

STATUS:?
0X08111001

Tip

When using PuTTY, select connection type Raw, set Host Name to the CT-BOX IP address and Port to 10001, then type commands directly in the terminal window.

Over USB or RS-232 (DLOG mode only) the same commands can be issued from a standard serial terminal (PuTTY, Minicom) configured at 115 200 baud for RS-232, or on the virtual COM port created by the ST VCP driver for USB.