001package com.pi4j.wiringpi;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  Serial.java  
009 * 
010 * This file is part of the Pi4J project. More information about 
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2015 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 * 
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 * 
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.util.NativeLibraryLoader;
034
035/**
036 * <p>
037 * WiringPi includes a simplified serial port handling library. It can use the on-board serial port,
038 * or any USB serial device with no special distinctions between them. You just specify the device
039 * name in the initial open function.
040 * </p>
041 * 
042 * <p>
043 * Note: The file descriptor (fd) returned is a standard Linux filehandle. You can use the standard
044 * read(), write(), etc. system calls on this filehandle as required. E.g. you may wish to write a
045 * larger block of binary data where the serialPutchar() or serialPuts() function may not be the
046 * most appropriate function to use, in which case, you can use write() to send the data.
047 * </p>
048 * 
049 * <p>
050 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
051 * the following system libraries:
052 * <ul>
053 * <li>pi4j</li>
054 * <li>wiringPi</li>
055 * </ul>
056 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
057 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
058 * </blockquote>
059 * </p>
060 * 
061 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
062 * @see <a
063 *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
064 * @author Robert Savage (<a
065 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
066 */
067public class Serial {
068
069    /**
070     * The default hardware COM port provided via the Raspberry Pi GPIO header.
071     * 
072     * @see #serialOpen(String,int)
073     */
074    public static final String DEFAULT_COM_PORT = "/dev/ttyAMA0";
075
076    // private constructor 
077    private Serial() {
078        // forbid object construction 
079    }
080    
081    static {
082        // Load the platform library
083        NativeLibraryLoader.load("libpi4j.so");
084    }
085
086    /**
087     * <p>int serialOpen (char *device, int baud);</p>
088     * 
089     * <p>
090     * This opens and initializes the serial device and sets the baud rate. It sets the port into
091     * raw mode (character at a time and no translations), and sets the read timeout to 10 seconds.
092     * The return value is the file descriptor or -1 for any error, in which case errno will be set
093     * as appropriate.
094     * </p>
095     * 
096     * <p>
097     * (ATTENTION: the 'device' argument can only be a maximum of 128 characters.)
098     * </p>
099     * 
100     * @see #DEFAULT_COM_PORT
101     * @see <a
102     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
103     * 
104     * @param device The device address of the serial port to access. You can use constant
105     *            'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the
106     *            GPIO header.
107     * @param baud The baud rate to use with the serial port.
108     * @return The return value is the file descriptor or -1 for any error, in which case errno will
109     *         be set as appropriate.
110     */
111    public static native int serialOpen(String device, int baud);
112
113    /**
114     * <p>void serialClose (int fd);</p>
115     * 
116     * <p>
117     * Closes the device identified by the file descriptor given.
118     * </p>
119     * @see <a
120     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
121     * @param fd <p>
122     *            The file descriptor of the serial port.
123     *            </p>
124     */
125    public static native void serialClose(int fd);
126
127    /**
128     * <h1>void serialFlush (int fd);</h1>
129     * 
130     * <p>This discards all data received, or waiting to be send down the given device.</p>
131     *
132     * @see <a
133     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
134     * @param fd The file descriptor of the serial port.
135     */
136    public static native void serialFlush(int fd);
137
138    /**
139     * <p>void serialPutchar (int fd, unsigned char c);</p>
140     * 
141     * <p>Sends the single byte to the serial device identified by the given file descriptor.</p>
142     *
143     * @see <a
144     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
145     * @param fd The file descriptor of the serial port.
146     * @param data The character to transmit to the serial port.
147     */
148    public static native void serialPutchar(int fd, char data);
149
150    /**
151     * <p>void serialPuts (int fd, char *s);</p>
152     * 
153     * <p>Sends the nul-terminated string to the serial device identified by the given file descriptor.</p>
154     * 
155     * <p>(ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)</p>
156     *
157     * @see <a
158     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
159     * @param fd The file descriptor of the serial port.
160     * @param data The data string to transmit to the serial port.
161     */
162    public static native void serialPuts(int fd, String data);
163
164    /**
165     * <p>void serialPuts (int fd, String data, String...arguments);</p>
166     * 
167     * <p>
168     * Sends the nul-terminated formatted string to the serial device identified by the given file
169     * descriptor.
170     * </p>
171     * 
172     * <p>(ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)</p>
173     *
174     * @see <a
175     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
176     * @param fd The file descriptor of the serial port.
177     * @param data The formatted data string to transmit to the serial port.
178     * @param args Arguments to the format string.
179     */
180    public static void serialPuts(int fd, String data, String... args) {
181        serialPuts(fd, String.format(data, (Object[]) args));
182    }
183
184    /**
185     * <p>int serialDataAvail (int fd);</p>
186     * 
187     * Returns the number of characters available for reading, or -1 for any error condition, in
188     * which case errno will be set appropriately.
189     *
190     * @see <a
191     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
192     * @param fd The file descriptor of the serial port.
193     * @return Returns the number of characters available for reading, or -1 for any error
194     *         condition, in which case errno will be set appropriately.
195     */
196    public static native int serialDataAvail(int fd);
197
198    /**
199     * <p>int serialGetchar (int fd);</p>
200     * 
201     * <p>Returns the next character available on the serial device. This call will block for up to 10
202     * seconds if no data is available (when it will return -1)</p>
203     *
204     * @see <a
205     *      href="http://wiringpi.com/reference/serial-library/">http://wiringpi.com/reference/serial-library/</a>
206     * @param fd The file descriptor of the serial port.
207     * @return Returns the next character available on the serial device. This call will block for
208     *         up to 10 seconds if no data is available (when it will return -1)
209     */
210    public static native int serialGetchar(int fd);
211}