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

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

namespace Cluster.iButtonLib
{
    public class iButtonConnectionUsb
    {
        const int VID = 0x0C16;
        const int PID = 0xDF05;

        public iButtonConnectionUsb()
        {
        }

        private HIDDevice OpenDevice()
        {
            return HIDDevice.FindDevice(VID, PID);
        }

        public bool IsPresent()
        {
            var device = OpenDevice();
            var present = device != null;
            if (present)
                device.Dispose();
            return present; 
        }

        public iButtonKey[] ReadKeys()
        {
            var device = OpenDevice();
            byte[] buffer = new byte[9];
            try
            {
                var result = new List<iButtonKey>();
                int keyCount = 0;
                bool initPacket;
                do
                {
                    initPacket = true;
                    device.Read(buffer, 0, 9);
                    for (int i = 2; i < 8; i++)
                    {
                        if (buffer[i] != 0xFF) initPacket = false;
                    }
                } while (!initPacket) ;
                keyCount = buffer[1];
                for (int key = 0; key < keyCount; key++)
                {
                    device.Read(buffer, 0, 9);
                    result.Add(new iButtonKey(buffer, 1));
                }
                return result.ToArray();
            }
            finally
            {
                device.Dispose();
            }            
        }

        public void Erase()
        {
            var device = OpenDevice();
            try
            {
                byte[] buffer = new byte[9] { 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
                device.Write(buffer, 0, 9);
            }
            finally
            {
                device.Dispose();
            }
        }

        public void Write(iButtonKey key)
        {
            var device = OpenDevice();
            try
            {
                byte[] buffer = new byte[9];
                buffer[0] = 0;
                var keyBuffer = key.ToArray();
                for (int i = 1; i <= 8; i++)
                    buffer[i] = keyBuffer[i - 1];
                device.Write(buffer, 0, 9);
            }
            finally
            {
                device.Dispose();
            }
        }
    }
}