跳到主要内容

AI 驱动文档

您想了解什么?

TCP 通信设置

本指南向您展示如何使用 Node-RED 配置 OV10i 摄像头与外部设备之间的 TCP 通信。使用 TCP 通信实现实时数据交换、远程控制,或与自定义应用程序和系统的集成。

视频指南

请查看本主题的演示: Auto-Integration Builder

When to Use TCP Communication: 实时数据流传输、自定义应用程序集成、与外部系统的双向通信、高频数据交换,或在 HTTP/REST API 不适用时。

先决条件

  • OV10i 摄像头系统已设置并连接
  • 具备 TCP 通信能力的目标设备/系统
  • 摄像头与目标设备之间的网络连通性
  • 对 IP 地址和端口号有基本了解
  • 已配置活跃配方(成像与检验设置已完成)

Step 1: Verify Network Configuration

1.1 Check Camera IP Address

  1. 进入系统设置
  2. 记录相机 IP 地址(例如 192.168.0.100
  3. 验证子网掩码和网络配置

1.2 Confirm Target Device Network

确保网络兼容性:

  • 同一子网: 相机和目标设备必须处于同一网段
  • 可访问端口: 目标设备端口不得被防火墙阻挡
  • 网络连通性: 如有可能,请使用 ping 命令进行测试

1.3 Network Requirements

要求相机目标设备说明
IP 范围192.168.0.100192.168.0.xxx必须处于同一子网
子网掩码255.255.255.0255.255.255.0标准配置
端口访问49155(示例)49155(示例)避免保留端口
防火墙允许 TCP 流量允许 TCP 流量双向通信

Step 2: Access Node-RED Editor

2.1 Navigate to IO Block

  1. 在配方面包屑菜单中点击“IO Block”,或
  2. 从 Recipe Editor 选择“Configure IO”

2.2 Open Node-RED Editor

  1. 点击 Configure IO 进入 Node-RED 流编辑器
  2. 验证 Node-RED 界面是否正常加载

Checkpoint: 您应在左侧看到带有节点调色板的 Node-RED 流编辑器。

Step 3: Configure TCP Input (Receive Data)

3.1 Add TCP Input Node

  1. 在左侧面板(Network 部分)定位“tcp in”节点
  2. 将“tcp in”节点拖放到流画布上
  3. 双击节点以进行配置

3.2 Configure TCP Input Settings

节点配置:

设置描述
类型Listen on摄像头作为服务器
端口49155摄像头监听的端口
数据模式Stream连续数据流
数据类型UTF8基于文本的通信
Topic(optional)消息分类/路由标识(可选)

3.3 TCP Input Configuration Steps

  1. 服务器配置:
    • 选择 "Listen on port"(服务器模式)
    • 输入 端口号(例如 49155
  2. 数据处理:
    • 数据模式:选择 "Stream" 以获取连续数据
    • 数据类型:选择 "UTF8" 以处理文本,或选择 "Buffer" 以处理二进制
  3. 高级设置:
    • 换行符: 如不需要特定分隔符,请留空
    • Topic: 可选的消息路由标识符
  4. 点击 Done 以保存配置

3.4 端口选择指南

Port RangeUsageRecommendation
1-1023System reserved避免
1024-49151Registered ports检查可用性
49152-65535Dynamic/private推荐

Step 4: Configure TCP Output (Send Data)

4.1 Add TCP Output Node

  1. 在左侧面板定位 "tcp out" 节点(Network 部分)
  2. 将 "tcp out" 节点拖到流程画布上
  3. 双击节点 进行配置

image.png

4.2 Configure TCP Output Settings

Node Configuration:

SettingValueDescription
TypeConnect toCamera acts as client
Host192.168.0.200Target device IP address
Port49155Target device port
ModeClientOutbound connection

4.3 TCP Output Configuration Steps

  1. Connection Settings:
    • Type: Select "Connect to" (client mode)
    • Host: Enter target device IP address
    • Port: Enter target device port number
  2. Connection Options:
    • Mode: Keep as "Client"
    • End connection: Configure based on use case
  3. Data Format:
    • Base64: Usually disabled for text data
    • TLS: Enable only if secure connection required
  4. Click Done to save configuration

Step 5: Create Communication Flow

5.1 Build the Complete Flow

Create a flow that can both send and receive TCP data:

  1. 在画布上添加以下节点:
    • Inject 节点(用于触发消息)
    • Function 节点(用于消息处理)
    • TCP Out 节点(用于发送数据)
    • TCP In 节点(用于接收数据)
    • Debug 节点(用于监控)

5.2 Configure Inject Node

  1. 双击 Inject 节点
  2. 配置设置:
    • Name: "Send Message"
    • Payload: Timestamp
    • Topic: (留空)
  3. Click Done

5.3 Configure Function Node

The function node will format your outgoing message:

msg.payload = "Hello from OV20i camera";
return msg;

  1. 双击 Function 节点
  2. 将上面的代码复制到 "On Message" 选项卡
  3. Name: "Format Message"
  4. Click Done

5.4 Wire the Connections

按以下顺序连接节点:

Outgoing flow:

  • Inject → Function → TCP Out
  • Function → Debug(查看输出消息)

Incoming flow:

  • TCP In → Debug(查看输入消息)

5.5 Complete Flow Structure

Your final flow should have:

  • Inject 连接到 Function
  • Function 连接到同时连接 TCP OutDebug
  • TCP In 连接到单独的 Debug node

Result: You can send messages by clicking the inject button and see both outgoing and incoming messages in the debug panel.

Step 6: Configure Message Format

6.1 Define Message Format

Keep message structure simple:

Message TypeFormatExample
Simple textPlain string"Hello from camera"
Status updatesText with info"STATUS: READY"
Data valuesKey-value format"TEMPERATURE: 25.5"

6.2 自定义消息示例

您可以为不同类型的消息修改函数节点:

简单状态消息:

msg.payload = "Camera Ready";
return msg;

时间戳消息:

msg.payload = "Time: " + new Date().toLocaleString();
return msg;

带值数据:

msg.payload = "INSPECTION_COUNT: 42";
return msg;

Step 7: Deploy and Test Configuration

7.1 Deploy Node-RED Flow

  1. Click Deploy button (top-right corner)
  2. Verify deployment success message
  3. Check node status indicators:
    • Green dot: Successfully connected
    • Red dot: Connection error
    • Yellow dot: Attempting connection

7.2 Test TCP Communication

7.2.1 Basic Connectivity Test

Using command line tools:

# Test TCP connection (Linux/Mac)
telnet [camera-ip] [port]
# Example: telnet 192.168.0.100 49155

# Test with netcat
nc [camera-ip] [port]
# Example: nc 192.168.0.100 49155

Windows PowerShell:

Test-NetConnection -ComputerName 192.168.0.100 -Port 49155

7.2.2 Send Test Messages

  1. Connect to camera TCP port
  2. Send test commands:
    • "STATUS" → Should receive status response
    • "TRIGGER" → Should trigger inspection
    • "INVALID" → Should handle unknown command

7.2.3 Monitor Debug Output

  1. Open Node-RED debug panel (right sidebar)
  2. Send test messages via TCP
  3. Verify debug output shows:
    • Incoming messages
    • Processing results
    • Outgoing responses

7.3 Validation Checklist

TestExpected ResultStatus
TCP connectionSuccessful connection to camera port
Message receptionDebug shows incoming messages
Message processingFunction node processes correctly
Response sendingTarget device receives responses
Error handlingInvalid messages handled gracefully

Step 8: Integration with Inspection System

8.1 Connect to Inspection Triggers

Link TCP communication with inspection workflow:

  1. Add "All Block Outputs" node (if not already present)
  2. Connect inspection results to TCP output
  3. Format inspection data for TCP transmission

8.2 Inspection Data Integration

Function node to process inspection results:

// Get inspection results from All Block Outputs
const results = msg.payload;

// Extract key information
const inspectionSummary = {
result: results.pass ? "PASS" : "FAIL",
timestamp: new Date().toISOString(),
processing_time: results.processing_time,
roi_count: results.roi_results ? results.roi_results.length : 0
};

// Format for TCP transmission
msg.payload = JSON.stringify(inspectionSummary);
return msg;

8.3 Bidirectional Control

Enable remote control via TCP:

// Handle remote commands
const command = msg.payload.toString().toUpperCase();

switch(command) {
case "START_INSPECTION":
// Trigger inspection sequence
global.set("trigger_inspection", true);
msg.payload = "INSPECTION_STARTED";
break;

case "STOP_INSPECTION":
// Stop inspection sequence
global.set("trigger_inspection", false);
msg.payload = "INSPECTION_STOPPED";
break;

case "CHANGE_RECIPE":
// Recipe change logic
msg.payload = "RECIPE_CHANGED";
break;
}

return msg;

Step 9: Troubleshooting Common Issues

9.1 Connection Problems

ProblemSymptomsSolution
Cannot connectRed status indicatorCheck IP 地址和端口
Connection dropsIntermittent yellow statusVerify network stability
Timeout errorsDelayed responsesAdjust timeout settings
Port conflictsConnection refusedUse different port number

9.2 Data Transmission Issues

ProblemSymptomsSolution
No data receivedDebug shows empty messagesCheck data format settings
Corrupted dataGarbled text in debugVerify encoding (UTF8/Buffer)
Message lossMissing messagesCheck network stability
Large message issuesTruncated dataUse shorter messages

9.3 Debug Techniques

Systematic troubleshooting:

  1. Enable debug nodes at each step
  2. Monitor Node-RED logs for errors
  3. Test with simple TCP clients first
  4. Verify network connectivity with ping

Success! Your TCP Communication is Ready

Your TCP communication system can now:

  • Send and receive data between camera and external devices
  • Process simple messages for basic communication
  • Monitor data flow with debug nodes
  • Handle basic network communication for your applications

Ongoing Maintenance

Regular System Checks

  • Monitor connection stability over time
  • Verify data transmission works consistently
  • Check debug logs for any error patterns
  • Test communication after network changes

Next Steps

After setting up basic TCP communication:

  1. Test with your external systems using the established connection
  2. Customize message formats for your specific needs
  3. Add more complex logic as your requirements grow
  4. Consider other communication methods if TCP doesn't meet all needs

🔗 See Also

For high-throughput applications:

  1. Reduce message frequency
  2. Batch multiple messages
  3. Use binary format for large data
  4. Implement compression

Debug Techniques

Systematic troubleshooting:

  1. Enable debug nodes at each step
  2. Monitor Node-RED logs for errors
  3. Use network monitoring tools (Wireshark)
  4. Test with simple TCP clients first

Success! Your TCP Communication is Ready

Your TCP communication system can now:

  • Send and receive data between camera and external devices
  • Process commands for remote control
  • Transmit inspection results in real-time
  • Handle errors gracefully with proper error handling
  • Integrate with production systems for automated workflows

持续维护

常规系统检查

  • 随时间监控连接稳定性
  • 在生产环境中验证数据完整性
  • 按需更新安全配置
  • 基于使用模式优化性能

性能监控

  • 跟踪消息吞吐量和时延
  • 监控错误率和连接失败情况
  • 分析数据模式以发现优化机会

后续步骤

在设置好 TCP 通信后:

  1. 使用已建立的协议将外部系统与本系统集成
  2. 为生产使用实现全面的错误处理
  3. 为系统健康设置日志记录与监控
  4. 为生产部署考虑安全性增强措施

🔗 相关链接