2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
10 using System.Threading;
11 using System.Globalization;
12 using System.Diagnostics;
14 namespace CLEON_Connector
21 private void serialPort1_DataReceived(
object sender, SerialDataReceivedEventArgs e)
23 SerialPort sp = (SerialPort)sender;
25 byte receivedByte = 0;
28 while (sp.BytesToRead > 0)
31 receivedByte = (byte)sp.ReadByte();
34 ProcessReceivedByte(receivedByte);
40 private void SendUSBCommand(byte command)
43 USBSsndFrame[0] = Constants.serialFrameHeader;
44 USBSsndFrame[1] = Constants.serialFrameHeader;
47 byte[] bytes =
new byte[4];
53 case Constants.commandConnect:
55 USBSsndFrame[3] = command;
59 case Constants.commandUpdateRTCTime:
61 int year, month, day, hour, minute, second;
63 while (DateTime.Now.Millisecond != 0) ;
72 USBSsndFrame[3] = command;
73 USBSsndFrame[4] = (byte)(year >> 8);
74 USBSsndFrame[5] = (byte)(year & 0xff);
75 USBSsndFrame[6] = (byte)month;
76 USBSsndFrame[7] = (byte)day;
77 USBSsndFrame[8] = (byte)hour;
78 USBSsndFrame[9] = (byte)minute;
79 USBSsndFrame[10] = (byte)second;
83 case Constants.commandUpdateTimeTick:
85 while (DateTime.Now.Millisecond != 0) ;
86 currentTimeTick = DateTime.UtcNow.Ticks;
88 currentTimeTick = (long)(currentTimeTick / 10000000);
89 currentTimeTick *= 10000000;
92 USBSsndFrame[3] = command;
93 USBSsndFrame[4] = (byte)(currentTimeTick & 0xff);
94 USBSsndFrame[5] = (byte)(currentTimeTick >> 8);
95 USBSsndFrame[6] = (byte)(currentTimeTick >> 16);
96 USBSsndFrame[7] = (byte)(currentTimeTick >> 24);
97 USBSsndFrame[8] = (byte)(currentTimeTick >> 32);
98 USBSsndFrame[9] = (byte)(currentTimeTick >> 40);
99 USBSsndFrame[10] = (byte)(currentTimeTick >> 48);
100 USBSsndFrame[11] = (byte)(currentTimeTick >> 56);
102 case Constants.commandUpdateSampleCount:
103 bytes = BitConverter.GetBytes(iSampleCount);
105 USBSsndFrame[3] = command;
106 USBSsndFrame[4] = bytes[0];
107 USBSsndFrame[5] = bytes[1];
108 USBSsndFrame[6] = bytes[2];
109 USBSsndFrame[7] = bytes[3];
111 case Constants.commandUpdateSampleGap:
112 bytes = BitConverter.GetBytes(iSampleGap);
114 USBSsndFrame[3] = command;
115 USBSsndFrame[4] = bytes[0];
116 USBSsndFrame[5] = bytes[1];
117 USBSsndFrame[6] = bytes[2];
118 USBSsndFrame[7] = bytes[3];
120 case Constants.commandUpdateChunkCount:
121 bytes = BitConverter.GetBytes(iChunkCount);
123 USBSsndFrame[3] = command;
124 USBSsndFrame[4] = bytes[0];
125 USBSsndFrame[5] = bytes[1];
126 USBSsndFrame[6] = bytes[2];
127 USBSsndFrame[7] = bytes[3];
129 case Constants.commnadUpdateChunkGap:
130 bytes = BitConverter.GetBytes(iChunkGap);
132 USBSsndFrame[3] = command;
133 USBSsndFrame[4] = bytes[0];
134 USBSsndFrame[5] = bytes[1];
135 USBSsndFrame[6] = bytes[2];
136 USBSsndFrame[7] = bytes[3];
143 USBSsndFrame[33] = BuildCRC(USBSsndFrame);
146 USBSsndFrame[34] = Constants.serialFrameFooter;
147 USBSsndFrame[35] = Constants.serialFrameFooter;
150 serialPort1.Write(USBSsndFrame, 0, Constants.serialFrameLength);
154 private byte BuildCRC(byte[] targetArray)
156 byte ucCheckOctet = 0;
158 for (
int i = 3; i < (Constants.serialFrameLength - 3); i++)
162 ucCheckOctet = targetArray[i];
166 ucCheckOctet ^= targetArray[i];
170 ucCheckOctet = (byte)~ucCheckOctet;
176 private void ProcessReceivedByte(byte receivedByte)
178 switch (USBRecvFrameByteCount)
182 if (receivedByte == Constants.serialFrameHeader)
184 USBRecvFrame[USBRecvFrameByteCount] = receivedByte;
185 USBRecvFrameByteCount++;
189 USBRecvFrameByteCount = 0;
194 if ((USBRecvFrame[0] == Constants.serialFrameHeader) && (USBRecvFrame[1] == Constants.serialFrameHeader) &&
195 (receivedByte > 0) && (receivedByte < Constants.serialFrameDataAndCommandLength))
197 USBRecvFrame[USBRecvFrameByteCount] = receivedByte;
198 USBRecvFrameByteCount++;
202 USBRecvFrameByteCount = 0;
236 USBRecvFrame[USBRecvFrameByteCount] = receivedByte;
237 USBRecvFrameByteCount++;
240 if (receivedByte == BuildCRC(USBRecvFrame))
242 USBRecvFrame[USBRecvFrameByteCount] = receivedByte;
243 USBRecvFrameByteCount++;
247 USBRecvFrameByteCount = 0;
252 if (receivedByte == Constants.serialFrameFooter)
254 USBRecvFrame[USBRecvFrameByteCount] = receivedByte;
255 USBRecvFrameByteCount++;
259 USBRecvFrameByteCount = 0;
264 if (receivedByte == Constants.serialFrameFooter)
266 USBRecvFrame[USBRecvFrameByteCount] = receivedByte;
267 USBRecvFrameByteCount = 0;
268 bFLAG_USBFrameReceived =
true;
272 USBRecvFrameByteCount = 0;