Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to do that. Someone help me.

What I have tried:

private void timer1_Tick(object sender, EventArgs e)
     {
          item3.Azimuth++;
         label2.Text = item3.Azimuth.ToString();


         byte[] bytesToSend = new byte[7] { 0x7E, 0xD6, Convert.ToByte(item3.Azimuth  >> 8), Convert.ToByte(item3.Azimuth), 0x20, 0x00, 0x40 };
         port.Write(bytesToSend, 0, bytesToSend.Length);
     }
Posted
Updated 18-Oct-20 23:21pm

1 solution

What it's saying is that the value in Azimuth is too big for a byte: it exceeds teh maximum value of 0xFF or 255 that a byte value can contain.
For example:
C#
int Azimuth = 0x1234;
byte b = Convert.ToByte(Azimuth);
Will throw the same error.
To fix that, mask it:
C#
int Azimuth = 0x1234;
byte b = Convert.ToByte(Azimuth & 0xFF);
Or better:
C#
int Azimuth = 0x1234;
byte b = (byte) (Azimuth & 0xFF);

In your code that would be:
C#
byte[] bytesToSend = new byte[7] {0x7E, 
                                  0xD6, 
                                  (byte) (item3.Azimuth >> 8), 
                                  (byte) (item3.Azimuth & 0xFF), 
                                  0x20, 
                                  0x00, 
                                  0x40};
 
Share this answer
 
Comments
CPallini 19-Oct-20 5:22am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900