-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataSend.cs
65 lines (55 loc) · 2.44 KB
/
DataSend.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
using socket_server.Object;
using System;
using System.Text;
namespace socket_server
{
public class DataSend
{
public static void Get(StateObject state, string[] data)
// [0] : ins
{
byte[] stx = new byte[1] { Constants.VALUE.STX };
byte[] send_time = new byte[7];
byte[] seq = new byte[2];
byte[] type = new byte[1] { 0x06 };
byte[] placeid = new byte[8] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
byte[] deviceid = new byte[2] { 0x00, 0x01 };
byte[] ins = new byte[2];
byte[] ml = new byte[2];
byte[] VD = null;
byte[] crc = new byte[2];
byte[] etx = new byte[1] { Constants.VALUE.ETX };
type = Convertion.IntToByteArray(Convert.ToInt32(Detail.get(state, Detail.TYPE.Type)), 1);
placeid = Encoding.ASCII.GetBytes(Detail.get(state, Detail.TYPE.PlaceID));
deviceid = Convertion.IntToByteArray(Convert.ToInt32(Detail.get(state, Detail.TYPE.DeviceID)), 2);
int a = 0;
var n = DateTime.Now.ToString("yyyyMMddHHmmss");
foreach (var bcd in Convertion.StringToHex(n)) {
send_time[a++] = bcd;
}
ins = Encoding.ASCII.GetBytes(data[0]);
switch (data[0]) {
case "1a":
VD = new byte[2] { Constants.VALUE.ACK, 0x00 };
break;
}
if (VD != null) {
ml[0] = (byte)(0x000000ff & (VD.Length >> 8));
ml[1] = (byte)(0x000000ff & (VD.Length));
}
byte[][] combineData_Temp = new byte[][] { stx, send_time, seq, type, placeid, deviceid, ins, ml, VD, crc, etx };
byte[] packet = Convertion.Combine(combineData_Temp);
byte[] array = new byte[packet.Length - 4];
Array.Copy(packet, 1, array, 0, packet.Length - 4);
crc = BitConverter.GetBytes(Convertion.Crc16.CalcCRC(array));
packet[packet.Length - 3] = crc[1];
packet[packet.Length - 2] = crc[0];
Server.Send(state.workSocket, packet);
data = new string[] { Detail.get(state, Detail.TYPE.PlaceID),
Detail.get(state, Detail.TYPE.DeviceID),
Detail.get(state, Detail.TYPE.Addr),
data[0] };
Database.Charger.Record.LeaveDataRaw(true, data, packet, false);
}
}
}