Asked
— Edited

@Athena When I write a script using Javascript and I want to set a variable what command should I use. I've used both setVar and simply just Var. Both have worked for me.
For example I have this in my INT Script that runs when my project starts:
setVar( "$adcSoilSpecified", 200 );
And I have this in a stand alone script that operates a motor and watches an encoder:
var Get_Lft_Elbow_P = "2, Getp\r";
UART.hardwareUartWrite(0,Get_Lft_Elbow_P.length,2); //UART 0 on EZB 2
Which is the correct one to use and what is the difference between the two?
Related Hardware EZ-B v4
@Athena, what is the Javascript command that is equivalent to the EZ Script command:
UARTReadBinary( boardIndex, port, numBytes, variable )
Receive binary bytes from the Peripheral UART Receive Buffer of the EZ-B v4 into the variable as an array. The UART receive buffers on the EZ-B v4 are 5,000 bytes. To know how many bytes are available, use the UARTAvailable() function. The Board Index is the EZ-B index starting at 0. The port can be 0, 1 or 2. Look near at the UART Port section lower in this document for the EZ-B Pin’s associated with each UART Port. Example: UARTReadBinary(0, 0, 10, $variable) Example: UARTReadBinary(0, 0, UARTAvailable(0, 1), $variable))
I am trying to convert one of my EZ Scripts into Javascript and have this command I can't figure out how to write it in Javascritp:
Here is my whole EZ script that works. I'm sending a Simple Serial command to a Kangaroo Motor controller. The controller will return a position number starting with a upper or lower case "P" (P or p). Upper case "P" means the motor stopped and a lower case "p" means it's still moving. The purpose of this script is to find out if the motor is still moving:
In ARC's JavaScript environment, to perform an operation similar to the EZ-Script
UARTReadBinary()
function, you would use a combination of theUART.initHardwareUart()
,UART.hardwareUartWrite()
, andUART.hardwareUartRead()
functions. These functions allow you to initialize a UART port, send data, and read incoming binary data from the UART buffer.Here's how you might convert your EZ-Script to JavaScript for sending a command to your Kangaroo Motor controller and reading the response to check if the motor is still moving:
This script demonstrates how to send a command to the Kangaroo Motor controller and read the response to determine if the motor is still moving. Note that the
sleep()
function is used to provide a delay between sending the command and reading the response, which may need to be adjusted based on your specific requirements.I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Ugh. A lot of that doesn't sound correct. I already have written commands to do that. It's a one line command:
All I really wanted to know was if there is a JavaScript command that I can use that is the same as this EZ Script command.
@Athena, One last question, Like I mentioned, this script works as written in EZ Script. Inside the script control on the console I'll see the return in Binary numbers which is OK (112, Then this is a lowercase p and move is still underway and if Print command returns 80, Then this is a Uppercase P and move is over.). Now on the outside of the control in the control's console window I'll see the actual "P" or "p" letter. Why the different return between the script control's inside and outside console windows? What should I look for as a return in a script? Binary or the actual letters?
She's "sort of" correct because the code you pasted is a bit unrelated to the question, so i think she was trying to help you rewrite the whole thing without having any context of what the code does.
The answer to your question is this: synthiam.com/Support/javascript-api/UART/hardwareUartRead
hardwareUartRead
You can see all available UART commands in the UART section of the javascript support. That link will bring you to it.
In her example, she demonstrated how to determine how many bytes are available, and then read those bytes using hardwareUARTRead
OK, Thanks DJ, I'm able to get that far in a JavaScript written script and am able to read the returned value. For example I'll get a returned position that looks like this: 1,P2074 *The "1" is the motor channel on the Kangaroo *The "," is just a comma. *The P is returned in wither upper case or lower case depending on if the move is completed or not (motor moving or stopped). *The last numbers are the actual position when the data was sent.
Then I use this command to Parse the numbers from the "1" and "," :
When I try to adjust the .substr(3, 7) to target the "P" in the returned value of 1,P2074 I get a "NaN". My target is .substr(2, 4)
Any Idea why I am gettin NAN and not the status of the P?
I need clarification to ensure my advice doesn't take you down the wrong path - why are you asking about reading bytes, but the last bit of code you pasted shows reading a string?
It seems there might be a bit of confusion regarding handling binary data and string parsing in JavaScript. Let's clarify the process of reading data from a UART port and extracting specific parts of the data, such as the 'P' character and the position value from a string like "1,P2074".
Firstly, when you use
UART.hardwareUartRead()
, you're reading binary data (bytes) from the UART buffer. If you're expecting a string response like "1,P2074", you'll need to convert the binary data (byte array) into a string for easier manipulation.Here's a step-by-step approach to read the UART data, convert it to a string, and then parse the string to extract the 'P' character and the position value:
Here's how you could implement it in JavaScript:
Regarding your question about getting
NaN
(Not a Number) when trying to parse the position:NaN
results from attempting to convert a non-numeric string to a number usingparseInt()
or similar functions. If you're targeting the correct substring but still gettingNaN
, ensure that the substring indeed contains only numeric characters..substr(2, 4)
usage might not be what you want if you're trying to extract the position..substr(2, 4)
starts at index 2 and extracts 4 characters, which, in your example "1,P2074", would result in "P207". To extract the position, you should start parsing after the 'P', as shown in the example above.Remember, the exact parsing logic might need to be adjusted based on the consistent format of the data you're receiving from the UART port.
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
@athena what about this?