News:

Let's find out together what makes a PIC Tick!

Main Menu

Sending a - Signed Integer ?

Started by Craig, Oct 07, 2021, 07:59 PM

Previous topic - Next topic

Craig

Hello
I am trying to send a Negative or Positive Word (Signed Integer) over a Radio link. It prints correctly on the TX Side where I have a 18F47K40 Pic but,
Gives a strange value on the RX Side Pic24FJ64GA004. Both Devices are Transmitting correctly and sending all the other Data Correctly.
It is only this small issue that I am Having in Sending a - Negative Value over the Radio link, If I send a + Positive Value it works Correctly!

Is there a Better Way to do this, Maybe sending the - in it's own Array?

ON PIC18F47K40 Side:
____________________
Dim anTxBuf[Packet] As SWord 
Dim DistTR As SWord
anTxBuf[7] = DistTR            ' Load the Value -256 into array to send over TX
_____________________________________

ON PIC 24FJ64GA004 Side:
Dim anRxBuf[35]   As SWord
Dim VRead         As SWord

If anRxBuf[7] > 0 Then                                                                           
    VRead = anRxBuf[7]                                                                           
    VRead = (VRead * 10)                                                                       
    DelayMS 5
    Print at 1,1, SDec3 VRead,"  "
Endif
'--------------------------------------------

Kind Regards

Craig


Yasin

Quote from: Craig on Oct 07, 2021, 07:59 PM...
Dim anRxBuf[35]   As SWord
...
...
If anRxBuf[7] > 0 Then 


AnRxBuf is less than zero when it is negative. That's why the "If...Then" subroutine doesn't run.

John Lawton

Presumably your radio link transmits and receives bytes, not words. Signed bytes are -127 to +127. Presumably your anTxBuf[] array is sending bytes so if you load that buffer with -256 then you'll get rollover of the byte value? But maybe I've misunderstood.

John

John Drew

Hi Craig,
What I did many years ago in an earlier version of the compiler was to in effect create my own sWord by testing for negative at the Tx end and if so adding 65535. At the RX end I'd test for a number exceeding 32763, if so, subtract 65535 and assign to a sWord.
Without checking I think the latest compilers do that for you.
John

top204

#4
As far as all serial commands are concerned, a variable sent is neither signed or unsigned or floating point. It is simply a single or multiple binary values being transmitted or received depending on the size of the variable type.

For an 8-bit variable being transmitted, a single 8-bit binary value will be sent. For a 16-bit variable, two 8-bit binary values will be sent. For a 24-bit variable, three 8-bit binary values will be sent, and for 32-bit variables (including 32-bit floats), 4 bytes will be sent. The same for receive.

Once the value has been received into a specific variable type, it is then treated as signed or unsigned or floating point. So if the program is sending a 16-bit value of -256, it is sending two 8-bit binary values, and these must be received in a variable type that holds two 8-bit binary values. i.e. A Word or SWord. If not, there will be underflows or overflows and the value received will never contain the correct value that was transmitted.

Craig

Only got back to this project now. Thanks for all the help John, John, Yasin and Les, Very Much appreciated. The Tx uses a 64 Byte Fifo so i divided it by 10 on the TX side and then multiplied it by 10 on the RX Side to balance things out.

Regards
Craig