yeokm1 / SwiftSerial

A Swift Linux and Mac library for reading and writing to serial ports.
MIT License
136 stars 40 forks source link

Data transmission issue on Raspberry Pi #21

Open gregorFeigel opened 2 years ago

gregorFeigel commented 2 years ago

Issue description

After successfully compiling and testing the SwiftSerial package on my Mac I decided to give it a try on the Pi. Since the one and only USB port is used by my Ethernet dongle and my Arduino-based shield offers a GPIO header with RS323 support, I chose the Pi's miniUART port /dev/ttyAMA0. In order to be able to use the port I disabled bluetooth (dtoverlay=disable-bt), removed the console=serial0,115200 entry in the /boot/cmdline.txt file, disabled all bluetooth relevant daemons and rebooted the Pi. The compiled binary ran without crashing, but I didn't get the expected result message. I double and triple checked all the connections with my multimeter but everything was fine. I decided to test the serial connection with the bash cmd from below and everything worked fine. To rule out that the error is coming from a poorly configured operating system, I ran a test using the python script form below and everything worked as expected. A little confused, I re-run the Swift binary and this time everything worked as expected. After rebooting, the swift binary serial communication failed again, so I ran another test with the python script and it worked. Now that I run the Swift binary again, I got it working too. I removed all Swift related stuff, recompiled everything, set up my oscilloscope and logic analyzer, and rebooted the Pi. I connected the probes to the Tx and Rx pin and booted the Pi. No interfering signals, so bluetooth and UART terminal function are properly disabled and not messing up my data. Next, I run the Swift binary and encounter the same transfer issues again, but this time I can see what's going on on the logic analyzer display. It seems like all data sent is received back immediately and sent again and again until an unknown timeout occurs. This unwanted loopback when sending/receiving data only occurs when using the Swift Serial or SwiftyGPIO library. Everything works fine when using bash or python. Running the swift binary after running the python script allows the binary to run without issues until the next reboot. For now I just added a cronjob to run the python script on reboot but that seems like a bad workaround. Running the binary with sudo doesn't help. Images of the logic analyser data below.
How can this problem be solved ?

Working environment

Raspberry Pi

Code Snippets

Swift code

let serial = SerialPort(path: "/dev/ttyAMA0")
serial.setSettings(receiveRate: .baud9600, transmitRate: .baud9600, minimumBytesToRead: 1, parityType: .none,  sendTwoStopBits: false)
do {
    try serial.openPort()
    _ = try serial.writeString("SDI\n0R0C!\n")
    let res = try serial.readLine()
    print(res)
}
catch { fatalError(error.localizedDescription) }

compiled with: swift build -c release executed with: /home/pi/workDir/serial_Test/.build/release/serial_Test

Python Code

import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write("SDI\n0R0C!\n")

executed with python testSerial.py

Shell comand

echo "SDI\n0R0C!" >> /dev/ttyAMA0

Arduino

Just a small script replying to the serial input. Sends "Unknown command:$RxData" if the command is not in the Arduinos lookup table.

Photos

Logic Analyser Colors: Red = Rx, Green/Yellow = Tx

Photo 1: This is what the communication should like. Terminal_Normal

Photo 2: Valid transmission - Tx. Transmitt

Photo 3: Valid transmission - Rx. recieve

Photo 4: Valid transmission - Rx and Tx line. DATA_NORMAL

Photo 5: INVALID transmission - Rx and Tx line. DATA_ERROR_TX_RX

Photo 6: INVALID transmission - Logic analyser terminal. ERRO-COMMUNICATION_LOGICTERMINAL

yeokm1 commented 2 years ago

Hi @gregorFeigel Thanks for reporting this issue. I don't have an RPi with me anymore and it's been ages since I have touched this library.

It seems very unusual to me that running a Python script beforehand will make things work on the RPi and therefore not something I can replicate without actual RPi hardware.

If I do get to work with RPis eventually, I will keep this issue at the back of my mind and test this library again.

In the meantime I will still keep this issue open.