Quantcast
Channel: Instrument Control (GPIB, Serial, VISA, IVI) topics
Viewing all articles
Browse latest Browse all 5566

Bus error occurred during transfer. @sample code "SimpleReadWrite"

$
0
0

Dear All,

 

I using "NI GPIB-USB-HS" and "VisualStudio2019 C#" control instrument(IDRC).

 

Last week,

I used "NI-VISA IC" and sample code"SimpleReadWrite" (provided by NI)

to test all equipment, everything works very well.

 

 

But today,

I made some small changes on sample code.

I only added new "WindowsForm(Name: Output)"

And I wrote open method in "clear_Click button"(only added Green Cube)

(I didn't change other "MainForm"&"SelectResources" code)

ChuChuNice_1-1637248838474.png

未命名.png

 

 

 

 

 

 

 

After I press the "write button" at MainForm,

program will show error message "Bus error occurred during transfer"

 

Please tell me what happened and how to solve.

Sorry for my bad English and bad programing ability.

 

**NI-VISA VI still work well now.

 

The following is my code:

 

using NationalInstruments.Visa;
using System;
using System.IO;
using System.Windows.Forms;

namespace NationalInstruments.Examples.SimpleReadWrite
{
public partial class Output : Form
{
private MessageBasedSession mbSession;
private string lastResourceString = null;
private bool SamplingBool = false;

private void SamplingProgess()
{
WriteAndRead1();
}


public Output()
{
InitializeComponent();
}

private void Output_Load(object sender, EventArgs e)
{

}
private string WriteAndRead()
{

try
{
string WRtmp = ReplaceCommonEscapeSequences(Command1.Text);
mbSession.RawIO.Write(WRtmp);
return InsertCommonEscapeSequences(mbSession.RawIO.ReadString());
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
Cursor.Current = Cursors.Default;
}
return "XXX";
}
private void WriteAndRead1()
{
try
{
string WRtmp = ReplaceCommonEscapeSequences(Command1.Text);
mbSession.RawIO.Write(WRtmp);
ReturnValue1.Text = InsertCommonEscapeSequences(mbSession.RawIO.ReadString());
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
Cursor.Current = Cursors.Default;
}
}


private string InsertCommonEscapeSequences(string s)
{
return s.Replace("\n", "\\n").Replace("\r", "\\r");
}

private string ReplaceCommonEscapeSequences(string s)
{
return s.Replace("\\n", "\n").Replace("\\r", "\r");
}

private void WRbutton_Click(object sender, EventArgs e)
{
ReturnValue1.Text = String.Empty;
WriteAndRead1();
}

private void Link_Click(object sender, EventArgs e)
{


using (SelectResource sr = new SelectResource())
{
if (lastResourceString != null)
{
sr.ResourceName = lastResourceString;
}
DialogResult result = sr.ShowDialog(this);
if (result == DialogResult.OK)
{
lastResourceString = sr.ResourceName;
Cursor.Current = Cursors.WaitCursor;
using (var rmSession = new ResourceManager())
{
try
{
mbSession = (MessageBasedSession)rmSession.Open(sr.ResourceName);
}
catch (InvalidCastException)
{
MessageBox.Show("Resource selected must be a message-based session");
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
Cursor.Current = Cursors.Default;
}
}
}
}

 

}

private void ReturnValue1_TextChanged(object sender, EventArgs e)
{

}

private void SamplingButton_Click(object sender, EventArgs e)
{
SamplingBool = true;
while (SamplingBool == true)
{
String file_name = @"C:\Users\USER\Downloads\" + filename.Text;
StreamWriter str_writ = new StreamWriter(file_name, true);

str_writ.WriteLine(WriteAndRead());
str_writ.Close();

Application.DoEvents();
}
}

private void SamplingStop_Click(object sender, EventArgs e)
{
SamplingBool = false;
}
}
}


Viewing all articles
Browse latest Browse all 5566

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>