SDC C-Project CF Review 프로그램
LYW
2021-08-09 b354c153b0074e5d54371bc05b12edbe8e613a19
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "StdAfx.h"
#include "CHSignalControls/SignalControl_DitSharedMemorySync.h"
#include "SharedMemoryDefine.h"
 
CSignalControl_DitSharedMemorySync::CSignalControl_DitSharedMemorySync(int nIndex, DWORD dwPeriod) : CSignalControl(nIndex, dwPeriod)
    , m_ReadEvent(FALSE, FALSE, _T("DIT_PLCConnector_Event_1"))
    , m_WriteEvent(FALSE, FALSE, _T("DIT_PLCConnector_Write_Complete_Event_1"))
{
    m_hMemMap        = NULL;
    m_pMemStartAddr    = NULL;
}
 
CSignalControl_DitSharedMemorySync::~CSignalControl_DitSharedMemorySync(void)
{
    //AfxMessageBox(_T("CSignalControl_MxComponent"));
 
    Disconnect();
}
 
int    CSignalControl_DitSharedMemorySync::Connect(const CSignalControlInfo* pControlInfo)
{
    if (pControlInfo==NULL) return 0;
    m_ControlInfo = *pControlInfo;
 
    // set signal info
    if (m_ControlInfo.GetIndex() < 0) return -1;
 
    m_nIndex = m_ControlInfo.GetIndex();
 
    m_nReceiveSignalCount = m_ControlInfo.GetReceiveSignalCount();
    m_nSendSignalCount = m_ControlInfo.GetSendSignalCount();
 
    // new signal pointer
    m_pReceiveSignalValueAddr = new int[m_nReceiveSignalCount]; 
    for (int i=0; i<m_nReceiveSignalCount; i++)
    {
        m_pReceiveSignalValueAddr[i] = _ttoi(m_ControlInfo.GetReadSignalAddress(i));
    }
    m_pReceiveSignalValue = new WORD[m_nReceiveSignalCount];   
    m_pReceiveSignalValuePrev = new WORD[m_nReceiveSignalCount];
    memset(m_pReceiveSignalValue, 0, sizeof(WORD)*m_nReceiveSignalCount);
    memset(m_pReceiveSignalValuePrev, 0, sizeof(WORD)*m_nReceiveSignalCount);
 
    m_pSendSignalValueAddr = new int[m_nSendSignalCount];
    memset(m_pSendSignalValueAddr, 0, sizeof(int)*m_nSendSignalCount);
    for (int i=0; i<m_nSendSignalCount; i++)
    {
        m_pSendSignalValueAddr[i] = _ttoi(m_ControlInfo.GetSendSignalAddress(i));
    }
    m_pSendSignalValue = new WORD[m_nSendSignalCount];   
    m_pSendSignalValuePrev = new WORD[m_nSendSignalCount];
    memset(m_pSendSignalValue, 0, sizeof(WORD)*m_nSendSignalCount);
    memset(m_pSendSignalValuePrev, 0, sizeof(WORD)*m_nSendSignalCount);
 
    DWORD dwMEMSize = DIT_SHARED_MEMORY_SIZE;
    // 4k Å©±âÀÇ °øÀ¯ ¸Þ¸ð¸® ÆÄÀÏ »ý¼º
    m_hMemMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, dwMEMSize, DIT_PCCONTROL_SHARED_MEMORY_NAME);
 
    // ¸¸ÀÏ À̹̠»ý¼ºµÈ °´Ã¼°¡ ÀÖ´Ù¸é À̹̠Connector°¡ ÀÛµ¿ Áß..
    if (::GetLastError() == ERROR_ALREADY_EXISTS)
    {
        // À̹̠»ý¼ºµÈ °´Ã¼°¡ Àִٸ頻ý¼ºÀÌ ¾Æ´Ï¶ó ¿ÀÇÂÀ¸·Î ½ÇÇà
        m_hMemMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, DIT_PCCONTROL_SHARED_MEMORY_NAME);
    }
    else // ¸¸ÀÏ »ý¼ºµÈ °´Ã¼°¡ ¾ø´Ù¸é Á¤»ó µ¿ÀÛÇÏÁö ¾Ê´Â´Ù.
    {
        if (m_hMemMap == NULL) return -2;
    }
 
    //°øÀ¯¸Þ¸ð¸® ¸ÊÇÎ
    m_pMemStartAddr = (char*)::MapViewOfFile(m_hMemMap, FILE_MAP_ALL_ACCESS, 0, 0, dwMEMSize);
 
    if (m_pMemStartAddr == NULL) return -3;
 
    //0À¸·Î ÃʱâÈ­
    ZeroMemory(m_pMemStartAddr, dwMEMSize);       
 
    m_bConnected = TRUE;
 
    return 1;
 
}
 
