forked from DragDay7/posnet-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.cs
110 lines (103 loc) · 3.5 KB
/
Field.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Posnet
{
/// <summary>
/// Field creator.
/// </summary>
public partial class Field
{
/// <summary>
/// Initializes a new instance of the Field class using specified parameters.
/// </summary>
/// <param name="dataType">Type of data field.</param>
/// <param name="value">Content of a field.</param>
public Field(DataTypes dataType, object value)
{
DataType = dataType;
Value = value;
}
/// <summary>
/// Initializes a new instance of the Field class using bytes from serial port.
/// </summary>
/// <param name="bytes">Bytes of a field received within a message from serial port.</param>
internal Field(byte[] bytes)
{
switch((DataTypes)bytes[0])
{
case DataTypes.S:
Value = Encoding.Default.GetString(bytes, 1, bytes.Length - 2);
break;
case DataTypes.B:
Value = bytes[1];
break;
case DataTypes.V:
Value = BitConverter.ToUInt16(bytes, 1);
break;
case DataTypes.L:
Value = BitConverter.ToUInt32(bytes, 1);
break;
case DataTypes.N:
byte[] buf = new byte[bytes.Length - 1];
for (int i = 0; i < buf.Length; i++)
{
buf[i] = bytes[i + 1];
}
Value = FromBCD(buf);
break;
default:
throw new Exception("Unknown DataType in Field constructor.");
}
DataType = (DataTypes)bytes[0];
}
/// <summary>
/// Value of a field.
/// </summary>
public object Value { get; private set; }
/// <summary>
/// Type of data field.
/// </summary>
public DataTypes DataType { get; private set; }
/// <summary>
/// Returns field in bytes. No special characters SYN here.
/// </summary>
internal byte[] Bytes
{
get
{
byte[] tmp = new byte[0];
switch (DataType)
{
case DataTypes.S:
tmp = Encoding.ASCII.GetBytes((string)Value + '\0');
break;
case DataTypes.B:
tmp = new byte[] { (byte)Value };
break;
case DataTypes.V:
tmp = BitConverter.GetBytes((ushort)Value);
break;
case DataTypes.L:
tmp = BitConverter.GetBytes((uint)Value);
break;
case DataTypes.N:
tmp = ToBCD((long)Value);
break;
default:
throw new Exception("Field.Bytes; unknown DataType!");
}
int len = tmp.Length + 1;
byte[] result = new byte[len];
result[0] = (byte)DataType;
for (int i = 1; i < len; i++)
{
result[i] = tmp[i - 1];
}
return result;
}
}
}
}