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

📄 combase.c

📁 this program is the application of serial port
💻 C
字号:
// *********************************************************
// 模块编号: POS LINUX 007
// 模块名称: COMBASE.C 
// 功能描述: 通讯底层函数
// 作者∶     卢旭朝
// 公司:     杭州先创
// 日期∶     2003.11.06 
// 版本号∶   V1.00
// 修改历史∶
//
// *********************************************************
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/select.h>
#include <unistd.h>

int ngModem = -1;


// *******************************************************************
// Function  Name   : InitTTY
// Function         : Open the serial port(strTtyName) and set the attribte
// Parameter        : strTtyName: the tty name (ttyS1 for GPRS modem ttyS0 for com1)
//                  : nBaudRate: the baudrate will be set.
// Return           : on success return The device id
//                  : on error: return < 0
// *******************************************************************
int OpenTTY(char *strTtyName, int nBaudRate)
{
  int              status;
  struct termios   options;

  if (ngModem > 0) return ngModem;

  if ((ngModem = open(strTtyName, O_RDWR)) == -1) {
    return (-1);
  } 
  tcflush(ngModem,TCIOFLUSH);
  tcgetattr(ngModem, &options); 
  switch (nBaudRate) {
    case 1200:
      cfsetispeed(&options,B1200);
      cfsetospeed(&options,B1200);
      break;
    case 2400:
      cfsetispeed(&options,B2400);
      cfsetospeed(&options,B2400);
      break;
    case 4800:
      cfsetispeed(&options,B4800);
      cfsetospeed(&options,B4800);
      break;
    case 9600:
      cfsetispeed(&options,B9600);  //GPRS module need 9600
      cfsetospeed(&options,B9600);
      break;
    case 19200:
      cfsetispeed(&options,B19200);
      cfsetospeed(&options,B19200);
      break;
    case 38400:
      cfsetispeed(&options,B38400);
      cfsetospeed(&options,B38400);
      break;
    case 115200:
      cfsetispeed(&options,B115200);
      cfsetospeed(&options,B115200);
      break;     
    default:
      break;
  }
  options.c_cflag &= ~CSIZE;   
  options.c_cflag |= CS8;
  options.c_cflag &= ~PARENB;
  options.c_iflag &= ~INPCK; 
  options.c_cflag &= ~CSTOPB;  
  options.c_iflag &= ~IXON;  
	
  options.c_cc[VTIME]=3; 
  options.c_cc[VMIN] =200;

  options.c_iflag&=~ICRNL;
  options.c_iflag&=~IGNCR;
  options.c_lflag&=~ICANON;
  options.c_cflag&=~CRTSCTS;
  options.c_cflag|=(CLOCAL|CREAD); //???
//  options.c_cflag|=(CRTSCTS|CREAD);  //???
//  options.c_cflag&=~CLOCAL;         //???

  options.c_lflag &= ~(ECHO|ECHOE|ISIG);
  options.c_oflag &= ~OPOST;

  status = tcsetattr(ngModem,TCSANOW,&options);  
  if(status!=0) {        
    close(ngModem);
    ngModem = -1;
    return -2;     
  }    

fprintf(stderr, "before tcflush111\n");

  tcflush(ngModem,TCIOFLUSH);   
  return (ngModem);
}


