Award
Congratulations DJ Sures, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

How To Use The Uart Script Functions

Assistance Requested

Help OldBotBuilder with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

OldBotBuilder claims to have checked these sources:
 
#1

I am trying to interface my Robotics Bioloid to the EZ-Bv4 using UART 0 to transmit/receive the 5 word Comm packet; FF 55 Data_l ~Data_l Data_h ~Data_h
~ represents Inverse (1's Complement), Data_l is the least significant hex value, Data_h is the higher significant hex value the Robotis boomerang controller sends when a button is pressed.
For example: FF 55 01 FE 00 is the value 01 (button U)
FF 55 00 FF 01 is the value 256 (Hex 10) (button 5)

I can not get the new UartReadRinary, UartWriteBinary to work correctly.

Is there to be a tutorial on the use of the Uart script functions?
Thanks for any help.

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.


Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


#2

Here is an example that will help you...

Code:


# Define the Button U
# --------------------------
DefineArray($ButtonU, 5)
$ButtonU[0] = 0xFF
$ButtonU[1] = 0x55
$ButtonU[2] = 0x01
$ButtonU[3] = 0xFE
$buttonU[4] = 0x00

# Define the Button 5
# --------------------------
DefineArray($Button5, 5)
$Button5[0] = 0xFF
$Button5[1] = 0x55
$Button5[2] = 0x00
$Button5[3] = 0xFF
$button5[4] = 0x01



# Init the hardware UART. You would only do this once
# but in this example i have to show that it must be done
# Also, i have no idea what baud rate you will be using
# --------------------------------------------------------
UartInit(0, 0, 9600)

# Send the Button U code
# ----------------------
UartWriteBinary(0, 0, $ButtonU)

# Now send the Button 5 code
# ---------------------------
UartWriteBinary(0, 0, $Button5)

#3

Thanks DJ.

I get the general idea you show for writing to the Uart.
What about UartReadBinary?

BTW - Thanks for the very quick response!

#4

Read Binary puts the data into the provided Array Variable - there is ez-script help with an example

#5

I am a lot closer but still having problems:
1) If I don't press a controller button soon enough (several seconds) EZ-B4 disconnects and the red led flashes.

After a power cycle and 'connect' I start the get hex data script and hit button 5 I get this:

User-inserted image

and a RealTerm monitor connected to the RX of EZ-B4 shows this:


User-inserted image

Problem 2) RealTerm shows the HEX data but the script produced decimal data.

Here is the script:

Code:


# get hex data
uartinit(0, 0, 57600)

print("UART 0 Initialized at 57600 baud."Winky

:loop

$x = UartAvailable(0, 0)

sleep(500)

UARTReadBinary(0, 0, 6, $data)

$byte1 = $data[0]
$byte2 = $data[1]
$byte3 = $data[2]
$byte4 = $data[3]
$byte5 = $data[4]
$byte6 = $data[5]

sleep(200)
if ($byte1 == 255 AND $byte2 = 85)

print( $byte1 +" "+ $byte2 +" "+ $byte3 +" "+ $byte4 +" "+ $byte5+ " "+ $byte6 )

endif

sleep(200)

goto(loop)

#6

You don't have to bother using hex.
Just use decimal values, the Bioloid uses unsigned integers 0 to 255 to communicate.

if you convert the hex to the decimal value you'll see the same values.

I discovered this when trying to control the dynamixels with python.

#7

What is the meaning of $x is in your code sniplet...

Code:


$x = UartAvailable(0, 0)

sleep(500)

UARTReadBinary(0, 0, 6, $data)



Because you are not using $x anywhere - and the Read Binary is hardcoded to a value of 6 characters. This means you are 100% certain there is 6 characters available? You cannot be 100% certain without actually checking. So hardcoding a value of 6 is why you're program is crashing. You're requesting more information than what is available.

Perhaps something like this will make more sense...

Code:


:readLoop
$x = UartAvailable(0, 0)

if ($x < 6)
sleep(50)
goto(readLoop)
endif

UARTReadBinary(0, 0, 6, $data)

#8

DJ, Thanks again.
Works like a champ! Also gave me better understanding of how the script function operates.

Now I am ready to 'UartWriteBinary'.

#9

Woo! Smile your progress is impressive