//向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设备未连接!"); } }
private void button1_Click(object sender, EventArgs e) { if (this.button1.Text == "开始读取") { this.textBox1.Text = null; this.textBox2.Text = null; this.textBox3.Text = null; this.button1.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); this.btn_80_2.Text = "1:接收"; } FileThread = new SaveFile(); FileThread.FileInit(); FileThread.FileSaveStart(); MyLog.Info("开始读取"); RecvTag = true; ThisCount = 0; LastCount = 0; new Thread(() => { RecvAllUSB(); }).Start(); new Thread(() => { DealWithADFun(); }).Start(); } else { MyLog.Error("单元测试仪未连接!"); } } else { this.button1.Text = "开始读取"; ThisCount = 0; LastCount = 0; RecvTag = false; Thread.Sleep(500); if (FileThread != null) { FileThread.FileClose(); } } }
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设备未连接!"); } }
private void btn_SerialOpen_2_Click(object sender, EventArgs e) { try { ComPortRecv = new SerialPort(); ComPortRecv.BaudRate = Convert.ToInt32(comboBox_SerialBaudrate_2.Text); ComPortRecv.PortName = comboBox_SerialPortNum_2.Text; ComPortRecv.DataBits = Convert.ToInt32(comboBox_SerialDatabit_2.Text); switch (comboBox_SerialStopbit_2.Text) { case "1": ComPortRecv.StopBits = StopBits.One; break; case "1.5": ComPortRecv.StopBits = StopBits.OnePointFive; break; case "2": ComPortRecv.StopBits = StopBits.Two; break; default: MessageBox.Show("Error:停止位参数设置不正确", "Error"); break; } switch (comboBox_SerialParity_2.Text) { case "无校验": ComPortRecv.Parity = Parity.None; break; case "偶校验": ComPortRecv.Parity = Parity.Even; break; case "奇校验": ComPortRecv.Parity = Parity.Odd; break; default: MessageBox.Show("Error:校验位参数设置不正确", "Error"); break; } ComPortRecv.ReadTimeout = 1000; //ComPortRecv.ReceivedBytesThreshold = 1; ComPortRecv.Open(); MyLog.Info("接收串口打开成功"); //事件注册 ComPortRecv.DataReceived += ComPortRecv_DataReceived; } catch (Exception ex) { Trace.WriteLine(ex.Message); } }
private void comboBox_SerialPortNum_2_DropDown(object sender, EventArgs e) { string[] str = SerialPort.GetPortNames(); if (str == null) { MyLog.Info("尝试选择串口,但是本机没有串口!"); } comboBox_SerialPortNum_2.Items.AddRange(str); int count = comboBox_SerialPortNum_2.Items.Count; for (int i = 0; i < count; i++) { string str1 = comboBox_SerialPortNum_2.Items[i].ToString(); for (int j = i + 1; j < count; j++) { string str2 = comboBox_SerialPortNum_2.Items[j].ToString(); if (str1 == str2) { comboBox_SerialPortNum_2.Items.RemoveAt(j); count--; j--; } } } }
private void initRBoard() { MyLog.Info("初始化数字电阻通道......"); //初始化板卡,每个通道值设置为0x00001C03向下发送 byte[] RSend = new byte[8 + 32]; RSend[0] = 0x1D; RSend[1] = 0x0D; RSend[2] = 0x00; RSend[3] = 0x20;//0x0020 = 32 // for (int i = 0; i < 32; i++) RSend[164 + i] = 0x0; for (int i = 1; i < 8; i++) { RSend[4 * i] = 0x0; RSend[4 * i + 1] = 0x0; //RSend[4 * i + 2] = 0x1c; //RSend[4 * i + 3] = 0x03; RSend[4 * i + 2] = 0x03; RSend[4 * i + 3] = 0x1c; } RSend[36] = 0xC0; RSend[37] = 0xDE; RSend[38] = 0xC0; RSend[39] = 0xDE; Register.Byte84H = (byte)(Register.Byte84H | 0x40); USB.SendCMD(Data.OnlyId, 0x84, Register.Byte84H); Register.Byte84H = (byte)(Register.Byte84H & 0x3f); USB.SendCMD(Data.OnlyId, 0x84, 0x00); USB.SendData(Data.OnlyId, RSend); }
//向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 button32_Click(object sender, EventArgs e) { ComPortRecv.Close(); Thread.Sleep(1000); // ComPortRecv.DataReceived -= ComPortRecv_DataReceived; Thread.Sleep(1000); MyLog.Info("接收串口关闭成功"); }
/*Summary * Search the device with VID-PID 04b4-00F1 and if found, select the end point */ private void SetDevice(bool bPreserveSelectedDevice) { int nDeviceList = USB.usbDevices.Count; for (int nCount = 0; nCount < nDeviceList; nCount++) { USBDevice fxDevice = USB.usbDevices[nCount]; String strmsg; strmsg = "(0x" + fxDevice.VendorID.ToString("X4") + " - 0x" + fxDevice.ProductID.ToString("X4") + ") " + fxDevice.FriendlyName; int key = int.Parse(fxDevice.ProductID.ToString("x4").Substring(0, 2), System.Globalization.NumberStyles.HexNumber); if (USB.MyDeviceList[key] == null) { USB.MyDeviceList[key] = (CyUSBDevice)fxDevice; MyLog.Info(USB.MyDeviceList[key].FriendlyName + ConfigurationManager.AppSettings[USB.MyDeviceList[key].FriendlyName] + "连接"); Data.OnlyId = key; USB.SendCMD(Data.OnlyId, 0x81, 0x7f); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x81, 0x00); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x82, 0x7f); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x82, 0x00); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x83, 0x7f); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x83, 0x00); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x84, 0x7f); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x84, 0x00); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x85, 0x7f); Thread.Sleep(100); USB.SendCMD(Data.OnlyId, 0x85, 0x00); Thread.Sleep(100); } } }
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()); }; } }