Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>

#include "rs232.h"
//#define DEBUG

const int debug=0;

struct termios tattr;

int Delay(double sec){
  return usleep((int) (sec*1e6) );
}

int SetComTime(int fd, double timeout_seconds){

  int ntenth;

  ntenth = (int)(timeout_seconds*10);
  tattr.c_cc[VTIME] = ntenth;
  tcsetattr(fd, TCSANOW, &tattr);
#ifdef DEBUG
  printf("SetComTime: %d\n", ntenth);
#endif
  return 0;
}

int FlushInQ(int fd){
  return 0;
}

int FlushOutQ(int fd){
  return 0;
}

int ComWrt (int fd, char* cmd, int Count ){
 
  int nwr;

  nwr=write(fd, cmd, Count);
#ifdef DEBUG
  printf (  "ComWrt: %d, %d, %s\n", Count, nwr, cmd);
#endif  
  return nwr;
}

int ComRdTerm  (int fd,  char *response, int nb, int termchar)
{

  int nread, nloop;

  nread=0;
  nloop=0;

#ifdef DEBUG
  printf (  "ComRdTerm start\n") ;
#endif
//  rewind(fpr);
  while (1) {
    if (nloop++ == nb) return -1;
//    nread += read ( fd, response+nread, nb-nread );
    nread += read ( fd, response+nread, 1 );
#ifdef DEBUG
    response[nread]=0;
    printf ("ComRdTerm nread: %d %d %s\n", nloop, nread, response) ;
#endif
    if (nread>1) if(response[nread-1] == termchar) break;
  }

  nread = nread - 2;
  response[nread]=0;
#ifdef DEBUG
  printf("CmdRdTerm: %s\n",response);
#endif
  return nread;
}

int OpenComConfig( char *dev, char * device_name, long Baud_Rate, int Parity, int Data_Bits,
                      int Stop_Bits, int Input_Queue_Size, int Output_Queue_Size ){

  int fd;

  memset (&tattr, 0, sizeof tattr);

  fd=open(dev, O_RDWR | O_NOCTTY | O_SYNC);
// see 'man tcsetattr'
  tcgetattr (fd, &tattr);
  cfmakeraw(&tattr);
  cfsetspeed(&tattr, B38400);
//  cfsetispeed(&tattr, B38400);
//  cfsetospeed(&tattr, B38400);
// input modes
  tattr.c_iflag&=~IGNBRK;
  tattr.c_iflag&=~(IGNCR | ICRNL | INLCR);
  tattr.c_iflag&=~(IXON | IXOFF | IXANY);
// output modess
  tattr.c_oflag=0;
// local modes
  tattr.c_lflag=0;
  tattr.c_lflag &= ~(ICANON|ECHO) ; // canonical mode and echo input char
// control modes
  tattr.c_cflag |= (CLOCAL | CREAD);    // ignore modem controls,
                                        // enable reading
  tattr.c_cflag &= ~(PARENB | PARODD);      // shut off parity
  tattr.c_cflag &= ~CSTOPB;     // set two stop bits
  tattr.c_cflag &= ~CRTSCTS;    // enable RTS/CTS flow control

  tattr.c_cc[VMIN] = 0;
  tattr.c_cc[VTIME] = 2;
  tcsetattr(fd, TCSAFLUSH, &tattr);

#ifdef DEBUG
  printf("OpenComConfig\n");
#endif
  return fd;                                                                                              ;
}


int CloseCom (int fd){

  close(fd);
  return;
}