Sensorian  1.0
C API Reference Guide Library
Serial.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <termios.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include "Serial.h"
41 
45 
51 int SerialOpen (int baud)
52 {
53  struct termios options ;
54  speed_t myBaud ;
55  int status, fd ;
56 
57  switch (baud)
58  {
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 ;
76 
77  default:
78  return -2 ;
79  }
80 
81  if ((fd = open ("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
82  {
83  fprintf (stderr, "Cannot open port serial port.\n") ;
84  }
85 
86  fcntl (fd, F_SETFL, O_RDWR) ;
87 
88 // Get and modify current options:
89 
90  tcgetattr (fd, &options) ;
91 
92  cfmakeraw (&options) ;
93  cfsetispeed (&options, myBaud) ;
94  cfsetospeed (&options, myBaud) ;
95 
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 ;
103 
104  options.c_cc [VMIN] = 0 ;
105  options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
106  tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
107 
108  ioctl (fd, TIOCMGET, &status);
109 
110  status |= TIOCM_DTR ;
111  status |= TIOCM_RTS ;
112 
113  ioctl (fd, TIOCMSET, &status);
114 
115  usleep (10000) ; // 10mS
116 
117  return fd ;
118 }
119 
125 void SerialFlush (int fd)
126 {
127  tcflush (fd, TCIOFLUSH) ;
128 }
129 
135 void SerialClose (int fd)
136 {
137  close (fd) ;
138 }
139 
146 void SerialPutchar (int fd, unsigned char c)
147 {
148  write (fd, &c, 1) ;
149 }
150 
157 void SerialPuts (int fd, char *s)
158 {
159  write (fd, s, strlen (s)) ;
160 }
161 
168 void SerialPrintf (int fd, char const *message, ...)
169 {
170  va_list argp ;
171  char buffer [1024] ;
172 
173  va_start (argp, message) ;
174  vsnprintf (buffer, 1023, message, argp) ;
175  va_end (argp) ;
176 
177  SerialPuts (fd, buffer) ;
178 }
179 
185 int SerialDataAvail (int fd)
186 {
187  int result ;
188 
189  if (ioctl (fd, FIONREAD, &result) == -1)
190  return -1 ;
191 
192  return result ;
193 }
194 
201 int SerialGetchar (int fd)
202 {
203  uint8_t x ;
204 
205  if (read (fd, &x, 1) != 1)
206  return -1 ;
207 
208  return ((int)x) & 0xFF ;
209 }
210 
211 /// @}
void SerialPuts(int fd, char *s)
Send a string to the serial port.
Definition: Serial.c:157
unsigned char buffer[256]
Definition: main.c:74
int SerialOpen(int baud)
Open and initialise the serial port with a specific baud rate.
Definition: Serial.c:51
void SerialClose(int fd)
Closes the serial port characterized by device number fd.
Definition: Serial.c:135
void SerialPrintf(int fd, char const *message,...)
Printf over Serial Port.
Definition: Serial.c:168
void SerialPutchar(int fd, unsigned char c)
Send a single character to the serial port.
Definition: Serial.c:146
int SerialDataAvail(int fd)
Return the number of bytes of data avalable to be read from the serial port.
Definition: Serial.c:185
int SerialGetchar(int fd)
Get a single character from the serial device. Note: Zero is a valid character and this function will...
Definition: Serial.c:201
Serial port driver header.
void SerialFlush(int fd)
Flush the serial buffers (both tx & rx)
Definition: Serial.c:125