kopia lustrzana https://github.com/Hamlib/Hamlib
non-serial parts of serial.[ch] moved to iofunc.[ch]
git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@876 7ae35d74-ebe9-4afe-98af-79ac388436b8Hamlib-1.1.3
rodzic
bc10e0eb97
commit
c3f9e8ec68
|
@ -1,6 +1,6 @@
|
|||
INCLUDES = @INCLUDES@ @INCLTDL@
|
||||
RIGSRC = rig.c serial.c misc.c register.c event.c cal.c conf.c tones.c \
|
||||
rotator.c locator.c rot_reg.c rot_conf.c
|
||||
rotator.c locator.c rot_reg.c rot_conf.c iofunc.c
|
||||
|
||||
lib_LTLIBRARIES = libhamlib.la
|
||||
libhamlib_la_SOURCES = $(RIGSRC)
|
||||
|
@ -8,5 +8,6 @@ libhamlib_la_LDFLAGS = -no-undefined -release @VERSION@ -version-info 0:0:0
|
|||
libhamlib_la_CFLAGS = -DIN_HAMLIB $(AM_CFLAGS)
|
||||
libhamlib_la_LIBADD = @LIBLTDL@ ../lib/libmisc.la -lm
|
||||
|
||||
noinst_HEADERS = event.h misc.h serial.h cal.h conf.h tones.h rot_conf.h
|
||||
noinst_HEADERS = event.h misc.h serial.h iofunc.c cal.h tones.h \
|
||||
conf.h rot_conf.h
|
||||
|
||||
|
|
294
src/serial.c
294
src/serial.c
|
@ -1,10 +1,10 @@
|
|||
/*
|
||||
* Hamlib Interface - serial communication low-level support
|
||||
* Copyright (c) 2000,2001 by Stephane Fillod and Frank Singleton
|
||||
* Copyright (c) 2000,2001,2002 by Stephane Fillod and Frank Singleton
|
||||
* Parts of the PTT handling are derived from soundmodem, an excellent
|
||||
* ham packet softmodem written by Thomas Sailer, HB9JNX.
|
||||
*
|
||||
* $Id: serial.c,v 1.22 2002-01-07 17:14:22 fgretief Exp $
|
||||
* $Id: serial.c,v 1.23 2002-01-16 22:58:50 fillods Exp $
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library General Public License as
|
||||
|
@ -339,92 +339,6 @@ int serial_open(port_t *rp) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* Write a block of count characters to file descriptor,
|
||||
* with a pause between each character if write_delay is > 0
|
||||
*
|
||||
* The write_delay is for Yaesu type rigs..require 5 character
|
||||
* sequence to be sent with 50-200msec between each char.
|
||||
*
|
||||
* Also, post_write_delay is for some Yaesu rigs (eg: FT747) that
|
||||
* get confused with sequential fast writes between cmd sequences.
|
||||
*
|
||||
*
|
||||
*
|
||||
* input:
|
||||
*
|
||||
* fd - file descriptor to write to
|
||||
* txbuffer - pointer to a command sequence array
|
||||
* count - count of byte to send from the txbuffer
|
||||
* write_delay - write delay in ms between 2 chars
|
||||
* post_write_delay - minimum delay between two writes
|
||||
* post_write_date - timeval of last write
|
||||
*
|
||||
* returns:
|
||||
*
|
||||
* 0 = OK
|
||||
* <0 = NOT OK
|
||||
*
|
||||
* Actually, this function has nothing specific to serial comm,
|
||||
* it could work very well also with any file handle, like a socket.
|
||||
*/
|
||||
|
||||
int write_block(port_t *p, const char *txbuffer, size_t count)
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifdef WANT_NON_ACTIVE_POST_WRITE_DELAY
|
||||
if (p->post_write_date.tv_sec != 0) {
|
||||
signed int date_delay; /* in us */
|
||||
struct timeval tv;
|
||||
|
||||
/* FIXME in Y2038 ... */
|
||||
gettimeofday(tv, NULL);
|
||||
date_delay = p->post_write_delay*1000 -
|
||||
((tv.tv_sec - p->post_write_date->tv_sec)*1000000 +
|
||||
(tv.tv_usec - p->post_write_date->tv_usec));
|
||||
if (date_delay > 0) {
|
||||
/*
|
||||
* optional delay after last write
|
||||
*/
|
||||
usleep(date_delay);
|
||||
}
|
||||
p->post_write_date.tv_sec = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (p->write_delay > 0) {
|
||||
for (i=0; i < count; i++) {
|
||||
if (write(p->fd, txbuffer+i, 1) < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR,"write_block() failed - %s\n",
|
||||
strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
usleep(p->write_delay*1000);
|
||||
}
|
||||
} else {
|
||||
write(p->fd, txbuffer, count);
|
||||
}
|
||||
|
||||
if (p->post_write_delay > 0) {
|
||||
#ifdef WANT_NON_ACTIVE_POST_WRITE_DELAY
|
||||
#define POST_WRITE_DELAY_TRSHLD 10
|
||||
|
||||
if (p->post_write_delay > POST_WRITE_DELAY_TRSHLD)
|
||||
gettimeofday(p->post_write_date, NULL);
|
||||
else
|
||||
#else
|
||||
usleep(p->post_write_delay*1000); /* optional delay after last write */
|
||||
/* otherwise some yaesu rigs get confused */
|
||||
/* with sequential fast writes*/
|
||||
#endif
|
||||
}
|
||||
rig_debug(RIG_DEBUG_TRACE,"TX %d bytes\n",count);
|
||||
dump_hex(txbuffer,count);
|
||||
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Flush all characters waiting in RX buffer.
|
||||
*/
|
||||
|
@ -436,210 +350,6 @@ int serial_flush( port_t *p )
|
|||
return RIG_OK;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Read "num" bytes from "fd" and put results into
|
||||
* an array of unsigned char pointed to by "rxbuffer"
|
||||
*
|
||||
* Blocks on read until timeout hits.
|
||||
*
|
||||
* It then reads "num" bytes into rxbuffer.
|
||||
*
|
||||
* Actually, this function has nothing specific to serial comm,
|
||||
* it could work very well also with any file handle, like a socket.
|
||||
*/
|
||||
|
||||
int read_block(port_t *p, char *rxbuffer, size_t count)
|
||||
{
|
||||
fd_set rfds;
|
||||
struct timeval tv, tv_timeout;
|
||||
int rd_count, total_count = 0;
|
||||
int retval;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(p->fd, &rfds);
|
||||
|
||||
/*
|
||||
* Wait up to timeout ms.
|
||||
*/
|
||||
tv_timeout.tv_sec = p->timeout/1000;
|
||||
tv_timeout.tv_usec = (p->timeout%1000)*1000;
|
||||
|
||||
while (count > 0) {
|
||||
tv = tv_timeout; /* select may have updated it */
|
||||
|
||||
retval = select(p->fd+1, &rfds, NULL, NULL, &tv);
|
||||
if (retval == 0) {
|
||||
rig_debug(RIG_DEBUG_WARN, "read_block: timedout after %d chars\n",
|
||||
total_count);
|
||||
return -RIG_ETIMEOUT;
|
||||
}
|
||||
if (retval < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR,"read_block: select error after %d chars: "
|
||||
"%s\n", total_count, strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
|
||||
/*
|
||||
* grab bytes from the rig
|
||||
* The file descriptor must have been set up non blocking.
|
||||
*/
|
||||
rd_count = read(p->fd, rxbuffer+total_count, count);
|
||||
if (rd_count < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n",
|
||||
strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
total_count += rd_count;
|
||||
count -= rd_count;
|
||||
}
|
||||
|
||||
rig_debug(RIG_DEBUG_TRACE,"RX %d bytes\n",total_count);
|
||||
dump_hex(rxbuffer, total_count);
|
||||
return total_count; /* return bytes count read */
|
||||
}
|
||||
|
||||
int fread_block(port_t *p, char *rxbuffer, size_t count)
|
||||
{
|
||||
fd_set rfds;
|
||||
struct timeval tv, tv_timeout;
|
||||
int rd_count, total_count = 0;
|
||||
int retval;
|
||||
int fd;
|
||||
|
||||
fd = fileno(p->stream);
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
|
||||
/*
|
||||
* Wait up to timeout ms.
|
||||
*/
|
||||
tv_timeout.tv_sec = p->timeout/1000;
|
||||
tv_timeout.tv_usec = (p->timeout%1000)*1000;
|
||||
|
||||
|
||||
/*
|
||||
* grab bytes from the rig
|
||||
* The file descriptor must have been set up non blocking.
|
||||
*/
|
||||
rd_count = fread(rxbuffer, 1, count, p->stream);
|
||||
if (rd_count < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n",
|
||||
strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
total_count += rd_count;
|
||||
count -= rd_count;
|
||||
|
||||
while (count > 0) {
|
||||
tv = tv_timeout; /* select may have updated it */
|
||||
|
||||
retval = select(fd+1, &rfds, NULL, NULL, &tv);
|
||||
if (retval == 0) {
|
||||
#if 0
|
||||
rig_debug(RIG_DEBUG_WARN, "fread_block: timedout after %d chars\n",
|
||||
total_count);
|
||||
#endif
|
||||
return -RIG_ETIMEOUT;
|
||||
}
|
||||
if (retval < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR,"fread_block: select error after %d chars: "
|
||||
"%s\n", total_count, strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
|
||||
/*
|
||||
* grab bytes from the rig
|
||||
* The file descriptor must have been set up non blocking.
|
||||
*/
|
||||
rd_count = fread(rxbuffer+total_count, 1, count, p->stream);
|
||||
if (rd_count < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n",
|
||||
strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
total_count += rd_count;
|
||||
count -= rd_count;
|
||||
}
|
||||
|
||||
rig_debug(RIG_DEBUG_TRACE,"RX %d bytes\n",total_count);
|
||||
dump_hex(rxbuffer, total_count);
|
||||
return total_count; /* return bytes count read */
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a string from "fd" and put result into
|
||||
* an array of unsigned char pointed to by "rxbuffer"
|
||||
*
|
||||
* Blocks on read until timeout hits.
|
||||
*
|
||||
* It then reads characters until one of the characters in
|
||||
* "stopset" is found, or until "rxmax-1" characters was copied
|
||||
* into rxbuffer. String termination character is added at the end.
|
||||
*
|
||||
* Actually, this function has nothing specific to serial comm,
|
||||
* it could work very well also with any file handle, like a socket.
|
||||
*
|
||||
* Assumes rxbuffer!=NULL
|
||||
*/
|
||||
int read_string(port_t *p, char *rxbuffer, size_t rxmax, const char *stopset)
|
||||
{
|
||||
fd_set rfds;
|
||||
struct timeval tv, tv_timeout;
|
||||
int rd_count, total_count = 0;
|
||||
int retval;
|
||||
|
||||
rxbuffer[0] = '\000';
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(p->fd, &rfds);
|
||||
|
||||
/*
|
||||
* Wait up to timeout ms.
|
||||
*/
|
||||
tv_timeout.tv_sec = p->timeout/1000;
|
||||
tv_timeout.tv_usec = (p->timeout%1000)*1000;
|
||||
|
||||
while (total_count < (rxmax-1)) {
|
||||
tv = tv_timeout; /* select may have updated it */
|
||||
|
||||
retval = select(p->fd+1, &rfds, NULL, NULL, &tv);
|
||||
if (retval == 0) /* Timed out */
|
||||
break;
|
||||
|
||||
if (retval < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR,__FUNCTION__": select error after %d chars:"
|
||||
" %s\n", total_count, strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
/*
|
||||
* read 1 character from the rig, (check if in stop set)
|
||||
* The file descriptor must have been set up non blocking.
|
||||
*/
|
||||
rd_count = read(p->fd, &rxbuffer[total_count], 1);
|
||||
if (rd_count < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, __FUNCTION__": read failed - %s\n",
|
||||
strerror(errno));
|
||||
return -RIG_EIO;
|
||||
}
|
||||
++total_count;
|
||||
if (stopset && strchr(stopset, rxbuffer[total_count-1]))
|
||||
break;
|
||||
/* Note: This function will always break on a '\000' character. */
|
||||
}
|
||||
rxbuffer[total_count] = '\000';
|
||||
|
||||
if (total_count == 0) {
|
||||
rig_debug(RIG_DEBUG_WARN, __FUNCTION__": timedout without reading a character\n");
|
||||
return -RIG_ETIMEOUT;
|
||||
}
|
||||
|
||||
rig_debug(RIG_DEBUG_TRACE,"RX %d characters\n",total_count);
|
||||
dump_hex(rxbuffer, total_count);
|
||||
return total_count; /* return bytes count read */
|
||||
}
|
||||
|
||||
/*
|
||||
* ser_ptt_set and par_ptt_set
|
||||
* ser_ptt_open/ser_ptt_close & par_ptt_open/par_ptt_close
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* Hamlib Interface - serial communication header
|
||||
* Copyright (c) 2000,2001 by Stephane Fillod and Frank Singleton
|
||||
*
|
||||
* $Id: serial.h,v 1.15 2002-01-07 17:14:22 fgretief Exp $
|
||||
* $Id: serial.h,v 1.16 2002-01-16 22:58:50 fillods Exp $
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library General Public License as
|
||||
|
@ -24,14 +24,11 @@
|
|||
#define _SERIAL_H 1
|
||||
|
||||
#include <hamlib/rig.h>
|
||||
#include "iofunc.h"
|
||||
|
||||
|
||||
extern HAMLIB_EXPORT(int) serial_open(port_t *rs);
|
||||
extern HAMLIB_EXPORT(int) read_block(port_t *p, char *rxbuffer, size_t count);
|
||||
extern HAMLIB_EXPORT(int) write_block(port_t *p, const char *txbuffer, size_t count);
|
||||
extern HAMLIB_EXPORT(int) fread_block(port_t *p, char *rxbuffer, size_t count);
|
||||
extern HAMLIB_EXPORT(int) serial_flush(port_t *p);
|
||||
extern HAMLIB_EXPORT(int) read_string(port_t *p, char *rxbuffer, size_t rxmax, const char *stopset);
|
||||
|
||||
/* Hamlib internal use, see rig.c */
|
||||
int ser_open(port_t *p);
|
||||
|
|
Ładowanie…
Reference in New Issue