[Kstars-devel] KDE/kdeedu/kstars/kstars/indi
Jasem Mutlaq
mutlaqja at ikarustech.com
Fri Apr 28 23:12:54 CEST 2006
SVN commit 535220 by mutlaqja:
Rearraning common RS232 communication functions in indicom for the next INDI release.
CCMAIL: kstars-devel at kde.org
M +169 -0 indicom.c
M +45 -0 indicom.h
--- trunk/KDE/kdeedu/kstars/kstars/indi/indicom.c #535219:535220
@@ -30,6 +30,8 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
#include "indicom.h"
@@ -636,3 +638,170 @@
return (ts);
}
+int tty_time_out(int fd, int timeout)
+{
+ struct timeval tv;
+ fd_set readout;
+ int retval;
+
+ FD_ZERO(&readout);
+ FD_SET(fd, &readout);
+
+ /* wait for 'timeout' seconds */
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ /* Wait till we have a change in the fd status */
+ retval = select (fd+1, &readout, NULL, NULL, &tv);
+
+ /* Return 0 on successful fd change */
+ if (retval > 0)
+ return TTY_NO_ERROR;
+ /* Return -1 due to an error */
+ else if (retval == -1)
+ return TTY_SELECT_ERROR;
+ /* Return -2 if time expires before anything interesting happens */
+ else
+ return TTY_TIME_OUT;
+
+}
+
+int tty_write(int fd, const char * buf, int *nbytes_written)
+{
+ unsigned int nbytes;
+ int totalBytesWritten;
+ int bytesWritten = 0;
+
+ nbytes = totalBytesWritten = strlen(buf);
+
+ while (nbytes > 0)
+ {
+
+ bytesWritten = write(fd, buf, nbytes);
+
+ if (bytesWritten < 0)
+ return TTY_WRITE_ERROR;
+
+ buf += bytesWritten;
+ nbytes -= bytesWritten;
+ }
+
+ /* Returns the # of bytes written */
+ *nbytes_written = totalBytesWritten;
+ return TTY_NO_ERROR;
+}
+
+int tty_read(int fd, char *buf, int nbytes, char stop_char, int timeout, int *nbytes_read)
+{
+
+ int bytesRead = 0;
+ int totalBytesRead = 0;
+ int err = 0;
+
+ /* Loop until encountring the stop_char */
+ if (nbytes == -1)
+ {
+ for (;;)
+ {
+ if ( (err = tty_time_out(fd, timeout)) )
+ return err;
+
+ bytesRead = read(fd, buf, 1);
+
+ if (bytesRead < 0 )
+ return TTY_READ_ERROR;
+
+ if (bytesRead)
+ totalBytesRead++;
+
+ if (*buf == stop_char)
+ {
+ *nbytes_read = totalBytesRead;
+ return TTY_NO_ERROR;
+ }
+
+ buf += bytesRead;
+ }
+ }
+
+ while (nbytes > 0)
+ {
+ if ( (err = tty_time_out(fd, timeout)) )
+ return err;
+
+ bytesRead = read(fd, buf, ((unsigned) nbytes));
+
+ if (bytesRead < 0 )
+ return TTY_READ_ERROR;
+
+ buf += bytesRead;
+ totalBytesRead++;
+ nbytes -= bytesRead;
+ }
+
+ *nbytes_read = totalBytesRead;
+ return TTY_NO_ERROR;
+}
+
+int tty_connect(const char *device, struct termios *ttyOptions, int *fd)
+{
+ /*IDLog("Connecting to device %s\n", device);*/
+ int t_fd=0, is_null=0;
+
+ if (ttyOptions == NULL)
+ {
+ ttyOptions = (struct termios *) malloc(sizeof(struct termios));
+ is_null = 1;
+ }
+
+ if ( (t_fd = open(device, O_RDWR)) == -1)
+ return TTY_PORT_FAILURE;
+
+ memset(ttyOptions, 0, sizeof(struct termios));
+ tcgetattr(t_fd, ttyOptions);
+
+ /* Control options
+ charecter size */
+ ttyOptions->c_cflag &= ~CSIZE;
+ /* 8 bit, enable read */
+ ttyOptions->c_cflag |= CREAD | CLOCAL | CS8;
+ /* no parity */
+ ttyOptions->c_cflag &= ~PARENB;
+
+ /* set baud rate */
+ cfsetispeed(ttyOptions, B9600);
+ cfsetospeed(ttyOptions, B9600);
+
+ /* set input/output flags */
+ ttyOptions->c_iflag = IGNBRK;
+ /* no software flow control */
+ ttyOptions->c_iflag &= ~(IXON|IXOFF|IXANY);
+
+ /* Read at least one byte */
+ ttyOptions->c_cc[VMIN] = 1;
+ ttyOptions->c_cc[VTIME] = 5;
+
+ /* Misc. */
+ ttyOptions->c_lflag = 0;
+ ttyOptions->c_oflag = 0;
+
+ /* set attributes */
+ tcsetattr(t_fd, TCSANOW, ttyOptions);
+
+ /* flush the channel */
+ tcflush(t_fd, TCIOFLUSH);
+ /* N.B. user responsible for freeing up his structure */
+ if (is_null) free(ttyOptions);
+
+ *fd = t_fd;
+ /* return success */
+ return TTY_NO_ERROR;
+}
+
+void tty_disconnect(int fd)
+{
+ /*IDLog("Disconnected.\n");*/
+ tcflush(fd, TCIOFLUSH);
+ close(fd);
+}
+
--- trunk/KDE/kdeedu/kstars/kstars/indi/indicom.h #535219:535220
@@ -45,6 +45,7 @@
#define INDICOM_H
#include <time.h>
+#include <termios.h>
#define J2000 2451545.0
#define ERRMSG_SIZE 1024
@@ -52,6 +53,9 @@
extern const char * Direction[];
extern const char * SolarSystem[];
+/* TTY Error Codes */
+enum TTY_ERROR { TTY_NO_ERROR, TTY_READ_ERROR, TTY_WRITE_ERROR, TTY_SELECT_ERROR, TTY_TIME_OUT, TTY_PORT_FAILURE };
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -85,6 +89,47 @@
/*@}*/
/**
+ * \defgroup ttyFunctions TTY Functions: Functions to perform common terminal access routines.
+*/
+
+/*@{*/
+
+/** \brief read buffer from terminal
+ \param fd file descriptor
+ \param buf pointer to store data. Must be initilized and big enough to hold data.
+ \param nbytes number of bytes to read.
+ \param stop_char if the function encounters \e stop_char then it stops reading and returns. The function only searches for stop_char when \e nbytes is -1
+ \param timeout number of seconds to wait for terminal before a timeout error is issued.
+ \param nbytes_read the number of bytes read.
+ \return On success, it returns TTY_NO_ERROR, otherwise, a TTY_ERROR code.
+*/
+int tty_read(int fd, char *buf, int nbytes, char stop_char, int timeout, int *nbytes_read);
+
+/** \brief Writes a buffer to fd.
+ \param fd file descriptor
+ \param buffer the buffer to write to fd.
+ \param nbytes_written the number of bytes written
+ \return On success, it returns TTY_NO_ERROR, otherwise, a TTY_ERROR code.
+*/
+int tty_write(int fd, const char * buffer, int *nbytes_written);
+
+/** \brief Establishes a tty connection to a terminal device.
+ \param device the device node. e.g. /dev/ttyS0
+ \param ttyOptions pointer to desired tty connection option. Set to NULL for default options.
+ \param fd The function will fill \e fd with the file descriptor value on success.
+ \return On success, it returns TTY_NO_ERROR, otherwise, a TTY_ERROR code.
+*/
+int tty_connect(const char *device, struct termios *ttyOptions, int *fd);
+
+/** \brief Closes a tty connection and flushes the bus.
+ \param fd the file descriptor to close.
+*/
+void tty_disconnect(int fd);
+
+int tty_timeout(int fd, int timeout);
+/*@}*/
+
+/**
* \defgroup ephemerisFunctions Ephemeris Functions: Functions to perform common ephemeris routines.
The ephemeris functions are date-dependent. Call updateAstroValues() to update orbital values used in many algorithms before using any ephemeris function. You only need to call updateAstroValues() again if you need to update the orbital values for a different date.
More information about the Kstars-devel
mailing list