Jump to content

ADC conversion from serial - (Moved)


Stacker
 Share

Recommended Posts

Hi i need help with conversion of data received from serial.

I read hex value 30  8C  in binary    00110000  10001100  (16 bit) i need to  have result decimal of 561  (i try all but i crash my head on the wall)

so : adc send a 10 bit value with first number "30" as LSB and the MSB (2 bits) are the 2 most left (6-7) of  second number in binary other bits 0-5 need to discard

MSB                    LSB                               RESULT I NEED IS

10xxxxxxxx        00110000                    10 00110000

i try with:

 

Local $ret = _CommGetLine(@CR, 100, 1000) ; return 308C

$retL =stringtobinary(StringLeft($ret,1))  ; get first char as LSB
    $retH = stringtobinary(StringRight($ret,1)) ; get second char
    $reth= Bitrotate($retl,2,"W") ; then try but nothing to do
    $ret = $reth+$retl  ; this sum need to be 561

please help me thanks a lot

Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

If I understand you correctly, I come up with a value of 560 not 561.

$xLittleEndianValue = Binary("0x308C")

;Split the 2 binary bytes into separate bytes (LSByte & MSByte)
$xLSByte = BinaryMid($xLittleEndianValue, 1, 1)
$xMSByte = BinaryMid($xLittleEndianValue, 2, 1)

;Shift the most significant byte's bits 6 bits to the right
;i.e. 10001100 (0x8C) -> 00000010 (0x02)
$xShiftedMSByte = Binary("0x" & Hex(BitShift($xMSByte, 6), 2))

;Concatenate the MSByte and LSByte back into its binary representation
$xLittleEndianResult = Binary("0x" & Hex($xLSByte) & Hex($xShiftedMSByte))

ConsoleWrite(StringFormat("$xLittleEndianValue  = %s", $xLittleEndianValue) & @CRLF)
ConsoleWrite(StringFormat("$xLSByte             = %s", $xLSByte) & @CRLF)
ConsoleWrite(StringFormat("$xMSByte             = %s", $xMSByte) & @CRLF)
ConsoleWrite(StringFormat("$xShiftedMSByte      = %s", $xShiftedMSByte) & @CRLF)
ConsoleWrite(StringFormat("$xLittleEndianResult = %s / BigEndian = 0x%04X / Decimal = %i", $xLittleEndianResult, $xLittleEndianResult, $xLittleEndianResult) & @CRLF)

Output:

$xLittleEndianValue  = 0x308C
$xLSByte             = 0x30
$xMSByte             = 0x8C
$xShiftedMSByte      = 0x02
$xLittleEndianResult = 0x3002 / BigEndian = 0x0230 / Decimal = 560

 

Edited by TheXman
Corrected my endian references.
Link to comment
Share on other sites

You're welcome. 

I added comments to the original snippet.  Hopefully it helps you better understand what the code is doing.

Link to comment
Share on other sites

Here's a much shorter way to do the conversion, just 1 line. 

$xLittleEndianValue = Binary("0x308C")
$iValue             = BitShift(Int($xLittleEndianValue), 6)

ConsoleWrite(StringFormat("Initial Value          = %s (Little-Endian)", $xLittleEndianValue) & @CRLF)
ConsoleWrite(StringFormat("Result (Decimal)       = %i", $iValue) & @CRLF)
ConsoleWrite(StringFormat("Result (Little-Endian) = %s", BinaryMid($iValue, 1, 2)) & @CRLF)
ConsoleWrite(StringFormat("Result (Big-Endian)    = 0x%04X", $iValue) & @CRLF)

Output

Initial Value          = 0x308C (Little-Endian)
Result (Decimal)       = 560
Result (Little-Endian) = 0x3002
Result (Big-Endian)    = 0x0230

 

Edited by TheXman
Fixed typo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...