Passing serial from Raspberry to Arduino USB HID

411 views Asked by At

I'm trying to pass data from a tkinter app on a RPi, to an Arduino Leonardo set up as a USB HID device, then on to a computer from the arduino using keyboard.write(), but not all the data ends up at the receiving computer.

I've got the tkinter app running on the Pi, and the Pi is hooked up to load cells and ultrasonic sensors that register weight and dimensions of parcels. The values are placed in a tkinter tree view. This is all working fine. I have hooked the Pi up to an Arduino Leonardo via serial (GPIO), the arduino is set up as a USB HID device. I want the values from the tree view on the pi to be passed to the arduino, and then be passed on (USB HID) to a connected computer. I have almost got this working, but not all the data is passed through, it will stop after a few characters. I need to pass TAB and INSERT to the receiving computer, so I’m sending “T” and “I” strings from the Raspberry, then “converting” that to HEX values on the arduino.

Both serial and USB HID is new to me, so I’m not sure if I’ve got it set up correctly… Any ideas?

This is the code running on the Raspberry Pi:

x = kolliTree.get_children()
    if x:
       count = len(x) 
       for i in x:
           a = str(kolliTree.item(i)["values"][0])
           dX = str(kolliTree.item(i)["values"][1])
           dY = str(kolliTree.item(i)["values"][2])
           dZ = str(kolliTree.item(i)["values"][3])
           w = str(kolliTree.item(i)["values"][4])
           kolli = ["T", a, "T", dX, "T", dY, "T", dZ, "T", w, "T", "I"]
           for i in kolli:
               leonardo.write(i.encode("latin1"))

This is the loop on the arduino:

void loop() {

  if (Serial1.available() > 0) {
    byte inChar = Serial1.read();
    if (inChar == 84) {
      Keyboard.write(0xB3);
    }
    else if (inChar == 73) {
      Keyboard.write(0xD1);
    }
    else {
      Keyboard.write(inChar);
    }
    delay(10);
1

There are 1 answers

1
jhoog On

Try sending some ASCII characters to the Pi to save yourself a step with your tab and insert commands. Arduino and most languages have built in functions to convert numerical values to ASCII characters. You can assign a constant integer based on the link below and call it, as in the second link below.

If some data gets through, but not all, double check your baud rates. Also check to see if the values are getting passed with any padding around them, like spaces on the front or back end. It may be useful to try to force a data type conversion on the receiving end as well to make sure you're not receiving an int when you expect a string (for example).

Hope this helps!

http://www.asciitable.com/

https://www.instructables.com/id/Converting-integer-to-character/