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; namespace LampInspectionMachine.KHLplc { public class KHLCommunicate { byte[] readBuf = new byte[2048]; byte[] writeBuf = new byte[2048]; private bool isConnected=false; private bool isNormal=false; private int sock=0; public bool IsConnected { get => isConnected; set => isConnected = value; } public bool IsNormal { get => isNormal; set => isNormal = value ; } public bool ConnPlc() { int err = 0; err = KHL.KHLInit(); if ( err != 0 ) { LogHelper.Info("plc连接初始化错误:" + err); IsConnected = false; IsNormal=false; return false; } err = KHL.KHLConnect("192.168.0.10", 8500, 3000, KHLSockType.SOCK_TCP, ref sock); if ( err != 0 ) { LogHelper.Info("plc连接错误:" + err); IsConnected = false; IsNormal = false; return false; } IsConnected = true; IsNormal = true; LogHelper.Info("plc连接成功"); return true; } 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 ) { IsNormal = false; LogHelper.Info("plc连接读取错误:" + err); return rdBool; } 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 ) { IsNormal = false; LogHelper.Info("plc连接读取错误:" + err); return false; } 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 ) { IsNormal = false; LogHelper.Info("plc连接读取错误:" + err); return rdUshort; } 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 ) { IsNormal = false; LogHelper.Info("plc连接读取错误:" + err); return false; } return true; } 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 ) { IsNormal = false; LogHelper.Info("plc连接读取错误:" + err); return rdint; } KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0); return rdint; } public bool WriteInt(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 ) { IsNormal = false; LogHelper.Info("plc连接读取错误:" + err); return false; } return true; } } }