From d5a340429d368584196cd03885c47be4055e52d8 Mon Sep 17 00:00:00 2001 From: "Frank Singleton, VK3FCS" Date: Tue, 25 Jul 2000 01:01:00 +0000 Subject: [PATCH] initial import git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@15 7ae35d74-ebe9-4afe-98af-79ac388436b8 --- common/Makefile | 34 ++++++++++ common/serial.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++ common/serial.h | 15 +++++ 3 files changed, 215 insertions(+) create mode 100644 common/Makefile create mode 100644 common/serial.c create mode 100644 common/serial.h diff --git a/common/Makefile b/common/Makefile new file mode 100644 index 000000000..48b115584 --- /dev/null +++ b/common/Makefile @@ -0,0 +1,34 @@ +# hamlib - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) +# +# +# Simple Make file for common parts. +# +# +# $Id: Makefile,v 1.1 2000-07-25 01:01:00 javabear Exp $ +# +# + +CC = gcc +INCLUDE = -I/usr/include -I/usr/local/include -I. -I../include +OBJ = serial.o +LDFLAG = -lm +CFLAGS = -g -Wall -ansi -pedantic -O2 -D__USE_FIXED_PROTOTYPES__ +MAKEALL = serial.o + +all: $(MAKEALL) + +serial.o: serial.c serial.h + $(CC) $(CFLAGS) $(INCLUDE) -c serial.c + +# clean up + +clean: + make cleanlocal + +# clean up local directory + +.PHONY: cleanlocal +cleanlocal: + rm -f *.o serial + + diff --git a/common/serial.c b/common/serial.c new file mode 100644 index 000000000..45576f98c --- /dev/null +++ b/common/serial.c @@ -0,0 +1,166 @@ +/* + * hamlib - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) + * + * serial.c - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) + * Provides useful routines for read/write serial data for communicating + * via serial interface . + * + * $Id: serial.c,v 1.1 2000-07-25 01:01:00 javabear Exp $ + * + */ + +#include +#include /* Standard input/output definitions */ +#include /* String function definitions */ +#include /* UNIX standard function definitions */ +#include /* File control definitions */ +#include /* Error number definitions */ +#include /* POSIX terminal control definitions */ +#include +#include "serial.h" + + +/* + * Open serial port + * + * Set to 4800 8N2 + * + * input: + * + * serial_port - ptr to a char (string) indicating port + * to open (eg: "/dev/ttyS1"). + * + * returns: + * + * fd - the file descriptor on success or -1 on error. + * + */ + +int open_port(char *serial_port) { + + int fd; /* File descriptor for the port */ + struct termios options; + + + fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY); + + if (fd == -1) { + + /* Could not open the port. */ + + printf("open_port: Unable to open %s - ",serial_port); + return -1; /* bad */ + } + + fcntl(fd, F_SETFL, 0); /* */ + + + /* + * Get the current options for the port... + */ + + tcgetattr(fd, &options); + + /* + * Set the baud rates to 4800... + */ + + cfsetispeed(&options, B4800); + cfsetospeed(&options, B4800); + + /* + * Enable the receiver and set local mode... + */ + + options.c_cflag |= (CLOCAL | CREAD); + + /* + * Set 8N2 + */ + + options.c_cflag &= ~PARENB; + options.c_cflag |= CSTOPB; + options.c_cflag &= ~CSIZE; + options.c_cflag |= CS8; + + /* + * Chose raw input, no preprocessing please .. + */ + + options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + + /* + * Chose raw output, no preprocessing please .. + */ + + options.c_oflag &= ~OPOST; + + /* + * Set the new options for the port... + */ + + tcsetattr(fd, TCSANOW, &options); + + return (fd); +} + +/* + * 'close_port()' - Close serial port + * + * fd - file descriptor for open port + * + * + * Returns success (0) or -1 on error. + */ + +int close_port(int fd) { + + if (close(fd) <0 ) { + printf("close_port: Unable to close port using fd %i - ",fd); + return -1; /* oops */ + } + return 0; /* ok */ +} + + +/* + * Provides delay for block formation. + * Should be 50-200 msec according to YAESU docs. + */ + +void pause2() { + usleep(50 * 1000); /* 50 msec */ + return; +} + +/* + * Write a 5 character block to a file descriptor, + * with a pause between each character. + * + * This is for Yaesu type rigs..require 5 character + * sequence to bsent with 50-200msec between each char. + * + * input: + * + * fd - file descriptor to write to + * data - pointer to a command sequence array + * + * returns: + * + * 0 = OK + * -1 = NOT OK + * + */ + +int write_block(int fd, unsigned char *data) { + int i; + + for (i=0; i<5; i++) { + if(write(fd, &data[i] , 1) < 0) { + fputs("write() of byte failed!\n", stderr); + return -1; + } + pause2(); /* 50 msec */ + } + return 0; +} diff --git a/common/serial.h b/common/serial.h new file mode 100644 index 000000000..88d48f2f2 --- /dev/null +++ b/common/serial.h @@ -0,0 +1,15 @@ +/* + * hamlib - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) + * + * serial.h - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) + * Provides useful routines for read/write serial data for communicating + * via serial interface . + * + * $Id: serial.h,v 1.1 2000-07-25 01:01:00 javabear Exp $ + */ + + +int open_port(char *serial_port); +int write_block(int fd, unsigned char *data); +int close_port(int fd); +