Code Examples

Comprehensive programming tutorials and examples for Box PC development. Learn GPIO control, serial communication, and industrial automation.

Examples

Basic GPIO Control

Digital input/output operations

Python • 15 lines

Serial Communication

RS232/485 device communication

Python • 25 lines

Modbus TCP

PLC communication protocol

Python • 35 lines

Data Logger

Sensor data acquisition

Python • 45 lines

Web Interface

Flask-based control panel

Python • 60 lines

MQTT IoT Client

IoT device communication

Python • 40 lines
gpio_control.py
import RPi.GPIO as GPIO
import time
import signal
import sys

# GPIO pin configuration
LED_PIN = 18
BUTTON_PIN = 17

# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Initialize LED state
led_state = False
GPIO.output(LED_PIN, led_state)

def signal_handler(sig, frame):
    """Clean up GPIO on exit"""
    print('\\nCleaning up GPIO...')
    GPIO.cleanup()
    sys.exit(0)

# Register signal handler
signal.signal(signal.SIGINT, signal_handler)

def button_callback(channel):
    """Callback function for button press"""
    global led_state
    led_state = not led_state
    GPIO.output(LED_PIN, led_state)
    print(f"LED {'ON' if led_state else 'OFF'}")

# Add event detection for button
GPIO.add_event_detect(BUTTON_PIN, GPIO.FALLING, 
                     callback=button_callback, bouncetime=200)

print("GPIO Control Example Started")
print("Press button to toggle LED (Ctrl+C to exit)")

try:
    while True:
        time.sleep(0.1)
        
except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
    print("GPIO cleaned up successfully")

Interactive Demo

$ python gpio_control.py
GPIO Control Example Started
Press button to toggle LED (Ctrl+C to exit)
LED OFF
LED ON
LED OFF
LED
Digital Output (Pin 18)
OFF
Digital Input (Pin 17)
Press to toggle

Hardware API Reference

Complete documentation for Box PC hardware interfaces

GPIO Interface

SETUP /gpio/setup

Configure GPIO pin mode and initial state

Parameters: pin, direction, initial_state
WRITE /gpio/write

Set GPIO pin state (HIGH/LOW)

Parameters: pin, value
READ /gpio/read

Read GPIO pin state

Parameters: pin
Returns: 0 (LOW) or 1 (HIGH)

Serial Communication

OPEN /serial/open

Open serial port with specified parameters

Parameters: port, baudrate, bytesize, parity, stopbits
WRITE /serial/write

Send data through serial port

Parameters: data, encoding
READ /serial/read

Read data from serial port

Parameters: size
Returns: received data string

Development Tools

Essential tools and libraries for Box PC development

SDK Downloads

Complete software development kits for all supported programming languages.

  • • Python SDK v2.1.0
  • • C++ SDK v1.8.3
  • • Node.js SDK v1.5.2
  • • .NET SDK v1.3.1

Code Templates

Ready-to-use templates for common industrial applications.

  • • Basic I/O control
  • • Serial communication
  • • Modbus protocols
  • • Data logging systems

Video Tutorials

Step-by-step video guides for Box PC development.

  • • Getting Started (15 min)
  • • GPIO Programming (25 min)
  • • Industrial Protocols (35 min)
  • • Advanced Features (45 min)