5

I am trying to communicate from my raspberry PI to a teensy (a arduino that can pretend to be a mouse and keyboard for those uninitiated).

I want to receive information on the arduino, and based on that information move the mouse.

On the arduino side, I have made this test script:

void setup() {
    Serial1.begin(9600); // According to the Teensy Docs, this is the RX1 & TX1 on my board.
    // Serial itself corrosponds to the micro-usb port
}
String msg = "";      

void loop() {

    if(Serial1.available() > 0) {
      msg = "";
      while(Serial1.available() > 0) {
          char read = Serial1.read();
          msg += read;
      }
      Serial1.write('X'); // Acknowledge with reply
    }
    Serial1.println(msg); // Output to console for debugging
    // Should be a number 1-9
    // TODO: further processing

}

On the raspberry pi, I am running this test script:

import time
import serial
import random       
ser = serial.Serial(            
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
 timeout=1
)
while True:
    n = random.randint(1,9)
    print("Writing", n)
    ser.write(n)
    time.sleep(1)
    feedback = ser.read()
    print(feedback) // Expecting 'X'

When i run the script, I see no output in the serial console as well as an empty message (b'') (Note the timeout parameter)

I have already enabled serial communication with raspi-config and restarted. When I list devices (ls -l /dev/), I can see:

lrwxrwxrwx  1 root root           5 Apr 28 20:21 serial0 -> ttyS0
lrwxrwxrwx  1 root root           7 Apr 28 20:21 serial1 -> ttyAMA0

As an additional test, I ran minicom -b 9600 -o -D /dev/ttyS0 with 1 wire connecting RX to TX on the pi, and it successfully echoed back.

Do I have a code issue, or possible hardware issue? Maybe since it is a teensy some different protocol is required? See here

I'm out of ideas as to why it isn't communicating correctly. Here is my wiring:

retep
  • 317
  • 2
  • 13
  • can you talk to the teensy using the arduino IDE serial terminal? or minicom ... most of the time i see this issue is that arduino has some small spinup time for serial after opening the port ... try putting a sleep for a few seconds after python opens the serial port for the arduino before trying to send any data – Joran Beasley Apr 29 '20 at 02:12
  • Am I looking at the picture wrong or do you have Rx-Rx and Tx-Tx? – Delta_G Apr 29 '20 at 03:28
  • @Delta_G isn't it supposed to line up? – Peter S Apr 29 '20 at 04:16
  • When you talk to someone, do you talk into their mouth and listen to their ears? What one side transmits the other needs to receive. – Delta_G Apr 29 '20 at 04:26
  • Hi, I've answered a similar question here for an ESP32: https://stackoverflow.com/a/76400334/13794189 – Miguel Tomás Jun 04 '23 at 12:09

1 Answers1

4

You have the Rx line connected together and the Tx lines connected together. What one transmits the other needs to receive. You need to go Tx-Rx and Rx-Tx.

Delta_G
  • 2,927
  • 2
  • 9
  • 15