Issue
I'm attempting to send a message over a serial connection from a Raspberry Pi 4 to a Teensy 4.0 (an Arduino-based microcontroller), from the TX-RX pins on the RP to the TX-RX pins on the Teensy.
I'm using C++ and the wiringPi
library on the RP, with it's default Raspberry Pi OS.
I made a minimal setup, and simply tried sending a message from the RP to the Teensy.
However, it appears to not be working. While there are no displayed errors on the RP side, there is no message recieved on the Teensy side. I tried using both the serialPuts
and serialPrintf
commands on the RP side, but to no avail.
RP code:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
int port;
if ((port = serialOpen("/dev/ttyAMA0", 9600)) < 0)
{
fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
return 1;
}
if (wiringPiSetup() == -1)
{
fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
return 1;
}
//serialPrintf(port, "test\n");
serialPuts(port, "test");
serialClose(port);
return 0;
}
Teensy code:
#include "Arduino.h"
void setup() {
Serial1.begin(9600);
Serial.println("Listening...");
}
void loop() {
if (Serial1.available() > 0)
{
String incoming = Serial1.read();
Serial.print("Recieved: ");
Serial.println(incoming);
}
}
Since I never attempted to connect via a serial port on a Linux before, I needed to find out what the "/dev/ttyAMA0"
string meant. After doing some research, as far as I understand, that string represents a serial port that can be opened to communicate with another device and the RP.
I also tried running the ls -l /dev/tty*
command on my RP in order to list all the supposed serial ports, however it prompted me with over 60 different tty
ports... I'm not quite sure what to look for, or what port name to pick here. I also tried running the command with my Teensy disconnected, to try and see whether I can identify the port that's connected to the Teensy, however there wasn't a port that disappeared when I disconnected the Teensy...
I know for a fact that the Teensy is working, as I can access it from my Arduino IDE, and I verified that the TX-RX are cross-connected (RX->TX & TX->RX). I connected RP pin GPIO 14
to Teensy pin 0
, and the RP pin GPIO 15
to Teensy pin 1
.
I'm also powering the Teensy via the 5V and GND pins on the RP, not that it should matter for the serial communication.
Here's a diagram and an image of my setup:
Is there something I'm missing here? Should I be using another tty
file in order to connect properly here? How do I decide which tty
file to pick? Am I supposed to do some sort of pre-setup on the RP in order to use the serial pins?
Thanks for reading my post, any guidance is appreciated.
Solution
Figured it out:
I was supposed to use the /dev/ttyS0
serial port file when initializing my serial connection, instead of the /dev/ttyAMA0
I was attempting to use previously.
It only appeared in the ls -l /dev/tty*
list once I followed hcheung's suggestion of disabling the login shell's accessibility over the serial port, and re-running the command.
Answered By - Runsva Answer Checked By - Robin (WPSolving Admin)