private static void OnReadComplete(IVisaAsyncResult result) { try { string responseString = mbSession.RawIO.EndReadString(result); if (ReadComplete != null) { readArgs e = new readArgs(); e.text = InsertCommonEscapeSequences(responseString); ReadComplete(null, e); } } catch (Exception exp) { if (ReadComplete != null) { readArgs e = new readArgs(); e.text = InsertCommonEscapeSequences(scopeName + ": OnReadComplete failed. " + exp.Message); ReadComplete(null, e); } else { //throw new Exception(scopeName + ": OnReadComplete failed. " + exp.Message); } } }
// ============================================================================================================ // NOTE: The following function is only for if you need to receive data asynchronously from the vna without // first sending data to it. public void receiveAsyncDataFromVnaThruVisa(IVisaAsyncResult result) { try { // read the data string s = visaMessageBasedSession.RawIO.EndReadString(result); // remove carriage return, line feed, and null characters if they exist s = s.Replace("\n", "").Replace("\r", "").Replace("\0", ""); // was any data received? if (!string.IsNullOrEmpty(s)) { // yes... // log the data received from vna appendToTextBoxLog("<< " + s); } // begin next asynchronous read from vna visaAsyncResult = visaMessageBasedSession.RawIO.BeginRead(1024, visaAsyncCallback, null); } catch (Exception e) { // log the error appendToTextBoxLog("Error Receiving Data from the VNA: " + e.Message); } }
//异步写 private void OnWriteComplete(IVisaAsyncResult result) { try { mbSession.RawIO.EndWrite(result); } catch (Exception exp) { MessageBox.Show(exp.Message, "提示"); } }
internal static void read() { try { asyncHandle = mbSession.RawIO.BeginRead( 1024, new VisaAsyncCallback(OnReadComplete), null); } catch (Exception exp) { throw new Exception(scopeName + ": Read failed. " + exp.Message); } }
internal static void write(string command) { try { string textToWrite = ReplaceCommonEscapeSequences(command); asyncHandle = mbSession.RawIO.BeginWrite( textToWrite, new VisaAsyncCallback(OnWriteComplete), (object)textToWrite.Length); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
protected void WriteRawData(byte[] data) { lock (threadLock) { bool completed; IVisaAsyncResult result = mbSession.RawIO.BeginWrite(data, new VisaAsyncCallback(OnWriteComplete), (object)data.Length); // get the Async result object from the operation waitHandleIO = result.AsyncWaitHandle; // set the wait handle completed = waitHandleIO.WaitOne(30000); // wait until the write operation has completed or timed out if (!completed) // check to see that the operation completed without timing out. { throw new TimeoutException("Timeout in WriteRawData"); // if it did time out, throw a timeout exception } mbSession.RawIO.EndWrite(result); // end the write, freeing up system resources. } }
private static void OnWriteComplete(IVisaAsyncResult result) { try { mbSession.RawIO.EndWrite(result); if (WriteComplete != null) { WriteComplete(null, default(EventArgs)); } } catch (Exception exp) { // throw new Exception(scopeName + ": OnWriteComplete failed. " + exp.Message); } }
//异步读 private void OnReadComplete(IVisaAsyncResult result) { try { string responseString = mbSession.RawIO.EndReadString(result); String[] resdata = responseString.Split(';'); if (resdata.Length >= 2) { double data1 = Convert.ToDouble(resdata[0]) / 10; double data2 = Convert.ToDouble(resdata[1]) / 10; var date = DateTime.Now; _myPlotModel.Axes[0].Maximum = DateTimeAxis.ToDouble(date.AddSeconds(1)); var lineSer = plotView1.Model.Series[0] as LineSeries; lineSer.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), data1)); if (!checkBox1.Checked) { if (lineSer.Points.Count > 250) { lineSer.Points.RemoveAt(0); } } lineSer = plotView1.Model.Series[1] as LineSeries; lineSer.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), data2)); if (!checkBox1.Checked) { if (lineSer.Points.Count > 250) { lineSer.Points.RemoveAt(0); } } _myPlotModel.InvalidatePlot(true); } } catch (Exception exp) { MessageBox.Show(exp.Message, "提示"); } }
private void OnWriteComplete(IVisaAsyncResult result) // for callbacks on data writing { manualResetEventIO.Set(); // set the IO manual reset event. }