C# NI-VISA MessageBasedSession方法发送命令字符串,后跟二进制数据
我正在尝试使用c#Net中的NI-VISA将数据发送到数字存储示波器。数据首先通过以下方式从DSO接收:C# NI-VISA MessageBasedSession方法发送命令字符串,后跟二进制数据,c#,visa,C#,Visa,我正在尝试使用c#Net中的NI-VISA将数据发送到数字存储示波器。数据首先通过以下方式从DSO接收: MessageBasedSession mbSession; MessageBasedSessionWriter writer = new MessageBasedSessionWriter(mbSession); MessageBasedSessionReader reader = new MessageBasedSe
MessageBasedSession mbSession;
MessageBasedSessionWriter writer = new MessageBasedSessionWriter(mbSession);
MessageBasedSessionReader reader = new MessageBasedSessionReader(mbSession);
reader.BinaryEncoding = BinaryEncoding.RawLittleEndian;
writer.BinaryEncoding = BinaryEncoding.RawLittleEndian;
string loadCommand = ":SYSTem:SETup?";
mbSession.WriteLine(loadCommand);//edited from .Write() to .WriteLine
byte[] setupData = reader.ReadBytes(2826);
到目前为止还不错,setupData中的字节是正确的。
问题是将数据发送回DSO。我能看到的唯一方法是使用两种写入方法:
loadCommand = ":SYSTem:SETup ";
mbSession.Write(loadCommand);
writer.WriteBinary( setupData);
问题是loadCommand和setupData似乎必须连续发送,至少文档中是这样的:
此外,在试验和出错失败期间,我唯一能从DSO获得响应的时间是当它在DSO屏幕上打印“文件未识别”时。这是我尝试的时候:
mbSession.Write(loadCommand + strMBRSess);//get 'failed to read file' from DSO
其中STRMBRESS是转换为ASCII字符串的setupData。尝试使用两个写入命令发送相同的数据时,DSO没有响应:
mbSession.Write(loadCommand);
mbSession.Write(strMBRSess);
我不确定MessageBasedSession是否可以做到这一点,我很高兴:
1-当然是一个解决方案。
2-明确知道这是不可能的。
我在NI-VISA论坛上发了一篇帖子,但没有得到任何回复
汤姆回答之后,事情就开始了
writer.WriteBinary(Encoding.ASCII.GetBytes(":SYSTem:SETup "));
writer.WriteBinary(setupData);//fails-no response
writer.Write(":SYSTem:SETup ");
writer.WriteBinary(setupData);//fails-no response
writer.WriteLine(":SYSTem:SETup ");
writer.WriteBinary(setupData);//fails-no response
byte[] loadCommandBytes = Encoding.ASCII.GetBytes(":SYSTem:SETup ");//or....
byte[] loadCommandBytes = Encoding.ASCII.GetBytes(":SYSTem:SETup " + Char.MinValue);
byte[] CombinedCommandData = new byte[loadCommandBytes.Length + setupData.Length];
Buffer.BlockCopy(loadCommandBytes, 0, CombinedCommandData, 0, loadCommandBytes.Length);
Buffer.BlockCopy(setupData, 0, CombinedCommandData, loadCommandBytes.Length, setupData.Length);
writer.WriteBinary(CombinedCommandData);//fails-no response
在同一方法中传递命令和数据不会编译:
writer.WriteBinary(Encoding.ASCII.GetBytes(":SYSTem:SETup ") + setupData); //operator + cannot be applied to operands of type 'byte[]' and 'byte[]'
已经找到一个帖子,显示这是有效的。不幸的是,它不是.Net:我怀疑
mbSession.Write(loadCommand)
正在发送一个行终止符,您不应该这样做。VisaNS文档没有说明,但根据我对SCPI的理解,您的查询必须终止,您已经说过这是可行的,因此我得出结论,发送了一个行终止符
提示:如果仪器的前面板上有错误信息,请密切关注它。有时,它们比通过编程检索的错误消息更好、更及时,并且在调试期间更容易看到
无论如何,这种方法应该是有效的:
writer.WriteBinary(Encoding.ASCII.GetBytes(":SYSTem:SETup "));
writer.WriteBinary(setupData);
而且,这可能:
writer.Write(":SYSTem:SETup ");
writer.WriteBinary(setupData);
我认为如果您创建了一个MessageBasedSessionWriter,那么应该使用诸如WriteBinary、Write和WriteLine之类的方法。MessageBasedSession中的方法似乎是为了方便您在不需要MessageBasedSessionWriter编写二进制文件时使用。谢谢,我为原始帖子中的错误道歉(.write()应该是.WriteLine());正是WriteLine添加了终止符。我已经在帖子中添加了代码,不幸的是,似乎没有任何效果,甚至没有从DSO(RigolDS2072A)那里得到响应。工作的Linux代码(见附件)和我从DSO获得响应时,字符串和数据都以相同的写入方法传递。如果是这样,那么Rigol并没有很好地实现这一点。