4,199
edits
Changes
m
</pre>
= Appendix A: 485-example.c : Basic example of RS-485 half duplex transmission =
<pre>/* 485-example.c : Basic example of RS485 half duplex transmission */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <asm/ioctls.h>
#include <linux/rs485.h>
int main(void)
{
char dev[] = "/dev/ttyS0";
char tosend[] = {'A', 'B', 'C'};
struct serial_rs485_settings ctrl485;
int status;
int fd;
struct termios ti;
speed_t speed;
/* Open the port */
fd = open(dev, O_RDWR);
if (fd < 0) {
printf("%s: Unable to open.\n", dev);
return -1;
}
/* Set the port in 485 mode */
ctrl485.flags = SER_RS485_MODE | SER_RS485_MODE_RTS | SER_RS485_RTS_TX_LOW;
ctrl485.delay_before_send = 0;
ctrl485.delay_after_send = 0;
status = ioctl(fd, TIOCSRS485, &ctrl485);
if (status) {
printf("%s: Unable to configure port in 485 mode, status (%i)\n", dev, status);
return -1;
}
/* Set the port speed */
speed = B9600;
tcgetattr(fd, &ti);
cfsetospeed(&ti, speed);
cfsetispeed(&ti, speed);
tcsetattr(fd, TCSANOW, &ti);
/* Send content to RS485 */
if (write(fd, tosend, sizeof(tosend)) != sizeof(tosend)) {
printf("%s: write() failed\n", dev);
}
return 0;
}
→Appendix A: 485-example.c : Basic example of RS-485 half duplex transmission
| 5 |---------- B --------------| 5 |
--- ---
</pre>