OPBTPython Script Manual


OPBTPython Script Manual

1. Overview

1.1 What is OPBTPython?

OP-BTS is the world’s first Python‑enabled meter optical probe that runs scripts directly on the device (Optical Probe for Meter Communication). It can work standalone without a PC or handheld. With an on‑board MCU and 16 MB of accessible storage, you can run your own Python scripts on the probe with a single button press.

As a meter‑reading device, OP-BTS together with scripting lets you write simple Python programs to:

  • read meter data,
  • display results on the device or send them over Bluetooth,
  • and save data into the built‑in storage.

You upload scripts via the Windows application or over Bluetooth, then run them directly on the probe. The device supports transparent mode, automatic baud‑rate detection (Mode E / Mode C), Bluetooth 5.0, long battery life, and works with Android, iOS, Windows, macOS, and Linux.

OPBTPython is the Python scripting environment running on OP-BTS. It is based on the PikaPython core and provides a lightweight Python runtime within the resource limits of the device so you can use familiar syntax and libraries for meter reading and data processing.

1.2 What can you do with OPBTPython on OP-BTS?

  • Interactive REPL: Connect over serial or Bluetooth and enter a Python interactive shell for step‑by‑step testing and debugging.
  • Run downloaded scripts: Use the dedicated Windows application OpBtFileManager to compile and manage scripts, then run them directly on the probe.
  • File‑system scripts: Store scripts in the device file system to implement offline meter reading, periodic data logging, and scheduled tasks.

1.3 How to use this manual

This manual covers getting started, basic syntax, standard libraries, device APIs, and practical examples.
For detailed terminal usage, see Using the Terminal (Interactive Mode).
Use the table of contents or example index to jump to the section you need.


2. Getting Started

2.1 Environment preparation

Before writing, downloading, or running scripts interactively, prepare the following hardware and software according to how you plan to use the device.

Hardware and connections

  • OP-BTS device: Make sure your firmware supports OPBTPython (see the product manual or contact support).
  • Connection options:
    • Optical‑head docking: Use a USB docking optical probe to connect to OP-BTS and configure the same serial parameters (baud rate, etc.) as the device.
    • Bluetooth: Pair OP-BTS with your PC or phone over Bluetooth, then use it for script download or interactive REPL over Bluetooth.

PC software (pick one)

  • OpBtFileManager (recommended): Vendor‑provided Windows client for connecting to the device, uploading and running scripts, and viewing output. Supports direct optical‑head docking over USB, Bluetooth virtual COM port, Bluetooth SPP, and BLE connections.
  • Serial/terminal tools: If you use a USB or virtual COM port, you can use any serial terminal such as PuTTY, SecureCRT, or other recommended tools. These are used to send commands, enter the Python REPL, and view output. Make sure you select the correct COM port and baud rate.

Mobile (optional)

  • When using Bluetooth, you can use an Android or iOS app that supports SPP/BLE to communicate with OP-BTS, download scripts, or run them interactively (see the vendor’s mobile documentation for details).

2.2 Downloading and running scripts

Windows application OpBtPythonManager

  • Introduction and download: Obtain the installer from the device documentation or the official website (download link).
  • UI overview: After connecting to the device, select a script file, compile it, then click Download to write it to the device’s /py directory.
  • Steps: Connect device → select script file → click Download → wait for completion.
  • Progress and errors: The application shows download progress. If it fails, check the connection and serial/Bluetooth configuration.
  • Script execution: You can run scripts directly from the application UI, or on the device via MENUSCRIPTS, selecting and running the script.

2.3 First script example

opbt.device.SetOutputMode(1)  # send output to Bluetooth
print('Hello OPBTPython!')

2.4 Interactive mode

Entering interactive mode

Use a serial or terminal tool to send commands to the device. Commands must be sent as a complete line, and each line must end with a line ending (\r\n or \n).

For firmware version > 4.68:

OP>2

That is, send the string OP>2 followed by carriage‑return and newline (\r\n).

For older firmware version < 4.68, send two commands:

OP>1
{"PythonRunMode":1}

Command behavior

After the above commands are sent, the device starts the Python interactive terminal and shows the prompt >>>.

Important: output port selection

  • If you send OP>2\r\n from the optical‑head (serial) side, all subsequent REPL output will go to the optical‑head side.
  • If you send OP>2\r\n from the Bluetooth side, all subsequent REPL output will go to the Bluetooth side.

Using the REPL and exiting

  • At the >>> prompt, enter Python code and press Enter to execute.
  • Enter exit() to leave interactive mode; on firmware versions > 4.60 this usually causes the device to reboot (for other versions, see the “Device not responding” section).

Errors and exceptions

  • If code contains errors, the device prints an error message in the terminal.
  • To interrupt a long‑running script, hold the left key, then hold the right key until the red LED turns on; release the right key and keep holding the left key for more than 3 seconds to stop the current script (see the device manual for exact behavior).

Device not responding

  • Method 1: Hold the left key, then press and hold the right key for more than 3 seconds to force the device to power off.
  • Method 2: Press and hold the middle key for about 20 seconds to shut down the device.

For more detailed terminal usage, see Using the Terminal (Interactive Mode).


3. OPBTPython basics

OPBTPython supports a subset of standard Python 3 syntax.

3.1 Supported language features

  • Data types: int, float, str, bool, list, dict, tuple, bytes
  • Control flow: if, elif, else, while, for
  • Functions: def, including default arguments, variable‑length arguments, and keyword arguments
  • Classes: class with inheritance
  • Module import: import, from ... import