void CSignalControl_DitSharedMemorySync::Disconnect()
{
    if (m_pMemStartAddr!=NULL)
    {
        ::UnmapViewOfFile(m_pMemStartAddr);
        m_pMemStartAddr = NULL;
    }
 
    if (m_hMemMap != NULL)
    {
        ::CloseHandle(m_hMemMap);
        m_hMemMap = NULL;
    }
}
 
int CSignalControl_DitSharedMemorySync::Hex2Dec(const CString& hex_str)
{
    CString hex("0123456789ABCDEF");
 
    int        cnv = 1;
    int     dec = 0;
    for(int i=0 ; i<hex_str.GetLength() ; i++)
    {
        dec += hex.Find(hex_str.GetAt(hex_str.GetLength()-1-i))*cnv;
        cnv *= 16;
    }
    return dec;
}
 
BOOL CSignalControl_DitSharedMemorySync::Read_ReceiveSignal(int nAddrIndex, WORD& wValue)
{
    if(m_pMemStartAddr == NULL) return FALSE;
    if (m_ControlInfo.GetIndex() < 0) return FALSE;
 
    int nAddress = m_pReceiveSignalValueAddr[nAddrIndex];
    
    // copy signal
    memcpy(&wValue, m_pMemStartAddr+nAddress, sizeof(WORD));
 
    return TRUE;
}
 
BOOL CSignalControl_DitSharedMemorySync::Read_SendSignal(int nAddrIndex, WORD& wValue)
{
    if(m_pMemStartAddr == NULL) return FALSE;
    if (m_ControlInfo.GetIndex() < 0) return FALSE;
 
    int nAddress = m_pSendSignalValueAddr[nAddrIndex];
 
    memcpy(&wValue, m_pMemStartAddr+nAddress, sizeof(WORD));
 
    return TRUE;
}
 
 
 
BOOL CSignalControl_DitSharedMemorySync::Write_SendSignal(int nAddrIndex, int nSignalIndex, WORD wSignalValue, int nOnTime)
{
    if(nOnTime == 0)
{
    if(m_pMemStartAddr == NULL) return FALSE;
    if (m_ControlInfo.GetIndex() < 0) return FALSE;
 
 
    CSingleLock localLock(&m_csWriteSignal);
    localLock.Lock();
 
    if(m_pSendSignalValueAddr[nAddrIndex]==NULL) return FALSE;
 
    int nAddress = m_pSendSignalValueAddr[nAddrIndex];
 
    // read value
    WORD wCurValue = 0;
    memcpy(&wCurValue, m_pMemStartAddr+nAddress, sizeof(WORD));
 
    // send value
    WORD wSendValue = 0;
    if (wSignalValue==1)        // signal on
    {
        wSendValue = 1 << nSignalIndex;
        wSendValue = wSendValue | wCurValue;
    }
    else                                // signal off
    {
        if (wCurValue>0)
        {
            wSendValue = 1 << nSignalIndex;
            wSendValue = (wCurValue - wSendValue)<0 ? 0 : (wCurValue - wSendValue);
        }
    }
 
    memcpy(m_pMemStartAddr+nAddress, &wSendValue, sizeof(WORD));
 
 
    return TRUE;
}
    else
{
    if (m_pSignalThread==NULL) return FALSE;
 
    CSignalData signalData;
    signalData.nAddrIndex        = nAddrIndex;
    signalData.nSignalIndex        = nSignalIndex;
    signalData.wSignalValue        = wSignalValue;
    signalData.dwOnTime            = nOnTime;
 
    return m_pSignalThread->AddThreadData(signalData);
}
 
 
}
 
