mrDarker
2025-08-22 f8ad0695ff2431cb90640be52b523d6434bdbf83
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
 
#include "stdafx.h"
#include "ExternLightControlAKPNV.h"
 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
 
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
CExternLightControlAKPNV::CExternLightControlAKPNV()
{
    m_hLightControl = NULL;
}
 
CExternLightControlAKPNV::~CExternLightControlAKPNV()
{
    CloseControl();
}    
 
BOOL CExternLightControlAKPNV::OpenControl(int nPort)
{
    m_nPort = nPort;
    
    DCB dcb;
 
    CString strPort;
    strPort.Format(_T("COM%d"), nPort);
    
    m_hLightControl = CreateFile( strPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
 
    if (m_hLightControl == INVALID_HANDLE_VALUE)
    {
        CloseHandle(m_hLightControl);
        m_hLightControl = NULL;
        return FALSE;
    }
    
    dcb.DCBlength = sizeof(DCB);
    if (GetCommState(m_hLightControl,(LPDCB) &dcb))
    {
        dcb.DCBlength        = sizeof(DCB);
        dcb.BaudRate        = CBR_9600;
        dcb.ByteSize        = 8;
        dcb.fParity            = FALSE;
        dcb.Parity            = NOPARITY;
        //        dcb.StopBits        = 1;
        dcb.fOutxCtsFlow    = 0;
        dcb.fOutxDsrFlow    = 0;
        dcb.fOutX            = 0;
        dcb.fInX            = 0;
        
        if (!SetCommState(m_hLightControl,(LPDCB) &dcb))
        {
            CloseHandle(m_hLightControl);
            m_hLightControl = NULL;
            return FALSE;
        }
    }
    else
    {
        CloseHandle(m_hLightControl);
        m_hLightControl = NULL;
        return FALSE;
    }    
    
    return TRUE;
}
 
BOOL CExternLightControlAKPNV::CloseControl()
{
    if (m_hLightControl)
        CloseHandle(m_hLightControl);
    m_hLightControl = NULL;
    
    return TRUE;
}
 
BOOL CExternLightControlAKPNV::SetLightControlValue(int nChannel, int nValue)
{
    if (nChannel < 0 || nChannel > MAX_LAMP_CHANNEL)
        return FALSE;
    if (!m_hLightControl)
        return FALSE;
    
    int nHigh = nValue / 16;
    int nLow = nValue % 16;
 
    CString hex("0123456789ABCDEF");
 
    char Data[50];
    memset(&Data[0],0x00,sizeof(Data));    
 
    sprintf_s(Data, "C%c%c%c%c%c", hex.GetAt(nChannel+1), hex.GetAt(nHigh), hex.GetAt(nLow), ASCII_CR, ASCII_LF);
 
    DWORD NumberOfBytesWritten;
 
    BOOL bRet = WriteFile( m_hLightControl,(LPCVOID) Data,6,(LPDWORD) &NumberOfBytesWritten,NULL);
    
    m_nLightValue = nValue;
 
    return bRet;
}
 
BOOL CExternLightControlAKPNV::IsTurnOnLamp(int iIdxChannel)
{
    
    return TRUE;
}
 
BOOL CExternLightControlAKPNV::TurnOnOffLamp(int nChannel,BOOL bOn)
{
    if (nChannel < 0 || nChannel > MAX_LAMP_CHANNEL)
        return FALSE;
    if (!m_hLightControl)
        return FALSE;
 
    char Data[50];
    memset(&Data[0],0x00,sizeof(Data));
 
    if(bOn == TRUE)
        sprintf_s(Data, "AP11%c%c", ASCII_CR, ASCII_LF);
    else
        sprintf_s(Data, "AP00%c%c", ASCII_CR, ASCII_LF);
 
    DWORD NumberOfBytesWritten;
 
    BOOL bRet = WriteFile( m_hLightControl,(LPCVOID) Data,6,(LPDWORD) &NumberOfBytesWritten,NULL);
    
    return bRet;
}