Asked

UART And ASCII Dec.

@DJ

My question pertains to UART communication. Hopefully I can explain this ... lol?

ASCII UARTWrite(0,0,"49") in decimal form is = 1 UARTWrite(0,0,"50") in decimal form is = 2 UARTWrite(0,0,"51") in decimal form is = 3 Yada Yada Yada UARTWrite(0,0,"57") in decimal form = 9

How do I write the decimal form of 10? UARTWrite(0,0, "49 48")   Does not work?

Does my question make sense?


Related Hardware EZ-B v4
Related Control EZ-Script Console

ARC Pro

Upgrade to ARC Pro

Stay on the cutting edge of robotics with ARC Pro, guaranteeing that your robot is always ahead of the game.

PRO
Synthiam
#9   — Edited

I think herrball is asking about ezscript, not javascript. So the ezscript manual says...

UARTWrite( boardIndex, port, data )

         Write ASCII data through the Peripheral UART.

         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: UARTWrite(0, 0, hello world )

         Example: UARTWrite(0, 0, 0x30, 0x40, hello )

and the answer would be

UARTWrite(0, 0, 40, 48)
#10   — Edited

DJ is correct about the EZ Script.

@Athena

UARTWrite(0, 0, 40, 48) ... which I believe should be UARTWrite(0, 0, 49, 48) does not work. It sends it in two separate parts, Decimal 49 and then Decimal 48. Which is a 1 and then a 0. Not as a 10.

I have been able to do a work around, instead of 1,2,3,4,5,6,7,8,9,10. I have done, 0,1,2,3,4,5,6,7,8,9 It still gives me the 10 Decimals I need but not using the 10.

Maybe this will help show you what I need to do?

void serialEvent() { while (Serial.available()) { // read the oldest byte in the serial buffer: incomingByte = Serial.read();
// if it's a capital 1 (ASCII 49), go to void oneon: if (incomingByte == '1') oneon(); // if it's a capital 2 (ASCII 50), go to void oneoff: if (incomingByte == '2') oneoff(); // if it's a capital 3 (ASCII 51), go to void twoon: if (incomingByte == '3') twoon(); // if it's a capital 4 (ASCII 52), go to void twooff: if (incomingByte == '4') twooff(); // if it's a capital 5 (ASCII 53), go to void threeon: if (incomingByte == '5') threeon(); // if it's a capital 6 (ASCII 54), go to void threeoff: if (incomingByte == '6) threeoff(); // if it's a capital 7 (ASCII 55), go to void fouron: if (incomingByte == '7) fouron(); // if it's a capital 8 (ASCII 56), go to void fouroff: if (incomingByte == '8') fouroff(); // if it's a capital 9 (ASCII 57), go to void fiveon: if (incomingByte == '9') fiveon(); // if it's a capital 10 (ASCII ?), go to void fiveoff:   ..... <<<<<<<<<<<<<<<<<<<<< if (incomingByte == '10') fiveoff();   ...... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< } }

If this all makes sense? LOL

It would be nice to know though how it can be done, if it can?

PRO
Synthiam
#11  

Well the problem is 10 is two bytes. Because it’s two characters.

insread if using strings, just use the decimal value. There’s no such thing as an ascii 10. Because a 10 is a 1 and a 0 next to each other. Just like there’s no such thing as an ascii banana. There’s an ascii b and an ascii a and so on

so it looks like you’re making a command protocol. If so, just use the decimal value, not the ascii value.

PRO
Synthiam
#12  

I was on my phone for the previous response so that I couldn't provide a code example. But here's what you want to do.

In ARC with ezscript...

# Send the command oneon();
UARTWrite(0, 0, 0)

# Send the command oneoff();
UARTWrite(0, 0, 1)

# Send the command fiveon();
UARTWrite(0, 0, 9)

# Send the command fiveoff();
UARTWrite(0, 0, 10)

On your Arduino or "whatever," this goes on...

void serialEvent() {
while (Serial.available()) {
   
//Read the oldest byte in the serial buffer:
incomingByte = Serial.read();

if (incomingByte == 1) oneon();
if (incomingByte == 2) oneoff();
if (incomingByte == 3) twoon();
if (incomingByte == 4) twooff();
if (incomingByte == 5) threeon();
if (incomingByte == 6) threeoff();
if (incomingByte == 7) fouron();
if (incomingByte == 8) fouroff();
if (incomingByte == 9) fiveon();
if (incomingByte == 10) fiveoff()
}
}

@athena, what do you think?

PRO
Canada
#13  

DJ Sures' approach is correct. In your case, you want to send a single byte to represent a command, and that byte's value corresponds to the command number. You do not need to use ASCII characters for this; you can use the actual byte values directly.

In EZ-Script, you would use the UARTWrite command with the decimal value of the command you want to send. For example, to send the command fiveoff, which you want to correspond to the number 10, you would use:

UARTWrite(0, 0, 10)

This sends the byte value 10 directly to the UART port. On the Arduino side, you would compare the received byte to the decimal values, not ASCII characters. DJ Sures provided the correct Arduino code to handle these byte values.

Remember that when you use UARTWrite with a number (not a string), you are sending the actual byte value, not an ASCII representation. This is a more efficient way to handle simple command protocols where each command can be represented by a single byte.

If you have any further questions or need additional clarification, feel free to ask!


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.
PRO
Synthiam
#14  

@athena explain to HerrBall why using a string in a communication protocol for the command is bad practice and requires additional effort.

PRO
Canada
#15  

Certainly, DJ Sures. Using strings in a communication protocol for commands can be considered bad practice for several reasons, especially when dealing with microcontroller communication, such as between ARC and an Arduino. Here's why:

  1. Increased Data Size: Strings are composed of multiple bytes, one for each character. This means that sending a command like "10" actually sends two bytes (49 and 48 for '1' and '0', respectively) instead of just one byte if you were to send the command as a number (10). This increases the amount of data transmitted, which can be less efficient, especially for high-throughput or real-time systems.

  2. Complexity in Parsing: When you send a string, the receiving device must parse and interpret each character to reconstruct the intended number or command. This adds complexity to the code and requires additional processing time, which can be a critical factor in performance-sensitive applications.

  3. Error Handling: With strings, there's a higher chance of errors in communication. For example, if part of a string is lost or corrupted during transmission, it can be more difficult to detect and correct the error compared to dealing with single-byte numerical commands.

  4. Memory Usage: Microcontrollers like the Arduino have limited memory resources. Storing and manipulating strings can consume more memory than handling simple numerical values, which can lead to memory constraints in larger programs.

  5. Consistency and Standards: Using numerical values for commands often aligns with industry standards and protocols, which typically use numerical codes or bytes to represent different commands and statuses. This makes it easier to maintain consistency and interoperability between different systems and devices.

For these reasons, it's generally better to use numerical values for commands in a communication protocol. It leads to simpler, more efficient, and more reliable communication between devices. In your case, sending a single byte to represent each command is a straightforward and effective approach.


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.