Project

General

Profile

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

RS485 receive 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
#include <unistd.h>
7
#include <string.h>
8

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

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

    
19
#define GPIO0_9 (9)
20

    
21
#define USE2
22

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

    
33
	int fd2 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
34

    
35
	rs485conf.flags = SER_RS485_ENABLED;
36
	rs485conf.flags |= SER_RS485_USE_GPIO;
37
	rs485conf.delay_rts_before_send = 0;
38
	rs485conf.gpio_pin = GPIO0_9;
39

    
40
	rv = ioctl (fd2, TIOCSRS485, &rs485conf);
41
	if (rv) {
42
		perror("unable to set IOCTL:");
43
	}
44

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

    
59
	fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
60

    
61
	printf("Reading (hit q, return to quit)...\n");
62
	while(1)
63
	{
64
		if(read(fd2, buffer2, 1) > 0) write(STDOUT_FILENO, buffer2, 1);
65
		if(read(STDIN_FILENO, buffer2, 1) > 0) {
66
			if (buffer2[0]='q') {
67
				break;
68
			}
69
		}
70
	}
71
	close(fd2);
72
}
(1-1/4) Go to top
Add picture from clipboard (Maximum size: 1 GB)