Convert unsigned integer to signed integer

1 month 4 days ago #29535410 by alhaddad
Hello everyone,
I'm having a problem with the MicroBasic Script while receiving integers, as they only appear to be unsigned integers. 
The controller (FBL2360T) is communication with another control unit via CAN Bus (RawCAN), and I am sending from the external control unit target increments values in negative values, but the MicroBasic Script is reading the data as unsigned integers! 
How could it be possible to convert those values to signed integers, since the programming possibilities in this scripting language are limited?
Thanks in advance.
 

Please Log in or Create an account to join the conversation.

1 month 4 days ago #29535411 by LROBBINS
I have a suspicion of what's going on.  Integers in MicroBasic are always 32 bit signed integers.  CAN, however, sends bytes, which are 8-bit positive integers.  If you want to send a negative value over CAN you have to calculate it as a signed integer (for example, a 16-bit signed integer is the default in the Arduino flavor of C++), then use bit manipulations (or if your compiler has them, built in functions like HiByte and LoByte) to split that into two bytes for transmission.  Then when those two bytes arrive in the Roboteq, it will put each of them into the 8 least significant bits 32-bit integers and you have to again use bit operations to concatenate the 8 least significant bits of each of these to get a single integer.   Then, if the uppermost bit of those 16 is a 1 you have to move it to the 32nd bit of this new combined number (probably the easiest way to do that is if the 16th bit is 1, reset it to 0 and set the 32nd bit to 1).  Remember that the 32nd bit is bit number 31 because bit indexing starts at 0 (same for the 16th bit - it's bit number 15).

Please Log in or Create an account to join the conversation.

Time to create page: 0.067 seconds