3.2 Operators

Supported operators include +, -, *, /, ==, !=, >, <, >=, <=, %, **, //, &, |, and, or, not, in, etc.

3.3 Quick examples

# variables and types
a = 1
b = "hello"
c = [1, 2, 3]

# condition and loop
if a > 0:
    print(b)
for x in c:
    print(x)

4. Standard libraries

The device scripting environment includes these standard modules: PikaStdLib, time, struct, os, math, etc.
For detailed APIs, see the API documentation.

4.1 PikaStdLib

  • Purpose: provide memory monitoring and related utilities.
  • Inline example:
import PikaStdLib
mem = PikaStdLib.MemChecker()
mem.max()   # print maximum memory usage
mem.now()   # print current memory usage

4.2 time module

  • Purpose: delays and time operations.
  • Inline example:
import time
time.sleep_ms(500)   # delay 500 ms

4.3 struct module

  • Purpose: pack and unpack binary data.
  • Inline example:
import struct
buf = struct.pack('hh', 1, 2)
a, b = struct.unpack('hh', buf)

4.4 os module

  • Purpose: file and path operations (platform‑dependent).
  • Inline example:
import os
p = os.path
p.join('dir', 'file.txt')

4.5 math module

  • Purpose: math functions.
  • Inline example:
import math
math.sqrt(2)
math.sin(0)

5. OP-BTS device API reference

Device scripts access hardware through the opbt module. See the OPBT device control API docs for full details.

5.1 Output mode

API Description Inline example
opbt.device.SetOutputMode(mode) Set output destination (0 auto, 1 Bluetooth, 2 optical probe, 3 LCD) opbt.device.SetOutputMode(1)
opbt.device.GetOutputMode() Get current output mode m = opbt.device.GetOutputMode()

5.2 Serial port

API Description Inline example
opbt.device.UartConfig(baudrate, parity, DataBit, StopBit) Configure UART parameters opbt.device.UartConfig(9600,"N",8,1)
opbt.device.UartSendByte(ch) Send a single byte opbt.device.UartSendByte(65)
opbt.device.UartGetByte() Read a byte, return -1 if no data ch = opbt.device.UartGetByte()
opbt.device.UartSendString(buf, len) Send a string (blocking) opbt.device.UartSendString("Hi",2)
opbt.device.UartSendStringNoWait(buf, len) Send a string (non‑blocking) opbt.device.UartSendStringNoWait("Hi",2)
opbt.device.UartSendBytes(buf, len) Send raw bytes (skipping protocol header) see device manual
opbt.device.TransparentModeOn()/Off() Enable/disable transparent transmission mode opbt.device.TransparentModeOn()
opbt.device.IecAutoOn()/Off() Enable/disable IEC auto‑baud mode opbt.device.IecAutoOn()

5.3 LEDs and keys

API Description Inline example
opbt.device.LedSet(leds, mode) Set LED behavior (mode: 0 off, 1 on, 2 blink, 4 fast blink, 8 toggle) opbt.device.LedSet(0x02,1)
opbt.device.LedBlink(leds, cnt, duty, time) Blink LEDs opbt.device.LedBlink(0x02,10,50,300)
opbt.device.GetKeyStatus() Read key status (bit 1 right key, bit 2 middle key) k = opbt.device.GetKeyStatus()

5.4 System and delay

API Description Inline example
opbt.device.SystemProcess() System processing, call periodically (e.g. with LedBlink) opbt.device.SystemProcess()
opbt.device.Sleep(ms) Delay in milliseconds opbt.device.Sleep(100)

5.5 LCD

API Description Inline example
opbt.device.LcdClear() Clear the screen opbt.device.LcdClear()
opbt.device.LcdTextColor(fc, bc) Set foreground and background colors opbt.device.LcdTextColor(0xF800,0x0000)
opbt.device.LcdTextXY(x, y) Set text position (x 0–19, y 0–4) opbt.device.LcdTextXY(0,0)
opbt.device.LcdPrint(str_p) Print a string opbt.device.LcdPrint("Hello")
opbt.device.LcdFill(xsta, ysta, xend, yend, color) Fill a rectangle opbt.device.LcdFill(0,0,10,10,0xFFFF)

For more parameters and color values, see ScriptDoc/cn/cn_module-opbt-device.md and the device manual.


6. Example index

This section is only an index; see each example document for full details.

6.1 OP-BTS device examples

  • OP-BTS device examples: LED, serial port, LCD, keys, transparent transmission, IEC auto‑baud, and combined usage.

6.2 OPBTPython built‑in feature examples

6.3 Standard‑library examples


7. Debugging and troubleshooting

  • Memory monitoring: Use MemChecker() and its max() / now() methods to observe memory usage.
  • Common issues: Check that imported modules are supported on this device; verify serial/Bluetooth connections; ensure the output mode matches the port you are using.
  • Debugging tips: Start with print() or LCD output for key variables; in interactive mode, run commands step by step to isolate problems.

8. Appendix

8.1 Quick API reference

See the tables in section 5 and the detailed device API documentation.

8.2 LCD color values

Color Hex value
White 0xFFFF
Black 0x0000
Red 0xF800
Green 0x07E0
Blue 0x001F
Yellow 0xFFE0

8.3 LED bit‑mask reference

Bit mask Description
0x01 Left red LED
0x02 Left green LED
0x04 Left blue LED
0x08 Right red LED
0x10 Right green LED
0x20 Right blue LED
0x40 Reserved