using MMCE_Test; using System; using System.Collections; using System.Linq; namespace SHARP_CLAS_UI { public class DO : Slave { #region Enum #endregion #region Property public ushort Channel_Size { get; private set; } public bool[] Status { get; private set; } private uint datasize { get; set; } #endregion #region Construct public DO(ushort Board_Id, ushort Device_Id, ushort Channel_Size) :base(Board_Id, Device_Id) { this.Channel_Size = Channel_Size; this.Status = new bool[Channel_Size]; this.datasize = (uint)Channel_Size / 8; } #endregion #region Function public bool Get_Ouput() { try { byte[] recvdata = new byte[datasize]; NMCSDKLib.MC_STATUS mc; mc = NMCSDKLib.MC_IO_READ(Board_Id, Device_Id, 0, 0, datasize, recvdata); if (mc == NMCSDKLib.MC_STATUS.MC_OK) { BitArray inputs = new BitArray(recvdata); bool[] data; data = new bool[inputs.Count]; int i = 0; foreach (bool input in inputs) { data[i] = input; i++; } Array.Copy(data, Status, Status.Count()); return true; } else { throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, " + mc.ToString()); } } catch (Exception ex) { //WriteExceptionLog(MethodBase.GetCurrentMethod().Name, ex); return false; } } public bool Set_Output(int byte_offset, int bit_offset, bool check) { try { if ((0 <= byte_offset && byte_offset * 8 < Channel_Size) && (0 <= bit_offset && bit_offset < 8)) { uint byteoffset = uint.Parse($"{byte_offset}"); byte bitoffset = byte.Parse($"{bit_offset}"); NMCSDKLib.MC_STATUS mc; mc = NMCSDKLib.MC_IO_WRITE_BIT(Board_Id, Device_Id, byteoffset, bitoffset, check); if (mc == NMCSDKLib.MC_STATUS.MC_OK) return true; else throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, " + mc.ToString()); } else throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, byte_offset({byte_offset}) or bit_offset({bit_offset}) range over."); } catch(Exception ex) { //WriteExceptionLog(MethodBase.GetCurrentMethod().Name, ex); return false; } } public bool Set_Output(int bit, bool check) { try { if (0 <= bit && bit < Channel_Size) { byte[] data = new byte[datasize]; BitArray bitarray = new BitArray(Status); bitarray[bit] = check; bitarray.CopyTo(data, 0); NMCSDKLib.MC_STATUS mc; mc = NMCSDKLib.MC_IO_WRITE(Board_Id, Device_Id, 0, datasize, data); if (mc == NMCSDKLib.MC_STATUS.MC_OK) return true; else throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, " + mc.ToString()); } else throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, bit({bit}) range over."); } catch (Exception ex) { //WriteExceptionLog(MethodBase.GetCurrentMethod().Name, ex); return false; } } public bool Set_Output_Bit(int bit, bool check) { try { if (0 <= bit && bit < Channel_Size) { uint Offset = 0; byte bit_offset = 0; Offset = (uint)bit / 8; bit_offset = BitConverter.GetBytes(bit % 8)[0]; NMCSDKLib.MC_STATUS mc; mc = NMCSDKLib.MC_IO_WRITE_BIT(Board_Id, Device_Id, Offset, bit_offset, check); if (mc == NMCSDKLib.MC_STATUS.MC_OK) return true; else throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, " + mc.ToString()); } else throw new Exception($"BoardID : {Board_Id}, AxisID : {Device_Id}, bit({bit}) range over."); } catch (Exception ex) { //WriteExceptionLog(MethodBase.GetCurrentMethod().Name, ex); return false; } } #endregion } }