private void ComPortRecv_DataReceived(object sender, SerialDataReceivedEventArgs e) { //throw new NotImplementedException(); Thread.Sleep(100); try { byte[] byteRead = new byte[ComPortRecv.BytesToRead]; ComPortRecv.Read(byteRead, 0, byteRead.Length); //5a 54 01 02 03 04 05 06 07 08 00 5a fe 09 0a 0b 00 ff ff 00 0e 00 aa 5a fe int N = 53 + 11; byte[] SendToMac = new byte[N]; SendToMac[0] = 0x5a; SendToMac[1] = 0x54; for (int i = 2; i < 53 + 2; i++) { SendToMac[i] = 0x00; } SendToMac[2] = 0x81; SendToMac[N - 9] = 0x00; SendToMac[N - 8] = 0xff; SendToMac[N - 7] = 0xff; SendToMac[N - 6] = 0x00; SendToMac[N - 5] = 0x53; SendToMac[N - 4] = 0x00; SendToMac[N - 3] = 0xaa; SendToMac[N - 2] = 0x5a; SendToMac[N - 1] = 0xfe; if (byteRead.Length > 0) { if (byteRead[0] == 0xAB) { if (ExecDec) { DisLen -= DecKM; } for (int j = 0; j < 30; j++) { this.textBox11.BeginInvoke(new Action(() => { this.textBox11.AppendText((DisLen).ToString() + "m, "); })); //if (DisLen > 0) //{ // SendToMac[11] = (byte)(DisLen & 0xff); // SendToMac[12] = (byte)((DisLen >> 8) & 0xff); // SendToMac[13] = (byte)((DisLen >> 16) & 0xff); // SendToMac[14] = (byte)((DisLen >> 24) & 0xff); //} if (DisLen > 125 && DisLen <= 145) { SendToMac[11] = 0x1f; SendToMac[12] = 0xff; SendToMac[13] = 0xff; SendToMac[14] = 0xff; } else if (DisLen > 120 && DisLen <= 125) { SendToMac[11] = 0x0f; SendToMac[12] = 0xff; SendToMac[13] = 0xff; SendToMac[14] = 0xff; } else if (DisLen > 40 && DisLen <= 120) { SendToMac[11] = 0x00; SendToMac[12] = 0xff; SendToMac[13] = 0xff; SendToMac[14] = 0xff; } else if (DisLen > 30 && DisLen <= 40) { SendToMac[11] = 0x00; SendToMac[12] = 0x0f; SendToMac[13] = 0xff; SendToMac[14] = 0xff; } else if (DisLen > 15 && DisLen <= 30) { SendToMac[11] = 0x00; SendToMac[12] = 0x00; SendToMac[13] = 0xff; SendToMac[14] = 0xff; } else if (DisLen > 8.5 && DisLen <= 15) { SendToMac[11] = 0x00; SendToMac[12] = 0x00; SendToMac[13] = 0x0f; SendToMac[14] = 0xff; } else { SendToMac[11] = 0x00; SendToMac[12] = 0x00; SendToMac[13] = 0x00; SendToMac[14] = 0xff; } ComPortRecv.Write(SendToMac, 0, SendToMac.Count()); // Thread.Sleep(1); } } else { String temp = ""; for (int i = 0; i < byteRead.Length; i++) { temp += byteRead[i].ToString("x2"); } MyLog.Error("串口收到非0xAB数据!-----" + temp); } } } catch (Exception ex) { Trace.WriteLine(ex.Message + "--ComPortRecv_DataReceived"); } }
void DealWithLongFrame(ref byte[] TempBuf, ref int TempTag) { ThisCount = TempStoreBuf[2] * 256 + TempStoreBuf[3]; if (LastCount != 0 && ThisCount != 0 && (ThisCount - LastCount != 1)) { MyLog.Error("出现漏帧情况!!"); Trace.WriteLine("出现漏帧情况:" + LastCount.ToString("x4") + "--" + ThisCount.ToString("x4")); } LastCount = ThisCount; byte[] buf_LongFrame = new byte[4096]; Array.Copy(TempStoreBuf, 0, buf_LongFrame, 0, 4096); Array.Copy(TempStoreBuf, 4096, TempStoreBuf, 0, TempStoreBufTag - 4096); TempStoreBufTag -= 4096; RecvdMB += 4; Recv4KCounts += 1; if (buf_LongFrame[0] == 0xff && buf_LongFrame[1] == 0x01) { byte[] bufsav = new byte[4092]; Array.Copy(buf_LongFrame, 4, bufsav, 0, 4092); SaveFile.Lock_3.EnterWriteLock(); SaveFile.DataQueue_SC3.Enqueue(bufsav); SaveFile.Lock_3.ExitWriteLock(); } if (buf_LongFrame[0] == 0xff && buf_LongFrame[1] == 0x02) { byte[] bufsav = new byte[4092]; Array.Copy(buf_LongFrame, 4, bufsav, 0, 4092); SaveFile.Lock_4.EnterWriteLock(); SaveFile.DataQueue_SC4.Enqueue(bufsav); SaveFile.Lock_4.ExitWriteLock(); } if (buf_LongFrame[0] == 0xff && buf_LongFrame[1] == 0x03) { byte[] bufsav = new byte[4092]; Array.Copy(buf_LongFrame, 4, bufsav, 0, 4092); SaveFile.Lock_5.EnterWriteLock(); SaveFile.DataQueue_SC5.Enqueue(bufsav); SaveFile.Lock_5.ExitWriteLock(); } if (buf_LongFrame[0] == 0xff && buf_LongFrame[1] == 0x04) { byte[] bufsav = new byte[4092]; Array.Copy(buf_LongFrame, 4, bufsav, 0, 4092); SaveFile.Lock_6.EnterWriteLock(); SaveFile.DataQueue_SC6.Enqueue(bufsav); SaveFile.Lock_6.ExitWriteLock(); } if (buf_LongFrame[0] == 0xff && buf_LongFrame[1] == 0x08) { //FF08为短帧通道 byte[] bufsav = new byte[4092]; Array.Copy(buf_LongFrame, 4, bufsav, 0, 4092); SaveFile.Lock_2.EnterWriteLock(); SaveFile.DataQueue_SC2.Enqueue(bufsav); SaveFile.Lock_2.ExitWriteLock(); for (int i = 0; i < 6; i++) { if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x00) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_7.EnterWriteLock(); SaveFile.DataQueue_SC7.Enqueue(buf1D0x); SaveFile.Lock_7.ExitWriteLock(); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x01) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_8.EnterWriteLock(); SaveFile.DataQueue_SC8.Enqueue(buf1D0x); SaveFile.Lock_8.ExitWriteLock(); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x02) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_9.EnterWriteLock(); SaveFile.DataQueue_SC9.Enqueue(buf1D0x); SaveFile.Lock_9.ExitWriteLock(); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x03) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_10.EnterWriteLock(); SaveFile.DataQueue_SC10.Enqueue(buf1D0x); SaveFile.Lock_10.ExitWriteLock(); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x04) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_11.EnterWriteLock(); SaveFile.DataQueue_SC11.Enqueue(buf1D0x); SaveFile.Lock_11.ExitWriteLock(); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x05) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_12.EnterWriteLock(); SaveFile.DataQueue_SC12.Enqueue(buf1D0x); SaveFile.Lock_12.ExitWriteLock(); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x06) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_13.EnterWriteLock(); SaveFile.DataQueue_SC13.Enqueue(buf1D0x); SaveFile.Lock_13.ExitWriteLock(); lock (Data.ADList01) { for (int j = 0; j < num; j++) { Data.ADList01.Add(buf1D0x[j]); } } } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x07) { int num = bufsav[i * 682 + 2] * 256 + bufsav[i * 682 + 3];//有效位 byte[] buf1D0x = new byte[num]; Array.Copy(bufsav, i * 682 + 4, buf1D0x, 0, num); SaveFile.Lock_14.EnterWriteLock(); SaveFile.DataQueue_SC14.Enqueue(buf1D0x); SaveFile.Lock_14.ExitWriteLock(); lock (Data.ADList02) { for (int j = 0; j < num; j++) { Data.ADList02.Add(buf1D0x[j]); } } } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x0f) { //空闲帧 } else { Trace.WriteLine("FF08通道出错!"); } } } }
private void RecvAllUSB() { CyUSBDevice MyDevice01 = USB.MyDeviceList[Data.OnlyId]; startDT = DateTime.Now; DateTime midDT = startDT; RecvdMB = 0; TempStoreBufTag = 0; while (RecvTag) { if (MyDevice01.BulkInEndPt != null) { byte[] buf = new byte[4096]; int buflen = 4096; MyDevice01.BulkInEndPt.XferData(ref buf, ref buflen); if (buflen > 0) { Trace.WriteLine("收到数据包长度为:" + buflen.ToString()); Array.Copy(buf, 0, TempStoreBuf, TempStoreBufTag, buflen); TempStoreBufTag += buflen; byte[] Svbuf = new byte[buflen]; Array.Copy(buf, Svbuf, buflen); SaveFile.Lock_1.EnterWriteLock(); SaveFile.DataQueue_SC1.Enqueue(Svbuf); SaveFile.Lock_1.ExitWriteLock(); while (TempStoreBufTag >= 4096) { if (TempStoreBuf[0] == 0xff && (0x0 <= TempStoreBuf[1]) && (TempStoreBuf[1] < 0x11)) { DealWithLongFrame(ref TempStoreBuf, ref TempStoreBufTag); } else { MyLog.Error("收到异常帧!"); Trace.WriteLine("收到异常帧" + TempStoreBufTag.ToString()); Array.Clear(TempStoreBuf, 0, TempStoreBufTag); TempStoreBufTag = 0; } } } else if (buflen == 0) { // Trace.WriteLine("数传422机箱 收到0包-----0000000000"); } else { Trace.WriteLine("收到buflen <0"); } endDT = DateTime.Now; double tempTime = endDT.Subtract(midDT).TotalSeconds; if (tempTime > 2) { midDT = endDT; double tempMB = Recv4KCounts / 256; Recv4KCounts = 0; this.textBox4.BeginInvoke(new Action(() => { double speed = tempMB / tempTime; textBox4.Text = speed.ToString(); this.progressBar1.Value = (int)speed; })); } } } endDT = DateTime.Now; this.textBox1.BeginInvoke( new Action(() => { double costTime = endDT.Subtract(startDT).TotalSeconds; double RecvdM = RecvdMB / 1024; textBox1.Text = costTime.ToString(); textBox2.Text = RecvdM.ToString(); textBox3.Text = (RecvdM / costTime).ToString(); })); }
private void WriteToFileSC(int key, object file, ref Queue <byte[]> myQueue, ref ReaderWriterLockSlim myLock) { Trace.WriteLine("Start WriteToFileSC Thread:" + key.ToString()); FileStream myfile = (FileStream)file; BinaryWriter bw = new BinaryWriter(myfile); // FileInfo fileInfo; while (SaveOn) { if (myQueue.Count() > 0) { try { myLock.EnterReadLock(); bw.Write(myQueue.Dequeue()); bw.Flush(); myLock.ExitReadLock(); #region 分割文件,防止文件过大 long FileSizeMB = myfile.Length / (1024 * 1024 * 1024); if (FileSizeMB > 1) { myFileList_dat[key].Flush(); string Path2 = myFileList_dat[key].Name; int count = Path2.LastIndexOf("\\"); Path2 = Path2.Substring(0, count + 1); myFileList_dat[key].Close(); FileStream newFile; string timestr = string.Format("{0}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); string filename = Path2 + timestr + ".dat"; newFile = new FileStream(filename, FileMode.Create); myFileList_dat.Remove(myFileList_dat[key]); myFileList_dat.Insert(key, newFile); break; //break跳出循环会执行新线程 } #endregion } catch (Exception e) { bw.Close(); Trace.WriteLine(myQueue.Count()); MyLog.Error(e.Message); break; } } else { Thread.Sleep(200); // Trace.WriteLine("Queue0 is empty!!"); } } bw.Close(); Trace.WriteLine("Leaving WriteToFileSC:" + key.ToString()); if (SaveOn) { WriteToFileSC(key, myFileList_dat[key], ref myQueue, ref myLock); } }
private void button_translate68_Click(object sender, EventArgs e) { Button btn = (Button)sender; string input = textBox11.Text; string trans_buf = null; //是否判断定长? if (input.Length == 162 * 2) { byte[] buf_before_trans = StrToHexByte(input); for (int i = 0; i < buf_before_trans.Length; i++) { trans_buf += Convert.ToString(buf_before_trans[i], 2).PadLeft(8, '0'); } if (trans_buf.Length == 1296) { trans_buf.Substring(0, 1296); } byte[] buf_after_trans = new byte[217]; string first = trans_buf.Substring(0, 6); byte hexcrc = Convert.ToByte(first.PadLeft(8, '0'), 2); for (int i = 0; i < 216; i++) { string temp = trans_buf.Substring(i * 6, 6); buf_after_trans[i] = Convert.ToByte(temp.PadLeft(8, '0'), 2); if (i == 0) { hexcrc = Convert.ToByte(temp.PadLeft(8, '0'), 2); } else { byte b2 = Convert.ToByte(temp.PadLeft(8, '0'), 2); hexcrc ^= b2; } } buf_after_trans[216] = (byte)(hexcrc & 0x3f); string output = null; for (int i = 0; i < 217; i++) { output += buf_after_trans[i].ToString("x2"); } if (btn.Name == "button_translate68_1") { textBox_value_1d01.Text = output; } else if (btn.Name == "button_translate68_2") { textBox_value_1d02.Text = output; } else { } } else { MyLog.Error("数据长度不正确,不是162Bytes!"); } }
private void button13_Click(object sender, EventArgs e) { Button btn = (Button)sender; String Path = Program.GetStartupPath() + @"注入码本\"; if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); } openFileDialog1.InitialDirectory = Path; string tmpFilter = openFileDialog1.Filter; string title = openFileDialog1.Title; openFileDialog1.Title = "选择要注入的码表文件"; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*) | *.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) //selecting bitstream { MyLog.Info("选取" + openFileDialog1.FileName + "文件成功"); try { string temp = System.IO.File.ReadAllText(openFileDialog1.FileName); if (btn.Name == "button13") { textBox_value_1d03.Text = temp; } else if (btn.Name == "button14") { textBox_value_1d04.Text = temp; } else if (btn.Name == "button15") { textBox_value_1d01.Text = temp; } else if (btn.Name == "button16") { textBox_value_1d02.Text = temp; } else if (btn.Name == "button17") { textBox_value.Text = temp; } else if (btn.Name == "button4") { textBox7.Text = temp; } else if (btn.Name == "button18") { textBox11.Text = temp; } else if (btn.Name == "button19") { textBox12.Text = temp; } else if (btn.Name == "button21") { textBox13.Text = temp; } else { ; } } catch (Exception ex) { MyLog.Error(ex.ToString()); }; } }