Changes

User:Pau pajuelo

1,368 bytes removed, 18:43, 28 August 2012
m
Appendix A: 485-example.c : Basic example of RS-485 half duplex transmission
| 5 |---------- B --------------| 5 |
--- ---
</pre>
 
= Appendix A: 485-example.c&nbsp;: Basic example of RS-485 half duplex transmission =
<pre>/* 485-example.c&nbsp;: Basic example of RS485 half duplex transmission */
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;errno.h&gt;
#include &lt;termios.h&gt;
 
#include &lt;sys/ioctl.h&gt;
#include &lt;asm/ioctls.h&gt;
 
#include &lt;linux/rs485.h&gt;
 
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 &lt; 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, &amp;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, &amp;ti);
cfsetospeed(&amp;ti, speed);
cfsetispeed(&amp;ti, speed);
tcsetattr(fd, TCSANOW, &amp;ti);
 
/* Send content to RS485 */
if (write(fd, tosend, sizeof(tosend))&nbsp;!= sizeof(tosend)) {
printf("%s: write() failed\n", dev);
}
 
return 0;
}
 
</pre>
4,199
edits