Code Examples

Last updated:2026-10-14 11:13:06

Category: Interface — Embedded Direct-Connect (TCP)

Compare Code Examples — these only illustrate the call method of this protocol and do not apply to the Internet interface.

What to prepare before starting?

First confirm the internal unit model, firmware, and device IP; the current communication spec uses a local HTTP address and CMD_ID commands.

The category name contains "TCP", but the actual request method follows "Communication Conventions and General Specifications". The call examples here apply only to the embedded protocol.

Replace {deviceIP} with the actual IP (consistent with the communication conventions); the default port is 8555; do not hardcode a single machine's address.

1. cURL

HOST="http://{deviceIP}:8555"
curl -X POST "$HOST/api/app_print" \
  -H "Content-Type: application/json" \
  -d "{\"CMD_ID\":\"00\",\"TYPE\":1}"
curl -X POST "$HOST/api/app_print" \
  -H "Content-Type: application/json" \
  -d "{\"CMD_ID\":\"01\",\"IMG_URL\":\"http://192.168.1.20:8080/jobs/a.png\",\"WIDTH\":100}"
curl -X POST "$HOST/api/app_print" \
  -H "Content-Type: application/json" \
  -d "{\"CMD_ID\":\"02\",\"TYPE\":1,\"MOVE\":10}"
curl -X POST "$HOST/api/app_print" \
  -H "Content-Type: application/json" \
  -d "{\"CMD_ID\":\"03\"}"
curl -X POST "$HOST/api/app_print" \
  -H "Content-Type: application/json" \
  -d "{\"CMD_ID\":\"04\"}"

2. C# (original heading labeled "C"; code is C#)

using System.Net.Http.Json;

var host = Environment.GetEnvironmentVariable("PRINTER_HOST")
           ?? "http://{deviceIP}:8555";
var http = new HttpClient { BaseAddress = new Uri(host) };

async Task<JsonElement> Call(object body) {
    var resp = await http.PostAsJsonAsync("/api/app_print", body);
    resp.EnsureSuccessStatusCode();
    var json = await resp.Content.ReadFromJsonAsync<JsonElement>();
    if (json.GetProperty("code").GetInt32() != 0)
        throw new Exception(json.GetProperty("msg").GetString());
    return json;
}

await Call(new { CMD_ID = "00", TYPE = 1 });
await Call(new { CMD_ID = "01", IMG_URL = "http://192.168.1.20:8080/jobs/a.png", WIDTH = 100 });
await Call(new { CMD_ID = "02", TYPE = 1, MOVE = 10 });
await Call(new { CMD_ID = "03" });
await Call(new { CMD_ID = "04" });

3. Java

Java 11+ java.net.http.HttpClient. Set the environment variable PRINTER_HOST to http://actual-IP:8555, or replace the placeholder with the device IP.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

String host = System.getenv().getOrDefault("PRINTER_HOST", "http://{deviceIP}:8555");
HttpClient http = HttpClient.newHttpClient();

String call(String jsonBody) throws Exception {
    HttpRequest req = HttpRequest.newBuilder()
            .uri(URI.create(host + "/api/app_print"))
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
            .build();
    HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString());
    if (resp.statusCode() / 100 != 2)
        throw new RuntimeException("HTTP " + resp.statusCode() + " " + resp.body());
    String body = resp.body();
    if (!body.contains("\"code\":0") && !body.contains("\"code\": 0"))
        throw new RuntimeException(body);
    return body;
}

call("{\"CMD_ID\":\"00\",\"TYPE\":1}");
call("{\"CMD_ID\":\"01\",\"IMG_URL\":\"http://192.168.1.20:8080/jobs/a.png\",\"WIDTH\":100}");
call("{\"CMD_ID\":\"02\",\"TYPE\":1,\"MOVE\":10}");
call("{\"CMD_ID\":\"03\"}");
call("{\"CMD_ID\":\"04\"}");

If the device encounters an unsolvable problem during debugging, contact the cloud-printing owner to troubleshoot and find a solution. Contact details are as follows:

  • Phone: 137-5177-4454 (WeChat same number)
  • Email: info@ColorPark.cn