LTT Source에 들어가는 Module Dll
장정호
2020-12-15 dd4a016ef8fc5cae0cbd336a5bdec4651b1d87c1
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
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DIT.Framework.Module._01_SerialModules
{
    public class EnergyMax_PowerMeter
    {
        #region Field
        private const char _LF = '\n';
        private const char _CR = '\r';
        //object objlock;
        private SerialPort m_serialport;
        private string m_recvdata;
        #endregion
 
        #region Property
        public SerialPort SerialPort
        {
            get { return m_serialport; }
            private set { m_serialport = value; }
        }
 
        public string RecvData
        {
            get { return m_recvdata; }
            private set { m_recvdata = value; }
        }
        #endregion
 
        #region Constructor
        public EnergyMax_PowerMeter()
        {
            this.SerialPort = new SerialPort();
        }
        #endregion
 
        #region Method
        public bool Connect(string port, int timeout)
        {
            if (port == null) return false;
 
            if (this.SerialPort.IsOpen == false)
            {
                this.SerialPort.PortName = port;
                this.SerialPort.WriteTimeout = timeout;
                this.SerialPort.ReadTimeout = timeout;
                this.SerialPort.Open();
            }
 
            if (this.SerialPort.IsOpen == false) return false;
 
            return true;
        }
 
        public void DisConnect()
        {
            if (this.SerialPort.IsOpen == false) return;
 
            this.SerialPort.Close();
        }
 
        public bool Send(string cmd)
        {
            string data = "";
 
            data = cmd + _CR + _LF;
 
            try
            {
                this.SerialPort.Write(data);
            }
            catch (Exception)
            {
                return false;
            }
 
            return true;
        }
 
        public bool SendWaitReceive(string cmd)
        {
            string data = "";
 
            data = cmd + _CR + _LF;
 
            try
            {
                this.SerialPort.Write(data);
                if (this.Receive() == false) return false;
            }
            catch (Exception)
            {
                return false;
            }
 
            return true;
        }
 
        public bool Receive()
        {
            DateTime starttime;
 
            try
            {
                starttime = DateTime.Now;
 
                while (true)
                {
                    if((DateTime.Now - starttime).TotalMilliseconds > this.SerialPort.ReadTimeout)
                    {
                        this.RecvData = "Empty";
                        break;
                    }
                    else
                    {
                        this.RecvData = this.SerialPort.ReadLine();
                        if (this.RecvData == null) continue;
 
                        break;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
 
            return true;
        }
        #endregion
    }
}