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: ca6aa2eb381cc0b553ebc0743cdd81c3a712c5d2 (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
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;
        }

        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;
        }

        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;
        }

        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();
            }            
        }

        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();
            }
        }

        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();
            }
        }

        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();
            }
        }
    }
}