//向USB发送数据,数据区不加0 public static void SendData(int id, byte[] temp) { int TempLength = temp.Length; if (MyDeviceList[id] != null) { lock (MyDeviceList[id]) { Register.Byte80H = (byte)(Register.Byte80H | 0x02); SendCMD(id, 0x80, Register.Byte80H); Register.Byte80H = (byte)(Register.Byte80H & 0xFD); SendCMD(id, 0x80, Register.Byte80H); if (MyDeviceList[id].BulkOutEndPt != null) { bool tag = MyDeviceList[id].BulkOutEndPt.XferData(ref temp, ref TempLength); if (!tag) { MyLog.Error("传输数据到USB板卡失败"); } else { MyLog.Info("传输成功:" + TempLength.ToString()); } } } } else { MyLog.Error("USB设备未连接!"); } }
public static void SendCMD(int id, byte ReqCode, byte Value) { if (MyDeviceList[id] != null) { CyControlEndPoint CtrlEndPt = null; CtrlEndPt = MyDeviceList[id].ControlEndPt; if (CtrlEndPt != null) { lock (MyDeviceList[id]) { CtrlEndPt.Target = CyConst.TGT_DEVICE; CtrlEndPt.ReqType = CyConst.REQ_VENDOR; CtrlEndPt.Direction = CyConst.DIR_TO_DEVICE; CtrlEndPt.Index = 0; CtrlEndPt.ReqCode = ReqCode; CtrlEndPt.Value = (ushort)Value; int len = 8; byte[] buf = new byte[8]; CtrlEndPt.XferData(ref buf, ref len); MyLog.Info("向USB机箱" + id.ToString() + "发送指令0x" + ReqCode.ToString("x2") + " 0x" + Value.ToString("x2") + "成功"); } } } else { MyLog.Error("USB设备未连接!"); } }
public void FileCreateDat(string Path, out FileStream file) { if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); } DirectoryInfo folder = new DirectoryInfo(Path); try { foreach (FileInfo tempfile in folder.GetFiles("*.*")) { string name = tempfile.Name; if (tempfile.Length == 0) { Trace.WriteLine("删除文件" + tempfile.FullName); File.Delete(tempfile.FullName); } } } catch (Exception ex) { MyLog.Error(ex.Message); } 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 = Path + timestr + ".dat"; file = new FileStream(filename, FileMode.Create); myFileList_dat.Add(file); }
//向USB发送数据,数据区加3个0 public static void SendDataByInt(int id, byte[] temp) { byte[] SendBytes = new byte[1024 * 16]; int temp_lenth = temp.Length; int SendBytes_lenth = 4 * (temp_lenth - 4) + 4; for (int j = 0; j < 4; j++) { SendBytes[j] = temp[j]; } for (int i = 4; i < SendBytes_lenth; i++) { if (i % 4 == 0) { SendBytes[i] = temp[(i / 4) + 3]; } if (i % 4 == 1) { SendBytes[i] = 0x0; } if (i % 4 == 2) { SendBytes[i] = 0x0; } if (i % 4 == 3) { SendBytes[i] = 0x0; } } if (MyDeviceList[id] != null) { lock (MyDeviceList[id]) { Register.Byte80H = (byte)(Register.Byte80H | 0x02); SendCMD(id, 0x80, Register.Byte80H); Register.Byte80H = (byte)(Register.Byte80H & 0xFD); SendCMD(id, 0x80, Register.Byte80H); if (MyDeviceList[id].BulkOutEndPt != null) { bool tag = MyDeviceList[id].BulkOutEndPt.XferData(ref SendBytes, ref SendBytes_lenth); if (tag) { MyLog.Info("传输数据到USB板卡成功"); } else { MyLog.Error("传输数据到USB板卡失败"); } } } } else { MyLog.Error("USB设备未连接!"); } }
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.开始ToolStripMenuItem.Text == "开始") { this.开始ToolStripMenuItem.Text = "停止"; if (USB.MyDeviceList[Data.OnlyId] != null) { CyControlEndPoint CtrlEndPt = null; CtrlEndPt = USB.MyDeviceList[Data.OnlyId].ControlEndPt; if (CtrlEndPt != null) { USB.SendCMD(Data.OnlyId, 0x80, 0x01); USB.SendCMD(Data.OnlyId, 0x80, 0x00); USB.MyDeviceList[Data.OnlyId].Reset(); Register.Byte80H = (byte)(Register.Byte80H | 0x04); USB.SendCMD(Data.OnlyId, 0x80, Register.Byte80H); } FileThread = new SaveFile(); FileThread.FileInit(); FileThread.FileSaveStart(); MyLog.Info("开始读取"); RecvTag = true; ThisCount = 0; LastCount = 0; new Thread(() => { RecvAllUSB(); }).Start(); new Thread(() => { DealWithADFun(); }).Start(); new Thread(() => { DealWithSERFun(); }).Start(); } else { MyLog.Error("设备未连接,请检查接线,打开电源!"); } } else { this.开始ToolStripMenuItem.Text = "开始"; ThisCount = 0; LastCount = 0; RecvTag = false; Thread.Sleep(500); if (FileThread != null) { FileThread.FileClose(); } } }
void UsbDevices_DeviceRemoved(object sender, EventArgs e) { USBEventArgs evt = (USBEventArgs)e; USBDevice RemovedDevice = evt.Device; string RemovedDeviceName = evt.FriendlyName; MyLog.Error(RemovedDeviceName + "板卡断开"); int key = int.Parse(evt.ProductID.ToString("x4").Substring(0, 2), System.Globalization.NumberStyles.HexNumber); USB.MyDeviceList[key] = null; }
//----------设置系统时间---------- public static void Set_Time(byte[] data) { try { byte[] gettime = new byte[23]; for (int datai = 0; datai < data.Length; datai++) { gettime[datai] = byte.Parse(data[datai].ToString()); } string dt_str = Encoding.ASCII.GetString(gettime); string str = dt_str.Substring(0, 4); SystemTime systime = new SystemTime(); systime.vYear = ushort.Parse(str); str = dt_str.Substring(5, 2); systime.vMonth = ushort.Parse(str); str = dt_str.Substring(8, 2); systime.vDay = ushort.Parse(str); str = dt_str.Substring(11, 2); systime.vHour = ushort.Parse(str); str = dt_str.Substring(14, 2); systime.vMinute = ushort.Parse(str); str = dt_str.Substring(17, 2); systime.vSecond = ushort.Parse(str); str = dt_str.Substring(20, 3); systime.vMilliseconds = ushort.Parse(str); //--------更改系统时间-------- SetLocalTime(ref systime); } catch { MyLog.Error("设置系统时间失败!"); } }
private void button1_Click(object sender, EventArgs e) { Button btn = (Button)sender; string name = btn.Name; int t = int.Parse(name.Substring(6));//t范围(1,16) openFileDialog1.InitialDirectory = Program.GetStartupPath() + @"单元测试仪数据\AD\" + btn.Text + @"\"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { openFileDialog1.InitialDirectory = Program.GetStartupPath(); try { myTxtGp[t - 1].Text = openFileDialog1.FileName; byte[] bufret = System.IO.File.ReadAllBytes(openFileDialog1.FileName); int num = bufret.Count(); double yMax = 0; double yMin = 0; double[] x = new double[num / 2]; double[] y = new double[num / 2]; for (int i = 0; i < num / 2; i++) { x[i] = i; int temp = bufret[2 * i] * 256 + bufret[2 * i + 1]; if ((bufret[2 * i] & 0x80) == 0x80) { temp = 0x8000 - temp; } double value = temp; value = 10 * (value / 32767); if ((bufret[2 * i] & 0x80) == 0x80) { y[i] = -value; } else { y[i] = value; } if (i == 0) { yMax = y[i]; yMin = y[i]; } else { if (y[i] > yMax) { yMax = y[i]; } if (y[i] < yMin) { yMin = y[i]; } } } //加速度以及保留在zed2,其他在zed1 if ((t >= 2 && t <= 8) || t == 16) {//zed2 Color color = GetColor(t); LineItem myCurve = MyPane2.AddCurve(btn.Text, x, y, color, SymbolType.Square); YAxis yAxis = new YAxis(btn.Text); yAxis.Scale.Max = yMax; yAxis.Scale.Min = yMin; MyPane2.YAxisList.Add(yAxis); zedGraphControl2.AxisChange(); zedGraphControl2.Invalidate(); } else {//zed1 Color color = GetColor(t); LineItem myCurve = MyPane.AddCurve(btn.Text, x, y, color, SymbolType.Square); YAxis yAxis = new YAxis(btn.Text); yAxis.Scale.Max = yMax; yAxis.Scale.Min = yMin; MyPane.YAxisList.Add(yAxis); zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate(); } } catch (Exception ex) { MyLog.Error(ex.ToString()); } } }
private void DealWithSERFun() { while (RecvTag) { bool Tag1 = false; bool Tag2 = false; bool Tag3 = false; bool Tag4 = false; lock (Data.SERList01) { if (Data.SERList01.Count >= 11) { byte[] ret = Data.SERList01.Skip(5).Take(4).ToArray(); int weight = ret[0] * 256 + ret[1]; if (Data.SERList01[0] == 0x04 && Data.SERList01[1] == 0x00 && Data.SERList01[2] == 0x01) { Data.SER1_speed_value = weight; } else { MyLog.Error("SERList01 串口数据偏移了!"); } Data.SERList01.RemoveRange(0, 11); Tag1 = true; } else { Tag1 = false; } } lock (Data.SERList02) { if (Data.SERList02.Count >= 9) { byte[] ret = Data.SERList02.Skip(3).Take(4).ToArray(); float temp = BitConverter.ToSingle(ret, 0); if (Data.SERList02[1] == 0x31) { Data.SER2_speed_value = temp; } else if (Data.SERList02[1] == 0x30) { Data.SER2_niuju_value = temp; } else { MyLog.Error("SERList02 串口数据偏移了!"); } Data.SERList02.RemoveRange(0, 9); Tag2 = true; } else { Tag2 = false; } } lock (Data.SERList03) { if (Data.SERList03.Count >= 9) { byte[] ret = Data.SERList03.Skip(3).Take(4).ToArray(); float temp = BitConverter.ToSingle(ret, 0); if (Data.SERList03[1] == 0x31) { Data.SER3_speed_value = temp; } else if (Data.SERList03[1] == 0x30) { Data.SER3_niuju_value = temp; } else { MyLog.Error("SERList03 串口数据偏移了!"); } Data.SERList03.RemoveRange(0, 9); Tag3 = true; } else { Tag3 = false; } } lock (Data.SERList04) { if (Data.SERList04.Count >= 100) { byte ret = Data.SERList04[0]; Data.SERList04.RemoveRange(0, 100); Tag4 = true; } else { Tag4 = false; } } if (Tag1 == false && Tag2 == false && Tag3 == false && Tag4 == false) { Thread.Sleep(100); } } }
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] == 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_3.EnterWriteLock(); //SaveFile.DataQueue_SC3.Enqueue(buf1D0x); //SaveFile.Lock_3.ExitWriteLock(); // string temp = null; // for (int j = 0; j < num; j++) temp += buf1D0x[j].ToString("x2"); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x01) //对应第1个圆盘 { 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_4.EnterWriteLock(); //SaveFile.DataQueue_SC4.Enqueue(buf1D0x); //SaveFile.Lock_4.ExitWriteLock(); lock (Data.SERList01) { Data.SERList01.AddRange(buf1D0x); } } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x02) //对应第2个圆盘 { 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_5.EnterWriteLock(); //SaveFile.DataQueue_SC5.Enqueue(buf1D0x); //SaveFile.Lock_5.ExitWriteLock(); lock (Data.SERList02) { Data.SERList02.AddRange(buf1D0x); } } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x03) //对应第3个圆盘 { 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_6.EnterWriteLock(); //SaveFile.DataQueue_SC6.Enqueue(buf1D0x); //SaveFile.Lock_6.ExitWriteLock(); lock (Data.SERList03) { Data.SERList03.AddRange(buf1D0x); } } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x0C) //报警参数 { 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_6.EnterWriteLock(); //SaveFile.DataQueue_SC6.Enqueue(buf1D0x); //SaveFile.Lock_6.ExitWriteLock(); lock (Data.SERList04) { Data.SERList04.AddRange(buf1D0x); } //myMonitor.textBox_speed.BeginInvoke(new Action(() => //{ // double speed = tempMB / tempTime; // myMonitor.textBox_speed.Text = speed.ToString(); // myMonitor.progressBar1.Value = (int)speed; //})); } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x0D) { 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(); lock (Data.ADList01) { Data.ADList01.AddRange(buf1D0x); //for (int j = 0; j < num; j++) // Data.ADList01.Add(buf1D0x[j]); } } else if (bufsav[i * 682 + 0] == 0x1D && bufsav[i * 682 + 1] == 0x0E) { 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(); lock (Data.ADList02) { Data.ADList02.AddRange(buf1D0x); //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; lock (MyDevice01) MyDevice01.BulkInEndPt.XferData(ref buf, ref buflen); if (buflen > 0) { Trace.WriteLine("收到数据包长度为:" + buflen.ToString()); // lock (TempStoreBuf) 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()); // lock(TempStoreBuf) 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; //myMonitor.textBox_speed.BeginInvoke(new Action(() => //{ // double speed = tempMB / tempTime; // myMonitor.textBox_speed.Text = speed.ToString(); // myMonitor.progressBar1.Value = (int)speed; //})); } } } endDT = DateTime.Now; //myMonitor.textBox_time.BeginInvoke( // new Action(() => // { // double costTime = endDT.Subtract(startDT).TotalSeconds; // double RecvdM = RecvdMB / 1024; // myMonitor.textBox_time.Text = costTime.ToString(); // myMonitor.textBox_recvsize.Text = RecvdM.ToString(); // myMonitor.textBox_avspeed.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); } }