123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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;
- }
- }
- }
|