完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
嗨,使用34410A数字万用表,我正在使用R?
命令从仪器中获取值数组。 将格式类型设置为ASCII时,转换为C#double值当然很容易[double.Parse(string s)],但是,将仪器设置为“FORM:DATA REAL,64会以ViReal64格式给出值, 据说是一个8字节的浮点数。当从仪器中获取一个字节数组时,返回的格式为;#210xxxxxxxxxxxx [...],其中#告诉我的解析器期望一个确定长度的块,第一个数字给出 标题的长度,以下数字给出ViReal64值的实际计数。然后我尝试将前8个字节转换为值,使用以下代码片段:List result = new List(); for(int i = 0; i {result.Add(System.BitConverter.ToDouble(b,i));}此操作后结果不正确,我假设这与实现的差异有关。我知道IFormattedIO488。 ReadIEEEBlock()方法,但由于特定于实现的原因,我想尽量避免使用它。问题是:如何转换ViReal64 数字,作为字节[8]存储到C#double值中? 关于如何解决这个问题的任何提示都是值得欢迎的,因为使用REAL值与ASCII相比,传输时间明显更短.Regards,Daniel Rhodin 以上来自于谷歌翻译 以下为原文 Hi, Using the 34410A DMM, I'm using the R? command to fetch an array of values from the instrument. When setting the format type to ASCII, conversion to a C# double value is easy of course [double.Parse(string s)], however, setting the instrument to "FORM:DATA REAL,64 gives the values in a ViReal64 format, which is said to be a 8 byte floating number. When fetching a byte array from the instrument, the returned format will be; #210xxxxxxxxxxxx[...], where the # tells my parser to expect a definite length block, the first digit gives the length of the header, and the following digits gives the actual count of ViReal64 values. Then I'm trying to convert the first 8 bytes to a value, using the following code snippet: List result = new List(); for(int i = 0;i result.Add(System.BitConverter.ToDouble(b, i)); } The results are not correct after this operation, which I assume is related to a difference in implementation. I am aware of the IFormattedIO488.ReadIEEEBlock() method, but for implementation-specific reasons I would like to try to avoid using that. Question is; how do I convert a ViReal64 number, stored as a byte[8] into a C# double value? Any hints on how to solve this issue is welcome, as the transfer time is significantly lower using REAL values compared to ASCII. Regards, Daniel Rhodin |
|
相关推荐
3个回答
|
|
|
我的第一个猜测是你需要向仪器发送FORM:BORD SWAP命令。
我猜你现在正在使用错误的Endian。先给出一个镜头然后看看是否能解决你的问题 以上来自于谷歌翻译 以下为原文 My first guess is you need to send the instrument the FORM:BORD SWAP command. I'm guessing your using the wrong Endian at the moment. Give that a shot first and see if that fixes your problem |
|
|
|
|
|
如果您打算使用FormattedIO488.ReadIEEEBlock,则应将格式设置为FORM:BORD NORM。
如果您要自己解析它,请使用FORM:BORD NORM。如果您使用的是ReadIEEEBlock,则可以使用一行:double[] data =(double[])fmio.ReadIEEEBlock(IEEEBinaryType.BinaryType_R8,true,true); 如果你自己写的话,那就需要多做一些工作。 类似下面的工作://从34410Abyte[]读取double / real64数据; tempBytes = new byte[ 9&#93 ;; // read#和数据长度部分中的字符数(单个字符)tempBytes = fmio.IO.Read(2); int numSizeBytes = Int32.Parse (System.Text.ASCIIEncoding.ASCII.GetString(tempBytes,1,1)); //读取数据的长度bytestempBytes = fmio.IO.Read(numSizeBytes); int numDataBytes = Int32.Parse(System.Text.ASCIIEncoding .ASCII.GetString(tempBytes,0,numSizeBytes)); //读取字节和终止字符[] byteData = fmio.IO.Read(numDataBytes + 1); double[] data = new double[ numDataBytes / sizeof(double)&#93 ;; System.Buffer.BlockCopy(byteData,0,data,0,numDataBytes);我需要1行超过8.性能非常相似 ,但ReadIEEEBlock还有一些功能。 以上来自于谷歌翻译 以下为原文 If you are going to use FormattedIO488.ReadIEEEBlock then you should set the format to FORM:BORD NORM. If you are going to parse it yourself, use FORM:BORD NORM. If you are using ReadIEEEBlock, you can use one line: double[] data = (double[])fmio.ReadIEEEBlock(IEEEBinaryType.BinaryType_R8, true, true); If you are writing it yourself, it's going to take a little more work. Something like the following works:
I'd take 1 line over 8. Performance is very similar, but ReadIEEEBlock has a few more features. |
|
|
|
|
|
*把我的头撞在墙上*刚刚意识到我出错的地方......我设法通过在最后包含终止字符来首先抵消字节数组。
所以我原始帖子中的代码按预期工作,这是我的源数据偏移了1个字节。关于为什么我要避免ReadIEEEBlock()的几句话:因为我正在构建一个更通用的通信工具,我不喜欢 事先不知道发送或接收的数据类型(这是由用户在脚本文件中配置的)。 所以我所做的是始终读取我收到的所有字节,如果我发现一个前导'#',我会将数据视为字节数组而不是字符串。 据我所知,我无法查看一个字节来发现是否应该使用ReadIEEEBlock()或Read(),这会强制用户明确设置一个标志,表明预期的接收数据是在一个确定的长度块中。不管怎样,谢谢 为了所有的帮助!最诚挚的问候,Daniel Rhodin 以上来自于谷歌翻译 以下为原文 *banging my head against a wall* Just realized where I went wrong... I managed to offset the byte array in the first place by including the termination char in the end. So the code in my original post works as expected, it was my source data that was offset by 1 byte. A few words on why I want to avoid the ReadIEEEBlock(): As I'm building a more generic communication tool, I don't know beforehand what type of data that is sent or received (this is configured by the user in a script file). So what I do is to consistently read in all bytes that I receive, and if I discover a leading '#', I will treat the data as a byte array instead of a string. To my knowledge, I can't peek a byte to discover if I should use ReadIEEEBlock() or Read(), which would force the user to explicitly set a flag that the expected received data is in a definite length block. Anyway, thanks for all help! Best Regards, Daniel Rhodin |
|
|
|
|
只有小组成员才能发言,加入小组>>
1849 浏览 0 评论
2743 浏览 1 评论
2643 浏览 1 评论
2454 浏览 5 评论
3464 浏览 3 评论
1849浏览 0评论
423浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-4 05:50 , Processed in 0.643681 second(s), Total 78, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3839