37 #include <sys/ioctl.h>
38 #include <sys/types.h>
53 struct termios options ;
59 case 50: myBaud = B50 ; break ;
60 case 75: myBaud = B75 ; break ;
61 case 110: myBaud = B110 ; break ;
62 case 134: myBaud = B134 ; break ;
63 case 150: myBaud = B150 ; break ;
64 case 200: myBaud = B200 ; break ;
65 case 300: myBaud = B300 ; break ;
66 case 600: myBaud = B600 ; break ;
67 case 1200: myBaud = B1200 ; break ;
68 case 1800: myBaud = B1800 ; break ;
69 case 2400: myBaud = B2400 ; break ;
70 case 9600: myBaud = B9600 ; break ;
71 case 19200: myBaud = B19200 ; break ;
72 case 38400: myBaud = B38400 ; break ;
73 case 57600: myBaud = B57600 ; break ;
74 case 115200: myBaud = B115200 ; break ;
75 case 230400: myBaud = B230400 ; break ;
81 if ((fd = open (
"/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
83 fprintf (stderr,
"Cannot open port serial port.\n") ;
86 fcntl (fd, F_SETFL, O_RDWR) ;
90 tcgetattr (fd, &options) ;
92 cfmakeraw (&options) ;
93 cfsetispeed (&options, myBaud) ;
94 cfsetospeed (&options, myBaud) ;
96 options.c_cflag |= (CLOCAL | CREAD) ;
97 options.c_cflag &= ~PARENB ;
98 options.c_cflag &= ~CSTOPB ;
99 options.c_cflag &= ~CSIZE ;
100 options.c_cflag |= CS8 ;
101 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
102 options.c_oflag &= ~OPOST ;
104 options.c_cc [VMIN] = 0 ;
105 options.c_cc [VTIME] = 100 ;
106 tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
108 ioctl (fd, TIOCMGET, &status);
110 status |= TIOCM_DTR ;
111 status |= TIOCM_RTS ;
113 ioctl (fd, TIOCMSET, &status);
127 tcflush (fd, TCIOFLUSH) ;
159 write (fd, s, strlen (s)) ;
173 va_start (argp, message) ;
174 vsnprintf (buffer, 1023, message, argp) ;
189 if (ioctl (fd, FIONREAD, &result) == -1)
205 if (read (fd, &x, 1) != 1)
208 return ((
int)x) & 0xFF ;
void SerialPuts(int fd, char *s)
Send a string to the serial port.
unsigned char buffer[256]
int SerialOpen(int baud)
Open and initialise the serial port with a specific baud rate.
void SerialClose(int fd)
Closes the serial port characterized by device number fd.
Serial port driver header.
void SerialPrintf(int fd, char const *message,...)
Printf over Serial Port.
void SerialPutchar(int fd, unsigned char c)
Send a single character to the serial port.
int SerialDataAvail(int fd)
Return the number of bytes of data avalable to be read from the serial port.
int SerialGetchar(int fd)
Get a single character from the serial device. Note: Zero is a valid character and this function will...
void SerialFlush(int fd)
Flush the serial buffers (both tx & rx)