⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seport.c

📁 功能丰富的串口通讯程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M. * Saggaf. All rights reserved. * * See the file COPYING (1-COPYING) or the manual page seyon(1) for a full * statement of rights and permissions for this program. *//*                               -*- Mode: C -*-  * SePort.c --- Modem port I/O routines * Author          : Muhammad M. Saggaf * Created On      : sometime in 1992 * Last Modified By: system admin * Last Modified On: Mon Jun 28 19:25:32 1993 * Update Count    : 10 * Status          : Mostly OK, needs some cleaning up */#include "config.h"#include <signal.h>#include <fcntl.h>#include <errno.h>#include <limits.h>#include <X11/Intrinsic.h>#if HAVE_TERMIOS#include <termios.h>#else#if HAVE_TERMIO#include <termio.h>#else#if HAVE_SGTTYB#include <sys/ioctl.h>#endif#endif#endif#if LF_USE_DEV_NUMBERS#include <sys/stat.h>#ifdef SVR4#include <sys/mkdev.h>#else#include <sys/sysmacros.h>#endif /* SVR4 */#endif /* LF_USE_DEV_NUMBERS */#if USE_NONSTD_BAUD#ifdef linux#include <linux/serial.h>#include <sys/ioctl.h>#include <linux/fs.h>#include <linux/tty.h>#endif#endif#include "seyon.h"#include "SeDecl.h"#if !defined(O_NDELAY) && defined(O_NONBLOCK)#define O_NDELAY O_NONBLOCK#endif#if !defined(MAX_INPUT) && defined(_POSIX_MAX_INPUT)#define MAX_INPUT _POSIX_MAX_INPUT#endifextern char TtyReadChar();extern int  TtyReadStr(),            TtyTimedReadChar(),            TtyReadLine(),            TtyTimedWaitFor(),            IoGetModemStat();extern speed_t  io_get_speed();/* * MDELAY is the delay in the output because (on my modem) the command would be * ignored if sent at full speed.  Change for other setups. (This setting is * for U.S. Robotics Password Modem). */#if HAVE_TERMIOSstatic struct termios pmode;	/* modem device control structure */#else#if HAVE_TERMIOstatic struct termio pmode;		/* modem device control structure */#else#if HAVE_SGTTYBstatic struct sgttyb pmode;#endif#endif#endifchar            modem_port[REG_BUF]; /* modem port device file string */static int      mfd = -1;		/* modem port file descriptor */int             baudrate = B9600; /* baud rate */voidMdmIFlush(){  int TtyIFlush();  TtyIFlush(mfd);}voidMdmOFlush(){  int TtyOFlush();  TtyOFlush(mfd);}voidMdmIOFlush(){  int TtyIOFlush();  TtyIOFlush(mfd);}voidsend_break(){  io_send_break(mfd);}voidMdmPutString(s)     char           *s;{  char            c;  usleep(MDELAY);  for (; (c = *s); s++) {    if (*s == '^' && *(s + 1))      if (*(++s) == '^') c = *s;      else c = *s & 0x1f;    if (c == '~') sleep(1);    else send_tbyte(c);    usleep(MDELAY);  }}voidmprintf(fmt, a, b, c)     char           *fmt,                    *a,                    *b,                    *c;{  char            buf[REG_BUF];  sprintf(buf, fmt, a, b, c);  MdmPutString(buf);}voidget_modem_attr(){  io_get_attr(mfd, &pmode);  baudrate = io_get_speed(&pmode);}voidset_modem_attr(){  io_set_speed(&pmode, baudrate);  io_set_attr(mfd, &pmode);}intget_modem_fio(){  return fcntl(mfd, F_GETFL);}voidset_modem_fio(fio)     int             fio;{  fcntl(mfd, F_SETFL, fio);}/*---------------------------------------------------------------------------+| MdmSaveRestoreAttr - saves or restores the modem attributes.+---------------------------------------------------------------------------*/intMdmSaveRestoreAttr(action)	 int action;{#if HAVE_TERMIOS  static struct termios savedAttr;#else#if HAVE_TERMIO  static struct termio savedAttr;#else#if HAVE_SGTTYB  static struct sgttyb savedAttr;#endif#endif#endif  if (mfd == -1) return -1;  if (action == ATTR_SAVE)	return io_get_attr(mfd, &savedAttr);  else if (action == ATTR_RESTORE) {	/* Not sure how to do this with sgttyb */#if HAVE_TERMIOS || HAVE_TERMIO	savedAttr.c_cflag &= ~HUPCL; /* don't hangup on closing */#endif	return io_set_attr(mfd, &savedAttr);  }  return -1;}intGetModemStat(newModem)	 int             newModem;{  static Boolean  useModemControl = True;  int             retStat;  if (newModem) useModemControl = True;  if (useModemControl && ((retStat = IoGetModemStat(mfd)) < 0)) {	SeErrorF("Could not get control line status for device %s", 			 modem_port, "", "");	SeNotice("Disabling status toggles for that device");	useModemControl = False;	qres.ignoreModemDCD = True;    PopupError("errModemControl", NULL);  }    return (useModemControl ?  retStat : 0);}intOnline(){  return((GetModemStat(0) & MDM_DCD) ? 1 : 0);}voidcancel_dial(verbose)     int             verbose;{  MdmPutString(qres.dialCancelString);  MdmPurge();  if (verbose)    SeyonMessage("Canceled");}voidSetInitialModemAttr(){#if HAVE_TERMIOS || HAVE_TERMIO  pmode.c_iflag |= (IGNBRK | IGNPAR);  pmode.c_iflag &= ~(ISTRIP | BRKINT);  pmode.c_lflag = 0;#ifdef XCLUDE  pmode.c_lflag |= XCLUDE;#endif  pmode.c_oflag &= ~OPOST;		/* transparent output */  pmode.c_cflag = baudrate | CREAD | CLOCAL;  /* this many characters satisfy reads */  pmode.c_cc[VMIN] = min(qres.modemVMin, MAX_INPUT);  pmode.c_cc[VTIME] = 1;		/* or in this many tenths of a second */#else#if HAVE_SGTTYB  pmode.sg_flags = RAW;#endif#endif  xc_setflow();  set_rtscts();}voidset_rtscts(){#ifdef CRTSCTS  if (qres.rtsctsFlowControl) {    pmode.c_cflag |= CRTSCTS;#ifdef CNORTSCTS    pmode.c_cflag &= ~CNORTSCTS;#endif  }  else {    pmode.c_cflag &= ~CRTSCTS;#ifdef CNORTSCTS    pmode.c_cflag |= CNORTSCTS;#endif  }#endif  if (mfd != -1)    io_set_attr(mfd, &pmode);}voidxc_setflow(){  if (qres.xonxoffFlowControl) {#if HAVE_TERMIOS    pmode.c_iflag |= IXON | IXOFF;#else#if HAVE_TERMIO    pmode.c_iflag |= IXON | IXOFF;    pmode.c_iflag &= ~IXANY;#else#if HAVE_SGTOBTYB    pmode.sg_flags |= TANDEM;#endif#endif#endif  }  else#if HAVE_TERMIOS    pmode.c_iflag &= ~(IXON | IXOFF);#else#if HAVE_TERMIO    pmode.c_iflag &= ~(IXON | IXOFF);    pmode.c_iflag &= ~IXANY;#else#if HAVE_SGTTYB    pmode.sg_flags &= ~TANDEM;#endif#endif#endif  if (mfd != -1)    io_set_attr(mfd, &pmode);}char           *mport(s)			/* get/set port string */     char           *s;{  if (s != NULL)    strcpy(modem_port, s);  return (modem_port);}intMdmSetGetBaud(baudIndex)     int             baudIndex;{  static char    *baud[] = {"0", "300", "1200", "2400", "4800", "9600", 							  "19200", "38400", "57600", "115200", NULL};  long            retBaud;  if (baudIndex != -1)    retBaud = mbaud(baud[baudIndex]);  else    retBaud = mbaud(NULL);  for (baudIndex = 0; baud[baudIndex]; baudIndex++)    if (atol(baud[baudIndex]) == retBaud)      return baudIndex;  return -1;}intMdmSetGetCSize(bits)     int             bits;{  if (bits != -1) {    switch (bits) {#if HAVE_TERMIOS || HAVE_TERMIO    case 5:      pmode.c_cflag &= ~CSIZE;      pmode.c_cflag |= CS5;	  MdmSetGetIStrip(1);      break;    case 6:      pmode.c_cflag &= ~CSIZE;      pmode.c_cflag |= CS6;	  MdmSetGetIStrip(1);      break;    case 7:      pmode.c_cflag &= ~CSIZE;      pmode.c_cflag |= CS7;	  MdmSetGetIStrip(1);      break;    case 8:      pmode.c_cflag &= ~CSIZE;      pmode.c_cflag |= CS8;	  MdmSetGetIStrip((int)qres.stripHighBit);      break;#else#if HAVE_SGTTYB    case 5:    case 6:    case 7:      pmode.sg_flags |= CBREAK;      pmode.sg_flags &= ~RAW;      MdmSetGetIStrip(0);      break;    case 8:      pmode.sg_flags |= RAW;      pmode.sg_flags &= ~CBREAK;      MdmSetGetIStrip(0);      break;#endif#endif    default:      SeErrorF("invalid number of bits: %d", bits, "", "");      return -1;    }	/* io_set_attr is called in  MdmSetGetIStrip */  }  if (mfd != -1)    io_get_attr(mfd, &pmode);#if HAVE_TERMIOS || HAVE_TERMIO  switch (pmode.c_cflag & CSIZE) {  case CS5:    return 5;  case CS6:    return 6;  case CS7:    return 7;  case CS8:    return 8;  default:    SeError("Consistency error in number of bits");    return -1;#else#if HAVE_SGTTYB  switch (pmode.sg_flags & CBREAK) {  case 0:    return 8;  default:    return 7;#endif#endif  }}intMdmSetGetParity(parity)     int             parity;{  if (parity != -1) {    switch (parity) {#if HAVE_TERMIOS || HAVE_TERMIO    case 0:      pmode.c_cflag &= ~PARENB;      break;    case 1:      pmode.c_cflag |= (PARENB | PARODD);      break;    case 2:      pmode.c_cflag |= PARENB;      pmode.c_cflag &= ~PARODD;      break;#else#if HAVE_SGTTYB    case 0:      pmode.sg_flags &= ~ANYP;      break;    case 1:      pmode.sg_flags |= ODDP;      pmode.sg_flags &= ~EVENP;      break;    case 2:      pmode.sg_flags |= EVENP;      pmode.sg_flags &= ~ODDP;      break;#endif#endif    default:      SeErrorF("Invalid parity: %d", parity, "", "");      return -1;    }    if (mfd != -1)      io_set_attr(mfd, &pmode);  }  if (mfd != -1)    io_get_attr(mfd, &pmode);#if HAVE_TERMIOS || HAVE_TERMIO  if (!(pmode.c_cflag & PARENB))    return 0;  else if (pmode.c_cflag & PARODD)    return 1;  else    return 2;#else#if HAVE_SGTTYB  switch (pmode.sg_flags & ANYP) {  case ODDP:    return 1;  case EVENP:    return 2;  default:    return 0;  }#endif#endif}intMdmSetGetIStrip(flag)     int             flag;{#if HAVE_TERMIOS || HAVE_TERMIO  if (flag != -1) {	if (flag)	  pmode.c_iflag |= ISTRIP;	else	  pmode.c_iflag &= ~ISTRIP;	    if (mfd != -1)      io_set_attr(mfd, &pmode);  }  if (mfd != -1)    io_get_attr(mfd, &pmode);  if (pmode.c_iflag & ISTRIP)    return 1;  else    return 0;#else#if !HAVE_SGTTYB  if (mfd != -1)    io_set_attr(mfd, &pmode);  return 0;#endif#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -