천호석
2022-06-17 4de9ea98e13449174aa2cd09c8351fc0e978f44a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
    }
}