# 多协议适配框架 **Repository Path**: chen-shiyun/MultiProtocolParsing ## Basic Information - **Project Name**: 多协议适配框架 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-04-18 - **Last Updated**: 2024-06-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 多协议设备适配 # 网口 ## iec104协议 >IEC 60870-5-101 provides a communication profile for sending basic telecontrol messages between a central telecontrol station and telecontrol outstations, which uses permanent directly connected data circuits between the central station and individual outstations. In some applications, it may be required to send the same types of application messages between telecontrol stations using a data network containing relay stations which store and forward the messages and provide only a virtual circuit between the telecontrol stations. This type of network delays messages by varying amounts of time depending on the network traffic load. In general, the variable message delay times mean that it is not possible to use the link layer as defined in IEC 60870-5-101 between telecontrol stations. However, in some cases it is possible to connect telecontrol stations having all three layers of the companion standard IEC 60870-5-101 to suitable data networks using Packet Assembler Disassembler (PAD) type stations to provide access for balanced communication. In all other cases this companion standard, which does not use the link functions of IEC 60870-5-101, may be used to provide balanced access via a suitable transport profile. 可以理解为IEC60870-1的基于tcp协议实现 ### 演示内容 #### appTest.exe 演示基础通讯 + 扫描成功,互发存活确认,记录,双端日志展示 + 扫描失败,记录设备掉线,扫描器记录,计数 #### extra.exe 演示 + 发送电路控制消息,app执行确认记录日志 ### 规定原文 https://webstore.iec.ch/preview/info_iec60870-5-104%7Bed2.0%7Den_d.pdf ### 实现思路 + 使用go的net包实现事件监听,接收[]byte数组 + 定义协议中用到的代码 + 解析报文 + 解析流程简化实现 ````go package iec104 import "fmt" func parseIEC104Message(apdu []byte) { if len(apdu) < 4 || apdu[0] != 0x68 { fmt.Println("Invalid APDU") return } apduLength := int(apdu[1]) if len(apdu) != apduLength+2 { fmt.Println("APDU length mismatch") return } controlField := apdu[2:4] fmt.Printf("Control Field: %x\n", controlField) asdu := apdu[4:] parseASDU(asdu) } func parseASDU(asdu []byte) { if len(asdu) < 10 { fmt.Println("Incomplete ASDU") return } typeID := asdu[0] variableStructureQualifier := asdu[1] causeOfTransmission := asdu[2:4] commonAddress := asdu[4:6] informationObjectAddress := asdu[6:9] informationElements := asdu[9:] fmt.Printf("Type ID: %d\n", typeID) fmt.Printf("Variable Structure Qualifier: %d\n", variableStructureQualifier) fmt.Printf("Cause of Transmission: %x\n", causeOfTransmission) fmt.Printf("Common Address: %x\n", commonAddress) fmt.Printf("Information Object Address: %x\n", informationObjectAddress) fmt.Printf("Information Elements: %x\n", informationElements) parseInformationElements(typeID, informationElements) } func parseInformationElements(typeID byte, elements []byte) { switch typeID { case 1: // 示例:单点信息 parseSinglePointInformation(elements) default: fmt.Printf("Unknown Type ID: %d\n", typeID) } } func parseSinglePointInformation(elements []byte) { for i := 0; i < len(elements); i++ { fmt.Printf("Information Element %d: %x\n", i, elements[i]) } } ```` + 发送过程 ````text +------------------------------------------------------+ | 发送方 (Client/SCADA) | +------------------------------------------------------+ | | | 启动连接 (TCP Handshake) | | | | +-------------------------+ | | | 建立TCP连接 | | | +-------------------------+ | | | | +-------------------------+ | | | 发送启动DT命令 | | | | (STARTDT ACT) | | | +-------------------------+ | | | | +-------------------------+ | | | 等待STARTDT CONF | | | | (确认启动DT) | | | +-------------------------+ | | | | +-------------------------+ | | | 发送监视指令或控制指令 | | | +-------------------------+ | | | | +-------------------------+ | | | 接收响应 | | | +-------------------------+ | | | | +-------------------------+ | | | 发送STOPDT命令 | | | | (停止DT) | | | +-------------------------+ | | | | +-------------------------+ | | | 关闭TCP连接 | | | +-------------------------+ | | | +------------------------------------------------------+ | v +------------------------------------------------------+ | 接收方 (Server/RTU) | +------------------------------------------------------+ | | | 启动连接 (TCP Handshake) | | | | +-------------------------+ | | | 接收TCP连接 | | | +-------------------------+ | | | | +-------------------------+ | | | 接收STARTDT命令 | | | +-------------------------+ | | | | +-------------------------+ | | | 发送STARTDT CONF | | | | (确认启动DT) | | | +-------------------------+ | | | | +-------------------------+ | | | 接收监视指令或控制指令 | | | +-------------------------+ | | | | +-------------------------+ | | | 发送响应 | | | +-------------------------+ | | | | +-------------------------+ | | | 接收STOPDT命令 | | | | (停止DT) | | | +-------------------------+ | | | | +-------------------------+ | | | 关闭TCP连接 | | | +-------------------------+ | | | +------------------------------------------------------+ ````