SpectroLink is the Python interface of SpectroLab. With the spectroLink.py module, a Python script drives an IndiGo spectrometer through a running SpectroLab instance: exposure, gain, light sources, captures, device values and colorimetry. This page covers installation, a first script, the full API, ready-to-use examples and the packet protocol.
User script + spectroLink.py <-- TCP 127.0.0.1:65357 --> SpectroLab <-- USB --> IndiGo
SpectroLink exists in two generations. Differences are flagged throughout this page:
- V2 – SpectroLab 2.x: one device, the one connected in the SpectroLab window.
- V3 – SpectroLab 3.x: several devices, addressed individually.
V2 / V3 – The V2 and V3
spectroLink.pyfiles are not interchangeable. Always use the file installed with the SpectroLab version you are running.
Contents: Requirements · Getting started · Python API · Examples · V2 / V3 summary · Protocol reference
1. Requirements and files
- Windows 10 / 11, 64-bit.
- SpectroLab running on the same computer, with or without its window (see 2.1). Download SpectroLab.
- For an external interpreter: Python 3 with numpy (matplotlib for the examples).
The files are installed with SpectroLab in <SpectroLab>\utils\python (by default C:\Program Files (x86)\GoyaLab\SpectroLab). Copy spectroLink.py next to your script.
| File | Content |
|---|---|
spectroLink.py |
Interface module |
pyExample.py |
Usage examples: captures, sources, device values, colorimetry |
test_headless.py |
Minimal capture test |
internallink.py, internal\ |
Used by SpectroLab’s internal math pipeline – not for user scripts |
2. Getting started
2.1 Start SpectroLab
SpectroLink needs a running SpectroLab instance, with its window or as a background process (no GUI):
cd "C:\Program Files (x86)\GoyaLab\SpectroLab"
start SpectroLab.exe --nogui
- Only one SpectroLab instance runs at a time.
- Wait for the end of initialisation (device detection, EEPROM read) before the first command: 3 to 20 s depending on the configuration.
- Stop a background instance with
taskkill /f /im SpectroLab.exe.
V3 – V3 also accepts
-ng. Starting the GUI while a background instance is running closes the background instance.
2.2 First script
Connect, set exposure and gain, and capture one spectrum. The helper wait_spectrum() is reused in the examples below. This script runs unchanged on V2 and V3.
import time
import numpy as np
from spectroLink import *
def wait_spectrum(link, timeout_s=5.0):
"""Returns the next spectrum, or None after timeout_s."""
t_end = time.time() + timeout_s
while time.time() < t_end:
if link.available() > 0:
pkt = link.read_packet()
if pkt.opcode() == SpectrolabOpcode.OPCODE_RECEIVED_SPECTRUM:
return pkt.to_spectrum()
else:
time.sleep(0.02)
return None
link = SpectroLink()
link.flush()
link.set_expo_ms(100)
link.set_gain_mdB(2000) # 2 dB
link.trigger_spectrum_capture()
s = wait_spectrum(link)
if s:
print(s.length_points, "points,", s.X[0], "to", s.X[-1], "nm")
2.3 Run a script from SpectroLab
A script can also be run from SpectroLab’s Python module. SpectroLab runs it through pyLink.exe, an embedded Python with numpy, scipy and matplotlib. The script must define spectrolab_run(): pyLink imports the file and calls this function. print() output appears in the module console.
from spectroLink import *
def spectrolab_run(): # entry point called by pyLink.exe
link = SpectroLink()
link.trigger_spectrum_capture()
s = wait_spectrum(link) # as defined in 2.2
print("max:", max(s.Y))
V2 / V3 – In V2, with an external interpreter, the line at the top of
spectroLink.pythat makes stdout unbuffered fails in consoles without a file descriptor (pythonw, some IDEs). Remove it in that case. V3 handles this case.
3. Python API
Methods of the SpectroLink class. In V3, every command method takes an extra keyword argument targetSPNIAddress (default: the device selected in SpectroLab, see 3.5).
3.1 Connection and reception
| Method | Description | V2 / V3 |
|---|---|---|
SpectroLink() |
Connects to SpectroLab. Raises ConnectionRefusedError if SpectroLab is not running. |
|
flush() |
Clears the received packets. | |
available() |
Reads the socket and returns the number of complete packets waiting. Must be polled. | |
read_packet() |
Returns the oldest waiting packet. | Empty queue: V2 [], V3 None |
await_packet(timeout_s) |
Waits for the next packet, polling every 100 ms. | V3 only |
read_sernum(addr) |
Returns the serial number of a device, or "" after 0.5 s. |
V3 only |
- Commands are asynchronous and not acknowledged. Only spectra, ADC data, device values and colorimetry results come back.
- Every connected script receives every packet, including spectra captured from the GUI: always test
opcode(). - A spectrum spans several socket reads: keep calling
available()until it returns a non-zero value.
3.2 Acquisition settings
| Method | Description |
|---|---|
set_expo_ms(expo_ms) |
Exposure time in ms (float). |
set_gain_mdB(gain_mdB) |
Gain in millidecibels: 2 dB = 2000. |
set_gain_lin(gain_lin) |
Linear gain, sent as 1000·log10(gain_lin) mdB. |
Settings stay active until changed, including for captures started from the GUI.
3.3 Captures
| Method | Description | V2 / V3 |
|---|---|---|
trigger_spectrum_capture(sourceId=-1) |
Starts a capture. Answer: one spectrum (one per device in V3). sourceId selects the source used by the capture sequence: 0 = none, n = n-th source counted from 1 as in the GUI, -1 = keep the current selection. |
|
trigger_dark_spectrum_capture() |
Dark capture, stored in SpectroLab. No answer. | V2 only |
trigger_reference_spectrum_capture(sourceId=-1) |
Reference capture, stored in SpectroLab. No answer. | V2 only |
trigger_adc_capture() |
Reads all ADC channels of the source module (UVx8). Answer: ADC data, read with to_adc_data(). |
V2 / V3 – In V3, handle dark and reference in the script: capture the dark with the source off, after setting exposure and gain, then subtract it (example 4.2).
3.4 Light sources
set_source(sourceId, isEnabled, pwmValue=-1, dacValue=-1) switches a source or sets its power:
| Call | Effect |
|---|---|
set_source(id, True) / set_source(id, False) |
Switches the source on / off. |
set_source(id, True, pwmValue=p) |
PWM duty cycle, 0–100 %. |
set_source(id, True, dacValue=d) |
DAC level, 0–100 %. |
set_source(id, True, pwmValue=p, dacValue=d) |
Both. |
When pwmValue or dacValue is given, isEnabled is not transmitted: only the power changes. Switch the source explicitly with set_source(id, True) / set_source(id, False).
V2 / V3 – Source index in
set_source(): V2 is 1-based, as in the SpectroLab GUI (1 = first source). V3 is 0-based, as in the firmware (0 = first source).
PWM period (V3 ≥ 3.0.9): set_source_pwm_period_us(periodUs, sourceId) sets the period in µs (note the argument order). set_source(..., pwmPeriodUs=p) sends it before the PWM value. The device clamps the value to the source limits (50–1 000 000 µs when the source has no EEPROM).
V2 / V3 – The V2 GUI applies the source’s default PWM period before enabling PWM. In V3, without
pwmPeriodUs, the device keeps its current period: passpwmPeriodUsto reproduce the V2 conditions.
3.5 Several devices (V3)
targetSPNIAddress selects the device:
| Constant (SPNIAddress) | Value | Target |
|---|---|---|
ADDRESS_CURRENT_DEVICE |
0x0FF0 | Device selected in SpectroLab (default) |
ADDRESS_ALL_DEVICES |
0x0FFE | All connected devices – one answer per device |
ADDRESS_BY_DEVICE_OFFSET + i |
0x0300 + i | Device of index i |
Each spectrum carries the address of its device in SPNIAddress. Identify devices by serial number with read_sernum() rather than by index (example 4.2).
3.6 Device values and colorimetry
| Method | Description | V2 / V3 |
|---|---|---|
request_read_device_data(keys) |
Requests one key or a list of keys. Answer: device values. | Keys: table below |
request_write_device_data(keys, values) |
Writes one or several uint32 values. | |
send_pipe_colo_spectrum(name, spectrum, illuminant=D65, degree=DEG_2) |
Computes colorimetry on a spectrum. Answer: 36 values, read with to_colorimetry_data(). |
V2 only |
| Key (SpectrolabDeviceKey) | V2 | V3 |
|---|---|---|
DEV_KEY_BOARD_TEMP |
Read | Read |
DEV_KEY_NB_ROWS |
Write | Read, write |
DEV_KEY_START_ROW |
Write | Read, write |
DEV_KEY_DEV_SN |
Read (key 4) | Read (key 0x0201), or read_sernum() |
DEV_KEY_CURR_SRC_NAME |
Read: selected source name | Read: selected source name |
| Other keys | – | SPNI model-value keys (communication protocol description, on request) |
V3 – Values read from the device come back with type NONE and the SPNI model key. Decode them with
to_str(),to_uint()orto_float().
3.7 Received packets
| SpectrolabPacket method | Returns |
|---|---|
opcode() |
SpectrolabOpcode |
to_spectrum() |
SpectrolabSpectrum: captureType, length_points, X (nm), Y; V3 adds SPNIAddress (source device) |
to_adc_data() |
SpectrolabAdcData: data (list of floats, one per channel) |
to_device_data() |
dict {key: SpectrolabDeviceValue}, with value(), to_uint(), to_float(), to_str() |
to_colorimetry_data() |
SpectrolabColorimetryData: CIE_X, CIE_x, Lab_L, Lab_a, Lab_b, CCT… |
4. Examples
4.1 Source in PWM mode
First source at 50 % PWM. Only the source index and the PWM period differ between versions.
# V2: 1-based index, PWM period set by SpectroLab
link.set_source(1, True)
link.set_source(1, True, pwmValue=50)
link.trigger_spectrum_capture()
s = wait_spectrum(link)
link.set_source(1, False)
# V3: 0-based index, explicit PWM period (>= 3.0.9)
link.set_source(0, True)
link.set_source(0, True, pwmValue=50, pwmPeriodUs=1000)
link.trigger_spectrum_capture()
s = wait_spectrum(link)
link.set_source(0, False)
4.2 Several devices with dark subtraction (V3)
# Map serial number -> device address
devices = {}
for i in range(8):
addr = SPNIAddress.ADDRESS_BY_DEVICE_OFFSET + i
sn = link.read_sernum(addr)
if sn:
devices[sn] = addr
def capture_all():
link.trigger_spectrum_capture(targetSPNIAddress=SPNIAddress.ADDRESS_ALL_DEVICES)
spectra = {}
for _ in devices:
s = wait_spectrum(link)
if s:
spectra[s.SPNIAddress] = s # address of the source device
return spectra
for addr in devices.values(): # settings first, then dark
link.set_expo_ms(100, targetSPNIAddress=addr)
link.set_gain_mdB(2000, targetSPNIAddress=addr)
link.set_source(0, False, targetSPNIAddress=addr)
dark = capture_all()
for addr in devices.values():
link.set_source(0, True, targetSPNIAddress=addr)
link.set_source(0, True, pwmValue=50, pwmPeriodUs=1000, targetSPNIAddress=addr)
time.sleep(0.5) # source stabilisation
light = capture_all()
for sn, addr in devices.items():
y = np.array(light[addr].Y) - np.array(dark[addr].Y)
print(sn, y.max())
4.3 Dark, reference and colorimetry (V2)
link.trigger_dark_spectrum_capture() # stored in SpectroLab, no answer
time.sleep(1) # no completion message: wait
link.trigger_reference_spectrum_capture(1) # reference with source 1
time.sleep(1)
link.trigger_spectrum_capture(1)
s = wait_spectrum(link)
link.send_pipe_colo_spectrum("sample", s, SpectroloabColorIlluminant.D65)
while True:
if link.available() > 0:
pkt = link.read_packet()
if pkt.opcode() == SpectrolabOpcode.OPCODE_PIPELINE_COLORIMETRY_IN:
colo = pkt.to_colorimetry_data()
print(colo.Lab_L, colo.Lab_a, colo.Lab_b, colo.CCT)
break
else:
time.sleep(0.02)
4.4 ADC readout (UVx8 module)
link.trigger_adc_capture()
while True:
if link.available() > 0:
pkt = link.read_packet()
if pkt.opcode() == SpectrolabOpcode.OPCODE_RECEIVED_ALL_CHANNELS_ADC_VALUE:
print(pkt.to_adc_data().data) # one value per channel
break
else:
time.sleep(0.02)
5. V2 / V3 summary
| Topic | V2 (SpectroLab 2.x) | V3 (SpectroLab 3.x) |
|---|---|---|
| Devices | One: the device connected in the GUI | Several, addressed individually (3.5) |
Source index in set_source() |
1-based (GUI numbering) | 0-based (firmware numbering) |
| PWM period | Set by SpectroLab | set_source_pwm_period_us(), pwmPeriodUs (≥ 3.0.9) |
| Dark / reference capture | Handled by SpectroLab | In the script |
| Colorimetry | Yes | No |
| Serial number | Key 4 | Key 0x0201; read_sernum() |
| Device value answers | Typed (STR, FLOAT…) | Type NONE, SPNI model key |
read_packet() on empty queue |
[] |
None |
| Helpers | – | await_packet(), read_sernum() |
| Spectrum packet | 4-byte header | 8-byte header, with the source device address |
6. Protocol reference
spectroLink.py builds and parses the packets for you. This section is only needed for debugging or for a client written in another language.
6.1 Transport
- TCP server opened by SpectroLab on 127.0.0.1, port 65357. Several clients can connect.
- Every packet sent by SpectroLab is broadcast to all clients.
- No acknowledgement, no CRC, no end key: the receiver resynchronises on the header.
- Little endian. Floats are IEEE-754 32-bit.
6.2 Frame
| Field | Bytes | Value |
|---|---|---|
| Header | 6 | 123, 56, 2, 3, 47, 231 (0x7B 0x38 0x02 0x03 0x2F 0xE7) |
| Sender address | 1 | See 6.3. SpectroLink: 7 (ADDRESS_PYLINK) |
| Number of destinations N | 1 | SpectroLink: 1 |
| Destination addresses | N | SpectroLink commands: 1 (ADDRESS_ALL_DEVICES) |
| Opcode | 1 | See 6.4 |
| Payload length L | 2 | uint16 |
| Payload | L | See 6.4 – 65 535 bytes maximum |
6.3 Addresses
| Value | Intercom address | Value | Intercom address |
|---|---|---|---|
| 0 | ADDRESS_ALL | 4 | ADDRESS_LIGHTING |
| 1 | ADDRESS_ALL_DEVICES | 5 | ADDRESS_EEPROM |
| 2 | ADDRESS_SPECTRUM | 6 | ADDRESS_TIMELAPSE |
| 3 | ADDRESS_BEER_LAMBERT | 7 | ADDRESS_PYLINK |
In V3, every command payload starts with the 4-byte target device address (uint32, see 3.5).
V2 / V3 – V2 payloads have no address prefix. A V2 client on V3, or the reverse, shifts every field.
6.4 Opcodes
Direction: → script to SpectroLab, ← SpectroLab to script. Payloads are given after the V3 address prefix.
| Code | Name (OPCODE_…) | Dir. | Payload | V2 | V3 |
|---|---|---|---|---|---|
| 1 | SET_EXPO_MS | → | float exposure (ms) | Yes | Yes |
| 2 | SET_GAIN_MDB | → | float gain (mdB) | Yes | Yes |
| 3 | TRIGGER_SPECTRUM_CAPTURE | → | Source (1): V2 optional uint8, V3 uint32 | Yes | Yes |
| 5 | TRIGGER_ALL_CHANNELS_ADC_CAPTURE | → | – | Yes | Yes |
| 6 / 7 | SET_SOURCE_ON / OFF | → | uint8 source | Yes | Yes |
| 8 | SET_SOURCE_PWM | → | uint8 source, uint8 PWM % | Yes | Yes |
| 15 | SET_SOURCE_DAC | → | uint8 source, uint8 DAC % | Yes | Yes |
| 16 | SET_SOURCE_PWM_AND_DAC | → | uint8 source, uint8 PWM %, uint8 DAC % | Yes | Yes |
| 23 | SET_SOURCE_PWM_PERIOD | → | uint8 source, uint32 period (µs) | No | ≥ 3.0.9 |
| 13 | REQUEST_READ_DEVICE_VALUE | → | uint8 n, n × uint32 key | Yes | Yes |
| 14 | REQUEST_WRITE_DEVICE_VALUE | → | uint8 n, n × uint32 key, n × uint32 value | Yes | Yes |
| 19 | TRIGGER_DARK_SPECTRUM_CAPTURE | → | – | Yes | No |
| 20 | TRIGGER_REF_SPECTRUM_CAPTURE | → | optional uint8 source (1) | Yes | No |
| 22 | PIPELINE_COLORIMETRY_OUT | → | See 6.7 | Yes | No |
| 9 | RECEIVED_SPECTRUM | ← | See 6.5 | Yes | Yes |
| 11 | RECEIVED_ALL_CHANNELS_ADC_VALUE | ← | uint8 n, uint8 size (4), n × float | Yes | Yes |
| 12 | RECEIVED_DEVICE_VALUE | ← | See 6.6 | Yes | Yes |
| 21 | PIPELINE_COLORIMETRY_IN | ← | uint16 name length, name, uint16 n (36), n × float | Yes | No |
| 17 / 18 | PIPELINE_MATH_SPEC_IN / OUT | ↔ | Internal math pipeline (internallink.py) | Int. | Int. |
(1) 0 = none, n = n-th source (1-based); absent (V2) or 0xFFFFFFFF (V3) = keep the current selection. Codes 4 and 10 are reserved. “No”: the packet is ignored.
6.5 Spectrum payload
| Field | Bytes | Description |
|---|---|---|
| Source device address (V3 only) | 4 | Address of the device that produced the spectrum |
| Capture type | 1 | 2 = wavelength (nm) |
| Number of points N | 2 | uint16 |
| Bytes per point | 1 | 4 (float32) |
| X | 4·N | Wavelengths (nm) |
| Y | 4·N | Intensities |
Only standard captures are returned. Dark and reference captures (V2) update SpectroLab’s internal dark and reference and produce no packet.
6.6 Device value payload
| Field | Bytes | Description |
|---|---|---|
| n | 1 | Number of values |
| Keys | 4·n | uint32 keys |
| Types | n | 0 NONE, 1 UINT8, 2 UINT16, 3 UINT32, 4 FLOAT, 5 STR |
| Lengths | 4·n | uint32, length in bytes of STR / NONE values |
| Values | Σ | 1, 2 or 4 bytes for numeric types, “length” bytes for STR / NONE |
Key values (SpectrolabDeviceKey): BOARD_TEMP 1, NB_ROWS 2, START_ROW 3, DEV_SN 4 (V2) / 513 (V3), CURR_SRC_NAME 5.
6.7 Colorimetry request (V2)
| Field | Bytes | Description |
|---|---|---|
| Name length | 2 | uint16 |
| Name | var. | UTF-8, returned with the result |
| Illuminant | 1 | A 0, B 1, C 2, D50 3, D65 4, E 5 |
| Observer | 1 | 0 = 2° |
| Capture type | 1 | 2 (nm) |
| N, bytes per point | 2 + 1 | uint16, 4 |
| X, Y | 4·N each | float32 |
The answer (PIPELINE_COLORIMETRY_IN) holds 36 floats, in this order: 15 calculation coefficients (xr … vppr), CIE XYZ, CIE xy, L*a*b*, RGB, LCHab, L*u*v*, LCHuv, CCT.
Need help with an integration, or the full device-key description for V3? Contact our R&D team. SpectroLab and SpectroLink are free, with unlimited licences: download SpectroLab.