int OpenTTY1(char *strTtyName, int nBaudRate)
{
  int              status;
  struct termios   options;

  if (ngModem > 0) return ngModem;

  if ((ngModem = open(strTtyName, O_RDWR)) == -1) {
    return (-1);
  } 
  tcflush(ngModem,TCIOFLUSH);
  tcgetattr(ngModem, &options); 
  switch (nBaudRate) {
    case 1200:
      cfsetispeed(&options,B1200);
      cfsetospeed(&options,B1200);
      break;
    case 2400:
      cfsetispeed(&options,B2400);
      cfsetospeed(&options,B2400);
      break;
    case 4800:
      cfsetispeed(&options,B4800);
      cfsetospeed(&options,B4800);
      break;
    case 9600:
      cfsetispeed(&options,B9600);  //GPRS module need 9600
      cfsetospeed(&options,B9600);
      break;
    case 19200:
      cfsetispeed(&options,B19200);
      cfsetospeed(&options,B19200);
      break;
    case 38400:
      cfsetispeed(&options,B38400);
      cfsetospeed(&options,B38400);
      break;
    case 115200:
      cfsetispeed(&options,B115200);
      cfsetospeed(&options,B115200);
      break;     
    default:
      break;
  }
  options.c_cflag &= ~CSIZE;   
  options.c_cflag |= CS8;
  options.c_cflag &= ~PARENB;
  options.c_iflag &= ~INPCK; 
  options.c_cflag &= ~CSTOPB;  
  options.c_iflag &= ~IXON;  
	
  options.c_cc[VTIME]=3; 
  options.c_cc[VMIN] =200;

  options.c_iflag&=~ICRNL;
  options.c_iflag&=~IGNCR;
  options.c_lflag&=~ICANON;
//  options.c_cflag|=(CLOCAL|CREAD); //???
//  options.c_cflag &= ~CLOCAL;  
  options.c_cflag |= CLOCAL;  
  options.c_cflag|=(CRTSCTS|CREAD);  //???
//  options.c_cflag&=~CLOCAL;         //???

  options.c_lflag &= ~(ECHO|ECHOE|ISIG);
  options.c_oflag &= ~OPOST;

  status = tcsetattr(ngModem,TCSANOW,&options);  
  if(status!=0) {        
    close(ngModem);
    ngModem = -1;
    return -2;     
  }    

fprintf(stderr, "before tcflush222\n");
  tcflush(ngModem,TCIOFLUSH);   
  return (ngModem);
}


// *******************************************************************
// Function  Name   : CloseTTY
// Function         : Close the tty
// Parameter        : 
// Return           : on success return 0
// *******************************************************************
int CloseTTY(void)
{
  if (ngModem < 0) return 0;

  tcflush(ngModem, TCIOFLUSH);
  close(ngModem);
  ngModem = -1;
  return 0;
}

// *******************************************************************
// Function  Name   : ReadTTY
// Function         : read data from ngModem, the wait time is "timeout"(in seconds)
// Parameter        : strBuf: the buffer to receive the data
//                  : nByte: want read nByte from the device
//                  : nTimeOut: at most wait time
// Return           : on success return the read bytes
//                  : on error: return < 0
// *******************************************************************
int  ReadTTY(unsigned char *strBuf, int nByte, int nTimeOut)
{
  int  nRead, nRet;
  int  nLeft;
  fd_set rfds;
  struct timeval tv;

  FD_ZERO(&rfds);
  FD_SET(ngModem, &rfds);
  tv.tv_sec = nTimeOut;
  tv.tv_usec = 0;

  nLeft = nByte;

  while (nLeft > 0) {

    nRet = select(ngModem+1, &rfds, NULL, NULL, &tv);
    if (nRet > 0) {
      nRead = read(ngModem, strBuf, nLeft);
      if (nRead < 0) return (nRead);
      nLeft -= nRead;
      strBuf += nRead;
    }
    else if (nRet == 0) {
      if (tv.tv_sec == 0 && tv.tv_usec == 0) return (nByte-nLeft);
    }
    else {
      return -1;
    }
  }
  return (nByte-nLeft);
}

// *******************************************************************
// Function  Name   : WriteTTY
// Function         : Write data to ngModem, the write wait time is "timeout"(in seconds)
// Parameter        : strBuf: the buffer contain the data will be write 
//                  : nByte: the data size 
//                  : nTimeOut: at most wait time
// Return           : on success return the write bytes
//                  : on error: return < 0
// *******************************************************************
int  WriteTTY(unsigned char *strBuf, int nByte, int nTimeOut)
{
  int  nLeft, nRead, nRet;
  fd_set wfds;
  struct timeval tv;

  FD_ZERO(&wfds);
  FD_SET(ngModem, &wfds);
  tv.tv_sec = nTimeOut;
  tv.tv_usec = 0;

  nLeft = nByte;

  while (nLeft > 0) 
  {
    nRet = select(ngModem+1, NULL, &wfds, NULL, &tv);
    if (nRet > 0) {
      nRead = write(ngModem, strBuf, nLeft);
      if (nRead <= 0) 
      { 
        return (nByte - nLeft);
      }
      nLeft -= nRead;
      strBuf += nRead;
    }
    else if (nRet == 0) {
      if (tv.tv_sec == 0 && tv.tv_usec == 0) {
        return (nByte-nLeft);
      }
    }
    else return -1;
  }
  
//  tcdrain(ngModem);
  
  return (nByte - nLeft);
}

⌨️ 快捷键说明

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