converted by lore's source converter


//////////////////////////////////////////////////////////////////////////////
//
// smp32d.cpp: Implementation of a deamon for the Smp32 sampler device
//
//////////////////////////////////////////////////////////////////////////////
//
// author:     Eckhard Kantz
// website:    http://wegalink.eu
//
//////////////////////////////////////////////////////////////////////////////
/* 
This is FREE software  

Permission is hereby granted, free of charge,  to any person obtaining  a copy 
of this software and associated documentation files (the "Software"),  to deal 
in the Software without restriction, including without limitation  the  rights 
to use,  copy,  modify,  merge,  publish,  distribute, sublicense, and/or sell 
copies  of  the  Software,   and  to  permit  persons  to  whom  the  Software 
is furnished to do so, subject to the following conditions: 

There are no conditions imposed on the use of this software. 

THE SOFTWARE IS PROVIDED "AS IS",  WITHOUT  WARRANTY  OF ANY KIND,  EXPRESS OR 
IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO  THE  WARRANTIES OF MERCHANTABILITY, 
FITNESS  FOR  A  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM,  DAMAGES OR OTHER 
LIABILITY,  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  THE 
SOFTWARE. 
*/

#include "MDataX.h"

#define FILE_PERIOD    3*60
#define DATA_LEN_1952  1952
#define DATA_LEN_512   512
uint8   buf[DATA_LEN_1952];
char  msg[4*DATA_LEN_1952];

void sampleData(int fd,char* cmd,int len)
{
   int ret, count = 0;

   // Sample requested number of bytes 
   if (-1==write(fd,cmd,1)){
      throw (MException(strerror(errno)));
   }
   while (count < len){
      if (0==(ret=read(fd,buf+count,len-count))){
         throw(MException("No data received"));
      }
      count += ret;
   }
}

int main()
{
   #define DEV_SERIAL   "/dev/cua0"
   #define DEV_IOSPEED  B38400
   char*   pszID = "ID";
   int     fd=0;
   int     ret;
   struct  termios termios;
   MDataX   mDataX("EKD@JN58ve.Smp32");
   MString mLine;
   MString mBinX;
   MDateTime mDateTime;
   uint64   u64MilliTime;

   try{
      printf("RS232 daemon 0.1\n");

      // Open serial device
      printf("opening: %s",DEV_SERIAL);
      if (-1==(fd = open(DEV_SERIAL,O_RDWR,O_NOCTTY))){
         throw (MException(strerror(errno)));
      }
      printf(" -> fd=%u\n",fd);

      // Adjust I/O speed
      printf("iospeed: ");

      if(-1==tcgetattr(fd,&termios)){
         throw (MException(strerror(errno)));
      }
      cfmakeraw(&termios);
      if (  -1==cfsetspeed(&termios,DEV_IOSPEED)
         ||-1==tcsetattr(fd,TCSANOW,&termios)
         ){
         throw (MException(strerror(errno)));
      }
      printf("%d\n",cfgetispeed(&termios));

      // Query device ID
      printf("device : ");
      if (-1==write(fd,pszID,strlen(pszID))){
         throw (MException(strerror(errno)));
      }
      if (0==(ret=read(fd,buf,20))){
         throw(MException("No data received"));
      }
      buf[ret]=0;
      printf("%s\n",buf);

      // ---------- Measurement loop ----------
      mDataX.FilePeriod = FILE_PERIOD;
      for (uint32 n=0;;n++){
         printf("\rData record: %u",n);
         // Get durrent timestamp
         u64MilliTime = (uint64)mDateTime.getSysClockNanoSec()/1000000;
         // Get a blok of samples
         sampleData(fd,"R",DATA_LEN_1952);
         // Generate a measurement line for the DataX file
         mLine = "";
         mDataX.num2BinX(&mBinX,&u64MilliTime);
         mLine += mBinX;
         mLine += ";";
         mDataX.bin2BinX(&mBinX,buf,DATA_LEN_1952);
         mLine += mBinX;
         mLine += "\n";
         // Write line to DataX file
         mDataX.writeData(u64MilliTime/1000,mLine);
      }
      // ----end--- Measurement loop ----------

   }catch(MException e){
      printf("\n->MException: %s '%s'\n",e.getMessage(),e.getContext());
   }
   // Free all resources
   if (fd>0){
      printf("closing: fd=%u\n",fd);
      close(fd);
   }
   printf("RS232 daemon finished\n\n");

   exit(0);
}