如何组合两个字节来获取c#中的原始值 [英] How to combine two bytes to get the original value in c#

查看:48
本文介绍了如何组合两个字节来获取c#中的原始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我需要你的帮助和支持,如何组合两个字节(每个字节是8位宽),以获得原始值。我正在GUI上显示从我的微控制器发送的值(从0到500)。我可以成功显示0到255之间的值,因为这只需要发送一个字节。但是,从256到500发送值需要发送两个字节。我遇到的问题是我无法重新组合收到的两个字节以获得原始值。以下是我的代码行:

Good day to all,
Please I need your help and support on how to combine two bytes (each byte is 8 bits wide) in order to get the original value. I am displaying values(from 0 to 500)sent from my microcontroller on the GUI.I can successfully display values from 0 to 255 as this requires just sending a byte. However sending values from 256 to 500 requires sending two bytes. The problem I am having is that I was unable to re-combine the received two bytes in order to get the original value. Below are my lines of code:

int  main( void ) // this is the main function inside the microcontroller  

02 {  

03     sei();  

04     USI_TWI_Master_Initialise();  

05     Spi_Master_Init();  

06        

07        

08     while(1)  // I am using this loop to send 500(111110100)  

09     {  

10                  

11            

12         Transmitt_Receive(244);//this function sends LOW BYTE of 500(11110100)  

13    

14         _delay_ms(1000);  

15    

16         Transmitt_Receive(1); // this function sends HIGH BYTE of 500(00000001)  

17         _delay_ms(1000);  

18            

19            

20            

21     }  

22 } 










private   void rxtemp_CheckedChanged(object sender, EventArgs e)  

02        {  

03            UInt32 numBytesRead = 0;  

04            UInt32 numBytesToRead =1 ;  

05            Int32 value;  

06    

07            byte[] readData = new byte[5];  

08            byte[] val      = new byte[5];  

09              

10            Thread th = new Thread(() =>  

11                {  

12                    while (true)  

13                    {  

14                        ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);  

15                        val[0]   = readData[0];  

16                        ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);  

17                        val[1]   = readData[0];  

18                   

19                        value = (val[0] << 8) | val[1];   

20    

21                            label3.Invoke(new Action(() =>  

22                            {  

23                                label3.Text = value + "ºC";  

24    

25    

26                            }));  

27    

28    

29                             

30                        

31                    }  

32                       

33                        

34                });  

35            th.IsBackground = true;  

36            th.Start();   

37        } 


When I combined the two bytes using the above arrangements, instead of getting 500 my GUI displayed 62708.I got the same result when I used BitConverter.
<pre lang="c#"> 
       value3 = BitConverter.ToInt32(val,0); 





如果有人能告诉我如何解决这个错误,我会很高兴。谢谢你的支持。最好的问候。



I would be very glad if somebody could put me through on how to resolve this error. Thank you for the support. Best regards.

推荐答案

两个字节是16位。请尝试 BitConverter.ToUInt16 。此外,一次读取两个字节而不是一个。网络字节是big-endian,你的系统是little-endian。您现在将它们作为小端处理,混合字节顺序,导致错误的值。



一些关于这些endians的额外信息(实际上来自Gulliver's Travels):

http://en.wikipedia.org/wiki/Endianness [ ^ ]



祝你好运!
Two bytes is 16 bit. Try BitConverter.ToUInt16 instead. Also, read the two bytes at once instead one at a time. Network bytes are big-endian and your system is little-endian. You now handle them as little endian, mixing up the byte-order, resulting in a faulthy value.

Some nice extra info about these endians (actually from Gulliver’s Travels):
http://en.wikipedia.org/wiki/Endianness[^]

Good luck!


这篇关于如何组合两个字节来获取c#中的原始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