private void StopButton_Click(object sender, EventArgs e) { byte[] cmdByte = new byte[10] { 0xAA, 1, 0, 0, 0, 0, 0, 0, 0, 0xBB }; CGloabal.WriteToCom(CGloabal.g_serialPorForUUT, cmdByte, 10); }
private void chipReset_Click(object sender, EventArgs e) { var chip = (byte)chipSelect.SelectedIndex; var path = (byte)pathSelect.SelectedIndex; Byte[] cmdByte = new Byte[10] { 0XAA, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0XBB }; cmdByte[2] = chip; cmdByte[3] = path; int error = CGloabal.WriteToCom(CGloabal.g_serialPorForUUT, cmdByte, 10); if (error < 0) { MessageBox.Show("芯片复位失败"); return; } }
private void g_serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { RModel.curDate = System.DateTime.Now; RModel.volVal1 = double.Parse(string.IsNullOrEmpty(vol1.Text)?"0": vol1.Text); RModel.eleVal1 = double.Parse(string.IsNullOrEmpty(ele1.Text) ? "0" : ele1.Text); RModel.volVal2 = double.Parse(string.IsNullOrEmpty(vol2.Text) ? "0" : vol2.Text); RModel.eleVal2 = double.Parse(string.IsNullOrEmpty(ele2.Text) ? "0" : ele2.Text); int length; byte[] ReceiveBytes = new byte[24]; CGloabal.g_bIsComRecvedDataFlag = true; //串口是否收到数据 length = CGloabal.g_serialPorForUUT.BytesToRead; byte[] Receivebuf = new byte[length]; CGloabal.ReadCom(CGloabal.g_serialPorForUUT, Receivebuf, length); //1、缓存数据 buffer.AddRange(Receivebuf); //2、完整性判断 while (buffer.Count >= 24) { if (buffer[0] == 0xAA) { //得到完整的数据,复制到ReceiveBytes中进行校验 buffer.CopyTo(0, ReceiveBytes, 0, 24); buffer.RemoveRange(0, 24); /////执行其他代码,对数据进行处理。 CGloabal.g_ComReadBuf = ReceiveBytes; int error = 0; int nRtnID = 0; error = CmdsAnalysis(CGloabal.g_ComReadBuf, ref nRtnID); } else //帧头不正确时,记得清除 { buffer.RemoveAt(0); } } }
/// <summary> /// 稳定性测试——执行测试 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTest_Click(object sender, EventArgs e) { CGloabal.g_serialPorForUUT.ReceivedBytesThreshold = CGloabal.nCOM_RECV_NUMS; CGloabal.g_serialPorForUUT.DataReceived += new SerialDataReceivedEventHandler(g_serialPort_DataReceived); //打开串口后开始接收数据 //变化电压3 Timer tVolChange = new Timer(); tVolChange.Interval = 300000; tVolChange.Tick += Fn_CircleVol; tVolChange.Enabled = true; //变化电压4 Timer tEleChange = new Timer(); tEleChange.Interval = 1200000; tEleChange.Tick += Fn_CircleEle; tEleChange.Enabled = true; byte[] cmdByte = new byte[10] { 0xAA, 1, 0, 0, 1, 0, 0, 0, 0, 0xBB }; CGloabal.WriteToCom(CGloabal.g_serialPorForUUT, cmdByte, 10); }