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

github.com/ClusterM/ibutton_client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2014-01-22 23:19:41 +0400
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2014-01-22 23:19:41 +0400
commit3bd3974d4917711623750445114920795a23ae33 (patch)
tree41a9595d49eee877657730399f430ca2b32bae72
parent8819be668e9a1bcb3946755a3094caeeddd30e99 (diff)
Комментарии
-rw-r--r--iButonManager/FormIButtonManager.cs2
-rw-r--r--iButtonLib/iButtonConnection.cs312
2 files changed, 167 insertions, 147 deletions
diff --git a/iButonManager/FormIButtonManager.cs b/iButonManager/FormIButtonManager.cs
index 5d54010..cb51ce5 100644
--- a/iButonManager/FormIButtonManager.cs
+++ b/iButonManager/FormIButtonManager.cs
@@ -12,6 +12,8 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
+// Внимание! Ниже много говнокода :)
+
namespace Cluster.iButtonManager
{
public partial class FormIButtonManager : Form
diff --git a/iButtonLib/iButtonConnection.cs b/iButtonLib/iButtonConnection.cs
index ca6aa2e..7b743fe 100644
--- a/iButtonLib/iButtonConnection.cs
+++ b/iButtonLib/iButtonConnection.cs
@@ -1,147 +1,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;
- }
-
- 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();
- }
- }
- }
-}
+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();
+ }
+ }
+ }
+}