void CSignalControl_DitSharedMemorySync::IST2P_RunThreadProcess(const CSignalData& signalData)
{
    CSingleLock localLock(&m_csWriteSignal);
    localLock.Lock();
 
    if (m_nSendSignalCount<=0) return;
 
    if(m_pMemStartAddr == NULL) return;
 
    int nAddress = m_pSendSignalValueAddr[signalData.nAddrIndex];
 
    // read value
    WORD wCurValue = 0;
    memcpy(&wCurValue, m_pMemStartAddr+nAddress, sizeof(WORD));
 
    // send value
    WORD wSendValue = 0;
    if (signalData.wSignalValue==1)        // signal on
    {
        wSendValue = 1 << signalData.nSignalIndex;
        wSendValue = wSendValue | wCurValue;
    }
    else                                // signal off
    {
        if (wCurValue>0)
        {
            wSendValue = 1 << signalData.nSignalIndex;
            wSendValue = (wCurValue - wSendValue)<0 ? 0 : (wCurValue - wSendValue);
        }
    }
 
    memcpy(m_pMemStartAddr+nAddress, &wSendValue, sizeof(WORD));
 
    if (signalData.dwOnTime>0)
    {
        // wait
        ::Sleep(signalData.dwOnTime);
 
        // clear value
        memcpy(m_pMemStartAddr+nAddress, &wCurValue, sizeof(WORD));
    }
}
 
BOOL CSignalControl_DitSharedMemorySync::ReadData(const CString strAddr, int nSize, CString& strData)
{
    if(m_pMemStartAddr == NULL)    return FALSE;
 
    int nAddress = _ttoi(strAddr);
 
    // read value
    char *strValue = new char[nSize+1];
    memcpy(strValue, m_pMemStartAddr+nAddress, sizeof(char)*nSize);
 
    strData = strValue;
 
    delete [] strValue;
 
    return TRUE;
}
 
BOOL CSignalControl_DitSharedMemorySync::ReadData(const CString strAddr, int nSize, char* pData)
{
    if(m_pMemStartAddr == NULL)    return FALSE;
 
    int nAddress = _ttoi(strAddr);
 
    // read value
    memcpy(pData, m_pMemStartAddr+nAddress, sizeof(char)*nSize);
 
    return TRUE;
}
 
BOOL CSignalControl_DitSharedMemorySync::WriteData(const CString strAddr, int nSize, CString strData)
{
    if(m_pMemStartAddr == NULL)    return FALSE;
 
    int nAddress = _ttoi(strAddr);
 
    if( nSize > 1000) nSize = 1000;
 
    char strTemp[128] = {0,};
    ZeroMemory(strTemp, sizeof(char)*128);
 
    USES_CONVERSION;
    sprintf_s(strTemp, "%s", strData);    
    memcpy(m_pMemStartAddr+nAddress, &strTemp, sizeof(char)*nSize);
 
    return TRUE;
}
 
 
BOOL CSignalControl_DitSharedMemorySync::WriteWordData(const CString strAddr, int nSize, int nData)
{
    if(m_pMemStartAddr == NULL)    return FALSE;
 
    int nAddress = _ttoi(strAddr);
 
    if( nSize > 1000) nSize = 1000;
 
    WORD dwData = WORD(nData);
 
    memcpy(m_pMemStartAddr+nAddress, &dwData, sizeof(WORD));
 
    return TRUE;
}
 
 
BOOL CSignalControl_DitSharedMemorySync::WritePacketData(const CString strAddr, int nSize, short* pPacketData)
{
    if(m_pMemStartAddr == NULL)    return FALSE;
    if (m_ControlInfo.GetIndex() < 0) return FALSE;
 
    int strlength =  lstrlen(strAddr);
    if ( strlength > 6) strlength = 6;
 
    if( nSize > 10000) nSize = 10000;
    int nAddr = _ttoi(strAddr);
 
    memset(m_pMemStartAddr+nAddr, 0x00, nSize);
    memcpy(m_pMemStartAddr+nAddr, pPacketData, nSize);
    return TRUE;
}