using System; using System.Collections.Generic; using System.Linq; using System.Text; using KHL = LampInspectionMachine.KHLplc.KvHostLink; using KHST = LampInspectionMachine.KHLplc.KvStruct; using System.Threading.Tasks; using LampInspectionMachine.Log4xml; using System.Windows.Controls; using System.Threading; namespace LampInspectionMachine.KHLplc { public class KHLCommunicate { byte[] readBuf = new byte[2048]; byte[] writeBuf = new byte[2048]; private bool isConnected=false; private int sock=0; public bool IsConnected { get => isConnected; set => isConnected = value; } public KHLCommunicate() { } public async Task ConnPlc() { while (true) { int err = 0; err = KHL.KHLInit(); if (err != 0) { LogHelper.Info("plc连接初始化错误:" + err); IsConnected = false; Thread.Sleep(1000); continue; } err = KHL.KHLConnect("192.168.1.10", 8500, 3000, KHLSockType.SOCK_TCP, ref sock); if (err != 0) { LogHelper.Info("plc连接错误:" + err); IsConnected = false; Thread.Sleep(1000); continue; } IsConnected = true; LogHelper.Info("plc连接成功"); break; } } public bool[] ReadBool(uint topno, ushort offset, uint bitnum) { if ( IsConnected ) { bool[] rdBool = new bool[bitnum]; int err = KHL.KHLReadDevicesAsBits(sock, KHLDevType.DEV_EM, topno, offset, bitnum, readBuf); if ( err != 0 ) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } KHST.ByteToBool(ref rdBool, readBuf, rdBool.Length, 0, 0); return rdBool; } return null; } public bool WriteBool(uint topno, ushort offset, uint bitnum,bool[] wrBool) { if ( IsConnected ) { bool[] rdBool = new bool[bitnum]; KHST.BoolToByte(ref writeBuf, wrBool, rdBool.Length, 0, 0); int err = KHL.KHLWriteDevicesAsBits(sock, KHLDevType.DEV_ZF, topno, offset, bitnum, writeBuf); if ( err != 0 ) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } return true; } return false; } public ushort[] ReadUshort(uint topno, uint wordnum) { ushort[] rdUshort = new ushort[wordnum]; int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, wordnum, readBuf); if ( err != 0 ) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } KHST.ByteToUshort(ref rdUshort, readBuf, rdUshort.Length, 0); return rdUshort; } public bool WriteUshort(uint topno, uint wordnum) { ushort[] rdUshort = new ushort[wordnum]; KHST.UshortToByte(ref writeBuf, rdUshort, rdUshort.Length, 0); int err = KHL.KHLWriteDevicesAsWords(sock, KHLDevType.DEV_ZF, topno, wordnum, writeBuf); if ( err != 0 ) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } return true; } public int ReadInt(uint topno) { int[] rdint = new int[1]; int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, 1, readBuf); if (err != 0) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0); return rdint[0]; } public int[] ReadInt(uint topno, uint wordnum) { int[] rdint = new int[wordnum]; int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, wordnum, readBuf); if ( err != 0 ) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0); return rdint; } public bool WriteInt(uint topno, uint wordnum, int[] rdInt) { KHST.IntToByte(ref writeBuf, rdInt, rdInt.Length, 0); int err = KHL.KHLWriteDevicesAsWords(sock, KHLDevType.DEV_ZF, topno, wordnum, writeBuf); if ( err != 0 ) { LogHelper.Info("plc连接读取错误:" + err); throw new Exception("plc连接读取错误"); } return true; } } }