# gos7 **Repository Path**: fierce_wolf/gos7 ## Basic Information - **Project Name**: gos7 - **Description**: 在原版github.com/robinson/gos7的基础上,补齐了不支持S7协议的布尔值设置的重要特性缺失。 - **Primary Language**: Go - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: http://www.fox-tech.cn - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-09 - **Last Updated**: 2025-01-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: S7, 西门子 ## README # gos7 ## 背景 原版的github.com/robinson/gos7,已经长期停更了。 robinson在开发gos7的时候,采用的思路是在S7协议的基础上,把S7设备当内存表,然后以byte数组的方式进行刷内存。 他的这个方案的思路,简单粗暴,但是确实能解决了90%以上的S7 PLC操作的场景。 但是,实际上很多工程项目中,那个剩下的10%特性缺失,对使用者来说,才是最要命的。 比如,bool类型的写入,原作者robinson的方法是先将表示bool的bit位编码成一个byte,然后刷入PLC内存表。 但是,他这个方法,会导致你在写入一个bit位的bool变量时,将同一个byte上的其他7个bit误覆盖了。 也就是说,原作者robinson的原版代码,是不支持bool变量操作的。 灵狐曾经纠结,是不是要重写一个完整S7协议栈的gos7?考虑到手头上已经有一个S7协议栈的JAVA完整实现,好像投入产出并不划算。 所以,灵狐多次考虑之后,最后的想法,gos7不就是缺失S7协议的某些实现嘛,将这个缺失的实现部分补齐,不就OK了? 所以,灵狐在robinson原代码gos7的基础上,将S7协议中的bool量设置,补齐进来 ## 原文 Implementation of Siemens S7 protocol in golang Overview ------------------- For years, numerous drivers/connectors, available in both commercial and open source domains, have supported the connection to S7 family PLC devices. GoS7 fills the gaps in the S7 protocol, implementing it with pure Go (also known as golang). There is a strong belief that low-level communication should be implemented with a low-level programming language that is close to binary and memory. The minimum supported Go version is 1.13. Functions ------------------- AG: * Read/Write Data Block (DB) (tested) * Read/Write Merkers(MB) (tested) * Read/Write IPI (EB) (tested) * Read/Write IPU (AB) (tested) * Read/Write Timer (TM) (tested) * Read/Write Counter (CT) (tested) * Multiple Read/Write Area (tested) * Get Block Info (tested) PG: * Hot start/Cold start / Stop PLC * Get CPU of PLC status (tested) * List available blocks in PLC (tested) * Set/Clear password for session * Get CPU protection and CPU Order code * Get CPU/CP Information (tested) * Read/Write clock for the PLC Helpers: * Get/set value for a byte array for types: value(bit/int/word/dword/uint...), real, time, counter Supported communication ----------------- * TCP * Serial (PPI, MPI) (under construction) How to: ---------- following is a simple usage to connect with PLC via TCP ```go const ( tcpDevice = "127.0.0.1" rack = 0 slot = 2 ) // TCPClient handler := gos7.NewTCPClientHandler(tcpDevice, rack, slot) handler.Timeout = 200 * time.Second handler.IdleTimeout = 200 * time.Second handler.Logger = log.New(os.Stdout, "tcp: ", log.LstdFlags) // Connect manually so that multiple requests are handled in one connection session handler.Connect() defer handler.Close() //init client client := gos7.NewClient(handler) address := 2710 start := 8 size := 2 buffer := make([]byte, 255) value := 100 //AGWriteDB to address DB2710 with value 100, start from position 8 with size = 2 (for an integer) var helper gos7.Helper helper.SetValueAt(buffer, 0, value) err := client.AGWriteDB(address, start, size, buffer) buf := make([]byte, 255) //AGReadDB to address DB2710, start from position 8 with size = 2 err := client.AGReadDB(address, start, size, buf) var s7 gos7.Helper var result uint16 s7.GetValueAt(buf, 0, &result) ``` References ---------- - libnodave http://libnodave.sourceforge.net/ - snap7 http://snap7.sourceforge.net/ - tarm serial library https://github.com/tarm/serial - Simatic Open TCP/IP Communication via Industrial Ethernet from Siemens(doku) - SIMATIC NET FDL-Programmierschnittstelle (doku) - Elementary Data Types from Siemens (doku) Simatic, Simatic S5, Simatic S7, S7-200, S7-300, S7-400, S7-1200, S7-1500 are registered Trademarks of Siemens License ---------- https://opensource.org/licenses/BSD-3-Clause Copyright (c) 2018, robinson