Read Serial UART for Talk-Back, Command Processing, Troubleshooting

Below is an example of Serial Com from the Computer to UART-0 (Mega) collecting the command and sending it back while printing it on a 16x2 parallel LCD. Overall, it works, but I feel it’s clunky, it could miss consecutive commands, and could be better written in the cpp not-implemented-in-xod node.

Serial communication is still required for some applications, particularly in the science industry. Typically, a the computer sends a serial message to the unit, and the unit responds and sends a message back. Setpoints, calibrations, or even robotic commands can be sent by serial. I like to send commands to my board even in small poc projects to change analog output or change a digitpot from the computer, for example. Finally, many sensors still use serial to send the data, so serial capture is imperative.

I’m surprised there’s not already a CORE tool for this… Currently, the “Read Bytes” node reads all the bytes, but it 1) doesn’t give a way to get the whole string of bytes, 2) sends out only the last byte, 3) sends the byte as hex. Then, xod doesn’t have an easy conversion node from hex-to-ascii… seems like someone would just bang out all the common tools for arduino on the cpp side.

since I’m not quite smart enough to figure out the node writing, yet, I made a system that reads serial until a stop character, usually ‘\r’, 0dx, CR, and writes it to lcd and prints it back to the computer UART-0 that can be interacted with on the Arduino IDE serial monitor.

Pitfalls and Weird Stuff:

  1. You have to have the Arduino IDE Serial Monitor Closed while uploading from XOD to Mega.
  2. You have to have the XOD main patch active when uploading from XOD to Mega
  3. Don’t activate XOD Serial Monitor - ever - I don’t see any use for it, because you don’t get replies.
  4. After the Program compiles and uploads, go to Arduino IDE and cntrl+shft+M for serial monitor.
  5. Make sure carriage return is selected, for ‘\r’ end of message
  6. Program clears old lcd message when upload was successful

Here’s the Main for the Program shown with Arduino IDE command message ready.

Here’s inside the Read-Bytes-with-Stop shown with the Talk Back from the Mega to the Computer through the default Serial.

Here’s the LCD with the message

Here’s the xod file containing the above:
aurt-test.xodball (19.8 KB)

Now, this thing took me at least 3-days to figure out (though going back and forth which would be easier, either to stitch something together with existing nodes, or try to finally figure out how to write my own node in cpp). I managed to stitch this together, it works, but it’s likely flawed.

  1. The ‘\r’ is printed in the LCD, and probably the serial monitor, too, but there’s no Trim function available, or a way to kick off the last character.
  2. It would likely miss consecutive commands, because it requires to wait for serial not available in order to reset the accumulate-string.
  3. The thing is more complicated than what it should be.

Note: I tried to use any existing libraries, but couldn’t get them working at all, even the gabbapeople/uart-led-control…

Here’s some arduino code that does that same thing… It should only take 5 seconds for someone who knows what they are doing to make this into a cpp node. All we need is to excise when a message comes through, and not burn following messages.

Code From: https://www.best-microcontroller-projects.com/arduino-string.html

String sdata = ""; // Initialised to nothing.

void setup (void) {
  Serial.begin(9600);
  Serial.println("Command Interpreter");
}

void loop(void ) {
  byte ch;

  if (Serial.available()) {
    ch = Serial.read();

    sdata += (char)ch;

    if (ch == '\r') { // Command recevied and ready.
      Serial.println("Received " + sdata);
      sdata.trim();
      // Process command in sdata.

      sdata = ""; // Clear the string ready for the next command.
    }
  }
}

Which is then use for commands, that I didn’t program in xod
Code From: https://www.best-microcontroller-projects.com/arduino-string.html

```
byte test,start;

#define BUF_LEN 20

void setup (void) {
   Serial.begin(9600);
   Serial.println("Command Interpreter");
}

void loop(void ) {
static char sdata[BUF_LEN], *pSdata=sdata;
byte ch;
int val;

   if (Serial.available()) {
      ch = Serial.read();

      // -1 for null terminator space
      if ((pSdata - sdata)>=BUF_LEN-1) {         pSdata--;          Serial.print("BUFFER OVERRUN\n");       }

      *pSdata++ = (char)ch;

      if (ch=='\r') {  // Command received and ready.

         pSdata--;       // Don't add \r to string.
         *pSdata = '\0';  // Null terminate the string.

         // Process command in sdata.
         switch( sdata[0] ) {
         case 's':
            start = 1;
            Serial.println("Start Process");
            break;
         case 't':
            test = 1;
            Serial.println("Test");
            break;
         case 'v':
            if (strlen(sdata)>1)
               val = atoi(&sdata[1]);

            Serial.print("Val ");
            Serial.println(val);
            break;
         default: Serial.println(sdata);
         } // switch

         pSdata = sdata; // Reset pointer to start of string.

      } // if \r
   }  // available
}
```

Would it be a challenge to write a couple xod nodes that do these kinds of tasks? I haven’t cracked the nut on xod cpp node writing, yet. There’s a couple tutorials, that didn’t totally cement things, but making something like a “read sd card file” would be super useful. It would essentially incorporate all these concepts, and has been a hanging chad for ages. Tutorial on that would be nuts!!

xodeos!

~Xodballxod

1 Like

To make a quick hex-to-ascii, you can use the accumulate-string from the Stream set. Make the buffer as long as you want, even 1 byte, and defer a loop to rst.

image

Demonstration of accumulate string with each pulse, looking for end Byte ‘\r’