Click here to Skip to main content
15,438,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read numbers from Arduino using Visual Basic.net

I used SerialPort Tool and ReadByte function And I succeeded in reading the numbers from the Arduino

To understand me more This is an example

VB
TextBox1.Text = SerialPort1.ReadByte()


My code does this It reads numbers from Arduino using Visual Basic.net and writes them in a text box

But the ReadByte function only reads positive numbers and I want to read positive numbers and negative numbers

How can I read positive numbers and negative numbers from Arduino using Visual Basic.net ?

What I have tried:

I am trying to read numbers from Arduino using Visual Basic.net

I used SerialPort Tool and ReadByte function And I succeeded in reading the numbers from the Arduino

To understand me more This is an example

VB
TextBox1.Text = SerialPort1.ReadByte()


My code does this It reads numbers from Arduino using Visual Basic.net and writes them in a text box

But the ReadByte function only reads positive numbers and I want to read positive numbers and negative numbers

How can I read positive numbers and negative numbers from Arduino using Visual Basic.net ?
Posted
Updated 30-Sep-21 23:30pm

1 solution

Bytes are neither positive nor negative in themselves: they are a pure eight bit value and it's only when you convert them to an integer that they "Acquire" a sign (or strictly speaking any "numeric value" at all).
When you call ReadByte it returns a 32 bit integer which contains the byte value as it' least significant bits; all the others are zero, which means a positive value for an integer - a negative integer has the top bits set so a hex value of FFFFFFFF is minus one, FFFFFFFE is minus two, and so on.

If your Arduino is returning a byte value which uses the same scheme so FF is minus one, FE is minus two, ... then you will need to extend the "sign bit" through the rest of the integer value to get your "real number".

I'd probably do it by checking bit 7 of the integer, and using a binary OR to add FFFFFF00 to the value if it was set.
 
Share this answer
 

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