Hello everyone.
As the thread topic suggests, it appears to be almost “impossible” to implement Half duplex RS-485 on the Raspberry Pi Ubuntu Core 16 image. Have anyone of you implemented a fix or hack to enable this?
Here is my setup:
MAX3485 RX, TX <–> TX, RX pins of (ttyAMA0)
MAX3485 RE/DE <–> GPIO 17 (ALT3 mode, RTS0)
The MAX3485 is connected to a simple cheap RS485 USB converter, and when opening the /dev/ttyAMA0 port (cat /dev/ttyAMA0) I can receive data, so which means the RS-485 setup is working as it should. I can also manually toggle the GPIO17 HIGH to write, and that works.
I enabled RTS/CTS using the code from: https://github.com/mholling/rpirtscts
Now the kicker is just getting it to work on the kernel side (using ioctl to configure).
But my sample app kicks me out saying error 25: ‘Inappropriate ioctl for device’ when I want the kernel serial driver to handle the RTS toggling when receiving/transmitting.
Any advice?
My C app:
#include <linux/serial.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <string.h>
int main(void)
{
int fd; /File Descriptor/
int retStatus;
fd = open("/dev/ttyAMA0",O_RDWR); //Opening the serial port
if (fd < 0){
printf(“Unable to open /dev/ttyAMA0\n”);
printf("%s", strerror(errno));
close(fd);
return 1;
}
struct serial_rs485 rs485conf;
/* Enable RS485 mode: */
rs485conf.flags |= SER_RS485_ENABLED;
/* Set logical level for RTS pin equal to 1 when sending: */
rs485conf.flags |= SER_RS485_RTS_ON_SEND;
/* Set logical level for RTS pin equal to 0 after sending: */
rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
/*Set the stuffs*/
retStatus = ioctl(fd, TIOCSRS485, &rs485conf); /*Set the RS-485 mode*/
if (retStatus < 0){
printf("ioctl failed and returned errno %d \n",errno);
printf("%s", strerror(errno));
getchar();
}
else{
printf("\nRS-485 Enabled.\n");
getchar(); //To view the pins change before closing the port
}
close(fd);
}