Project

General

Profile

RE: Write UART2 RS-485 API » send.c

RS485 send example code. - Michael Williamson, 03/27/2012 08:18 AM

 
1
#include <linux/serial.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <fcntl.h>
5
#include <termios.h>
6

    
7
/* Driver-specific ioctls: */
8
#define TIOCGRS485 0x542E
9
#define TIOCSRS485 0x542F
10

    
11
/* new defines in linux/serial.h of CL provided kernel */
12
#ifndef SER_RS485_USE_GPIO
13
#define SER_RS485_USE_GPIO (1<<4)
14
#define gpio_pin padding[0]
15
#endif
16

    
17
#define GPIO8_1 (16*8+1)
18

    
19
#define USE2
20

    
21
/*
22
 * Test program for 485 control
23
 */
24
int main(int argc, char* argv[])
25
{
26
	struct serial_rs485 rs485conf;
27
	char buffer[20], buffer2[21];
28
	int i, rv, opt;
29
	struct termios my_termios, new_termios;
30

    
31
	int fd0 = open("/dev/ttyS0", O_RDWR | O_NDELAY | O_NOCTTY);
32

    
33
	rs485conf.flags = SER_RS485_ENABLED;
34
	rs485conf.flags |= SER_RS485_USE_GPIO;
35
	rs485conf.delay_rts_before_send = 0;
36
	rs485conf.gpio_pin = GPIO8_1;
37
	rv = ioctl (fd0, TIOCSRS485, &rs485conf);
38
	if (rv) {
39
		perror("unable to set IOCTL:");
40
	}
41

    
42
	tcgetattr(fd0, &my_termios);
43
	my_termios.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
44
	my_termios.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OLCUC | OPOST);
45
	my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG | ECHOK | ECHOCTL | ECHOPRT);
46
	my_termios.c_cflag &= ~(CSIZE | PARENB);
47
	my_termios.c_cflag &= ~CRTSCTS;
48
	my_termios.c_cflag |= CS8 | CLOCAL;
49
	my_termios.c_cflag &= ~CBAUD;
50
	my_termios.c_cflag |= B19200;
51
	my_termios.c_cc[VMIN] = 1;
52
	my_termios.c_cc[VTIME] = 0;
53
	rv = tcsetattr(fd0, TCSANOW, &my_termios);
54
	tcgetattr(fd0, &new_termios);
55

    
56
	for (i = 0; i < 20; i++) {
57
		buffer[i] = 'A'+i;
58
	}
59

    
60
	for (i = 0; i < 30; i++) {
61
		printf("Writing [%d]\n", i);
62
		write(fd0, buffer, 20);
63
		sleep(1);
64
	}
65
	close(fd0);
66
}
(2-2/4) Go to top
Add picture from clipboard (Maximum size: 1 GB)