Java与opc通信之二 - s7协议(HslCommunication)
1、引入依赖
<dependency> <groupId>com.github.dathlin</groupId> <artifactId>HslCommunication</artifactId> <version>3.1.1</version> </dependency>
2、使用SiemensS7Net获取opc数据
/** * 读取DIO类型的PLC数据 */ public static void test() { SiemensS7Net siemensTcpNet = new SiemensS7Net(SiemensPLCS.S400, "xxx"); //105代表读取的长度 OperateResultExOne<byte[]> read = siemensTcpNet.Read("I32", (short) 1); OperateResultExOne<boolean[]> read1 = siemensTcpNet.ReadBool("I188", (short) 1); OperateResultExOne<Boolean> i188 = siemensTcpNet.ReadBool("I188.4"); OperateResultExOne<Boolean> i1881 = siemensTcpNet.ReadBool("I188.5"); OperateResultExOne<Boolean> i1882 = siemensTcpNet.ReadBool("I188.6"); if (read.IsSuccess) { // I74: 10100101 // 2:1 4:0 0:1 3:0 // I188: 11000100 // 4:0 5:0 3:0 2:1 7:1 6:1 byte[] content = read.Content; String str = getBinaryStrFromByte(content[0]); System.out.println("getDoubleData:"+str); //2是下标 //short no = siemensTcpNet.getByteTransform().TransInt16(content, 2); //System.out.println(no); } }