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
- 进入系统设置
- 记录相机 IP 地址(例如
192.168.0.100) - 验证子网掩码和网络配置
1.2 Confirm Target Device Network
确保网络兼容性:
- 同一子网: 相机和目标设备必须处于同一网段
- 可访问端口: 目标设备端口不得被防火墙阻挡
- 网络连通性: 如有可能,请使用 ping 命令进行测试
1.3 Network Requirements
| 要求 | 相机 | 目标设备 | 说明 |
|---|---|---|---|
| IP 范围 | 192.168.0.100 | 192.168.0.xxx | 必须处于同一子网 |
| 子网掩码 | 255.255.255.0 | 255.255.255.0 | 标准配置 |
| 端口访问 | 49155(示例) | 49155(示例) | 避免保留端口 |
| 防火墙 | 允许 TCP 流量 | 允许 TCP 流量 | 双向通信 |
Step 2: Access Node-RED Editor
2.1 Navigate to IO Block
- 在配方面包屑菜单中点击“IO Block”,或
- 从 Recipe Editor 选择“Configure IO”
2.2 Open Node-RED Editor
- 点击
Configure IO进入 Node-RED 流编辑器 - 验证 Node-RED 界面是否正常加载
Checkpoint: 您应在左侧看到带有节点调色板的 Node-RED 流编辑器。
Step 3: Configure TCP Input (Receive Data)
3.1 Add TCP Input Node
- 在左侧面板(Network 部分)定位“tcp in”节点
- 将“tcp in”节点拖放到流画布上
- 双击节点以进行配置
3.2 Configure TCP Input Settings
节点配置:
| 设置 | 值 | 描述 |
|---|---|---|
| 类型 | Listen on | 摄像头作为服务器 |
| 端口 | 49155 | 摄像头监听的端口 |
| 数据模式 | Stream | 连续数据流 |
| 数据类型 | UTF8 | 基于文本的通信 |
| Topic | (optional) | 消息分类/路由标识(可选) |
3.3 TCP Input Configuration Steps
- 服务器配置:
- 选择 "Listen on port"(服务器模式)
- 输入 端口号(例如
49155)
- 数据处理:
- 数据模式:选择 "Stream" 以获取连续数据
- 数据类型:选择 "UTF8" 以处理文本,或选择 "Buffer" 以处理二进制
- 高级设置:
- 换行符: 如不需要特定分隔符,请留空
- Topic: 可选的消息路由标识符
- 点击
Done以保存配置
3.4 端口选择指南
| Port Range | Usage | Recommendation |
|---|---|---|
| 1-1023 | System reserved | 避免 |
| 1024-49151 | Registered ports | 检查可用性 |
| 49152-65535 | Dynamic/private | 推荐 |
Step 4: Configure TCP Output (Send Data)
4.1 Add TCP Output Node
- 在左侧面板定位 "tcp out" 节点(Network 部分)
- 将 "tcp out" 节点拖到流程画布上
- 双击节点 进行配置
4.2 Configure TCP Output Settings
Node Configuration:
| Setting | Value | Description |
|---|---|---|
| Type | Connect to | Camera acts as client |
| Host | 192.168.0.200 | Target device IP address |
| Port | 49155 | Target device port |
| Mode | Client | Outbound connection |
4.3 TCP Output Configuration Steps
- Connection Settings:
- Type: Select "Connect to" (client mode)
- Host: Enter target device IP address
- Port: Enter target device port number
- Connection Options:
- Mode: Keep as "Client"
- End connection: Configure based on use case
- Data Format:
- Base64: Usually disabled for text data
- TLS: Enable only if secure connection required
- Click
Doneto save configuration
Step 5: Create Communication Flow
5.1 Build the Complete Flow
Create a flow that can both send and receive TCP data:
- 在画布上添加以下节点:
- Inject 节点(用于触发消息)
- Function 节点(用于消息处理)
- TCP Out 节点(用于发送数据)
- TCP In 节点(用于接收数据)
- Debug 节点(用于监控)
5.2 Configure Inject Node
- 双击 Inject 节点
- 配置设置:
- Name: "Send Message"
- Payload: Timestamp
- Topic: (留空)
- Click
Done
5.3 Configure Function Node
The function node will format your outgoing message:
msg.payload = "Hello from OV20i camera";
return msg;
- 双击 Function 节点
- 将上面的代码复制到 "On Message" 选项卡
- Name: "Format Message"
- 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 Out 和 Debug
- 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 Type | Format | Example |
|---|---|---|
| Simple text | Plain string | "Hello from camera" |
| Status updates | Text with info | "STATUS: READY" |
| Data values | Key-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
- Click
Deploybutton (top-right corner) - Verify deployment success message
- 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
- Connect to camera TCP port
- Send test commands:
- "STATUS" → Should receive status response
- "TRIGGER" → Should trigger inspection
- "INVALID" → Should handle unknown command
7.2.3 Monitor Debug Output
- Open Node-RED debug panel (right sidebar)
- Send test messages via TCP
- Verify debug output shows:
- Incoming messages
- Processing results
- Outgoing responses
7.3 Validation Checklist
| Test | Expected Result | Status |
|---|---|---|
| TCP connection | Successful connection to camera port | ☐ |
| Message reception | Debug shows incoming messages | ☐ |
| Message processing | Function node processes correctly | ☐ |
| Response sending | Target device receives responses | ☐ |
| Error handling | Invalid messages handled gracefully | ☐ |
Step 8: Integration with Inspection System
8.1 Connect to Inspection Triggers
Link TCP communication with inspection workflow:
- Add "All Block Outputs" node (if not already present)
- Connect inspection results to TCP output
- 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
| Problem | Symptoms | Solution |
|---|---|---|
| Cannot connect | Red status indicator | Check IP 地址和端口 |
| Connection drops | Intermittent yellow status | Verify network stability |
| Timeout errors | Delayed responses | Adjust timeout settings |
| Port conflicts | Connection refused | Use different port number |
9.2 Data Transmission Issues
| Problem | Symptoms | Solution |
|---|---|---|
| No data received | Debug shows empty messages | Check data format settings |
| Corrupted data | Garbled text in debug | Verify encoding (UTF8/Buffer) |
| Message loss | Missing messages | Check network stability |
| Large message issues | Truncated data | Use shorter messages |
9.3 Debug Techniques
Systematic troubleshooting:
- Enable debug nodes at each step
- Monitor Node-RED logs for errors
- Test with simple TCP clients first
- 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:
- Test with your external systems using the established connection
- Customize message formats for your specific needs
- Add more complex logic as your requirements grow
- Consider other communication methods if TCP doesn't meet all needs
🔗 See Also
- MQTT Communication Setup
- RS-232 Communication Setup
- Connect to PLC (Ethernet/IP, PROFINET)
- Node-RED Basics
- Overview Node-RED Custom Blocks
- IP & Network Discovery Failures
For high-throughput applications:
- Reduce message frequency
- Batch multiple messages
- Use binary format for large data
- Implement compression
Debug Techniques
Systematic troubleshooting:
- Enable debug nodes at each step
- Monitor Node-RED logs for errors
- Use network monitoring tools (Wireshark)
- 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 通信后:
- 使用已建立的协议将外部系统与本系统集成
- 为生产使用实现全面的错误处理
- 为系统健康设置日志记录与监控
- 为生产部署考虑安全性增强措施