Welcome to mirror list, hosted at ThFree Co, Russian Federation.

iButtonConnection.cs « iButtonLib - github.com/ClusterM/ibutton_client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b743fe73b87f7ec50a887650aa15db6ac23a202 (plain)
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cluster.iButtonLib
{
    public class iButtonConnection
    {
        public string Port { get; set; }

        public iButtonConnection(string port)
        {
            Port = port;
        }

        /// <summary>
        /// Открывает порт
        /// </summary>
        private SerialPort OpenPort()
        {
            SerialPort sPort;
            sPort = new SerialPort();
            sPort.PortName = Port;
            sPort.WriteTimeout = 500; sPort.ReadTimeout = 3000;
            sPort.BaudRate = 19200;
            sPort.Parity = Parity.None;
            sPort.DataBits = 8;
            sPort.StopBits = StopBits.One;
            sPort.Handshake = Handshake.None;
            sPort.DtrEnable = false;
            sPort.RtsEnable = false;
            sPort.NewLine = System.Environment.NewLine;
            sPort.Open();
            return sPort;
        }

        /// <summary>
        /// Проверяет - на этом ли порту наше устройство
        /// </summary>
        public bool Test()
        {
            bool result = false;
            SerialPort port = null;
            try
            {
                port = OpenPort();
                port.Write("\r\nname\r\n");
                while (true)
                {
                    var line = port.ReadLine().Trim();
                    if (line.Contains("iButton"))
                    {
                        result = true;
                    }
                }
            }
            catch { }
            finally
            {
                if (port != null)
                    port.Close();
            }
            return result;
        }

        /// <summary>
        /// Читает ключи из устройства
        /// </summary>
        public iButtonKey[] ReadKeys()
        {
            var port = OpenPort();
            try
            {
                var result = new List<iButtonKey>();
                port.Write("\r\nread\r\n");
                int keyCount = 1;
                do
                {
                    var line = port.ReadLine().Trim();
                    if (line.StartsWith("Count: "))
                    {
                        keyCount = byte.Parse(line.Substring(7), NumberStyles.AllowHexSpecifier);
                    }
                    else if (line.StartsWith("Key: "))
                    {
                        string keyLine = line.Substring(8);
                        result.Add(new iButtonKey(keyLine));
                    }
                } while (result.Count < keyCount);
                return result.ToArray();
            }
            finally
            {
                port.Close();
            }            
        }

        /// <summary>
        /// Стирает ключи из устройства
        /// </summary>
        public void Erase()
        {
            var port = OpenPort();
            try
            {
                port.Write("\r\nerase\r\n");
                while (true)
                {
                    var line = port.ReadLine().Trim();
                    if (line == "Done.") return;
                }
            }
            finally
            {
                port.Close();
            }
        }

        /// <summary>
        /// Записывает ключ в устройство
        /// </summary>
        public void Write(iButtonKey key)
        {
            var port = OpenPort();
            try
            {
                port.Write("\r\nwrite " + key.ToString() + "\r\n");
                while (true)
                {
                    var line = port.ReadLine().Trim();
                    if (line == "Done.") return;
                }
            }
            finally
            {
                port.Close();
            }
        }

        /// <summary>
        /// Перезагружает устройство
        /// </summary>
        public void Reboot()
        {
            var port = OpenPort();
            try
            {
                port.Write("\r\nreboot\r\n");
                while (true)
                {
                    var line = port.ReadLine().Trim();
                    if (line == "OK.") return;
                }
            }
            finally
            {
                port.Close();
            }
        }
    }
}