Quelltext vom C++ Programm zum Auslesen eines SR810 bzw. 830
Download


/*
*  Ralph Uhl, 2000    ralph@r-uhl.de
*
*  Use at own Risk!
*
*  Program to readout a LockIn Amplifier
*  Stanford Research  SR810 or SR830
*  via the serial port (standart /dev/ttyS1 -> com2)
*  optional commandlineparameter: device
*  example: ./lockin /dev/ttyS0
*  Parameters:
*  8Bit, No Parity, 19200 Baud
*/
#include <stdio.h>
#include <string>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>
 

class Lockin {
  private:
   fstream li;
   void set(char*);
   float* data;
  public:
   Lockin();
   Lockin(char*);
   ~Lockin();
 
/*Lockin Parameter:  Phase Frequenz Amplitude HarmonicMode Sensitivity
                     ReserveMode Zeitkonstante und Samplingrate
*/
   float phase,freq,ampl;
   int harm,sens,reserve,timeconst,srat;
   int ltype;                  //lockintyp-810 oder 830
   int trigMod;

/* Ab jetzt die Lockin-Funkionen */

   void getConfig();   //holt Lockin Parameter
   void setSampl(int);
   void waitStart();
   void waitStop();
   void startMess();
   int  stopMess();
   float* hohlData(int);
};

Lockin::Lockin(){
  li.open("/dev/ttyS1",ios::app|ios::in);
  if (!li) {
   cout<<"RS-232 kann nicht geoeffnet werden!"<<endl;exit(1);
   }
  set("/dev/ttyS1");
 
}
Lockin::Lockin(char* device){
   li.open(device,ios::app|ios::in);
   if (!li) {
   cout<<"RS-232 kann nicht geoeffnet werden!"<<endl;exit(1);
   }
   set(device);
}

Lockin::~Lockin(){
//Filedescriptor schliessen + lcd
li.close();
}

void Lockin::set(char * device){
 
  struct termios settings;

  int fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1){cout << "RS-232 kann nicht geoeffnet werden!"<<endl;exit(1);}

  //Setzen der Inputflags
  settings.c_iflag = IGNPAR | IGNPAR;
 
  //Setzen der Outputflags
  settings.c_oflag = 0;
 
  //Setzen der Controlflags
  settings.c_cflag = CS8;

  //Setzen der Localflags
  settings.c_lflag = 0;

  // Maximale Zeit, die beim Lesen gewartet wird (in 0.1s)
  settings.c_cc[VTIME] = 10;

  // Minimale Anzahl der zu lesenden Bytes
  settings.c_cc[VMIN] = 0;

  //Setzen der Uebertragungsgeschwindigkeit
  cfsetspeed(&settings, B19200);
 
  //Einstellungen an Schnittstelle uebergeben
  tcsetattr(fd, TCSANOW, &settings);
 close(fd);
 
  // Welcher Trigger-Modus? 0 = Manuell;1=TTL an AUX1
  cout << "Trigger-Modus (0=Man,1=Trigger)";
  cin >> trigMod;
  //welcher Lock-In ?
  string str="";
  li << "*IDN ?\r";
  li >> str;
  if (str[29]=='1') ltype = 810;
    else{
     if (str[29]=='3') ltype = 830;
       else {
            cout << "Fehler ! (unbekanntes Geraet?)\nProgramm beendet"
             <<endl; exit(1);
            }
        }void Lockin::getConfig(){
  li << "Freq ?\r" << endl;
  li >> freq;

  li << "SRAT ?\r" << endl;
  li >> srat;

  li << "PHAS ?\r" << endl;
  li >> phase;

  li << "SLVL ?\r" << endl;
  li >> ampl;

  li << "HARM ?\r" << endl;
  li >> harm;

  li << "SENS ?\r" << endl;
  li >> sens;

  li << "RMOD ?\r" << endl;
  li >> reserve;

  li << "OFLT ?\r" << endl;
  li >> timeconst;

//cout << ampl << harm << sens << endl;
}

 

void Lockin::setSampl(int smpl){
  li << "SRAT" << smpl <<"\r" << endl;
  srat=smpl;
}
 

void Lockin :: waitStart() {
    float u1, u2;
    cout << "\nZum Starten Return" << endl;
    cin.get();
    switch(trigMod){
      case 0 : {
        cout << "  Start .. " << endl;
      } break;

      case 1 : {
        cout << "\nWarten auf Start-Trigger "  << endl;
        li<<"OAUX? 1\r"<<endl;               //AUX1 anfordern
        li>>u1;
        u1 = ((u1 > 2)? 5 : 0);
        do{
            u2 = u1;
            li<<"OAUX? 1\r"<<endl;;
            li>>u1;
            cout << u1 << endl;
            u1 = ((u1 > 2)? 5: 0);
            }while(u1 <= u2);  // bis Sprung 0 auf 5  (TTL Wavetek)
        cout << "  Start .. " << endl;
      } break;

    }//switch
}
void Lockin :: waitStop() {
    float u1, u2;
    switch(trigMod){
      case 0 : {
        cout << "\nZum Stoppen Return " << endl;
        cin.get();
        cout << "  .. Stop " << endl;
      } break;

      case 1 : {
        cout << "\nWarten auf Stop-Trigger "  << endl;
 
        li<<"OAUX? 1\r"<<endl;               //AUX1 anfordern
        li>>u1;
        u1 = ((u1 > 2)? 5 : 0);
        do{
            u2 = u1;
            li<<"OAUX? 1\r"<<endl;;
            li>>u1;
            u1 = ((u1 > 2)? 5: 0);
        }while(u1 <= u2);  // bis Sprung auf irgentwas  (TTL Wavetek)

        cout << "  Stop " << endl;

      } break;
    } //switch
}
 

void Lockin::startMess(){
  li << "REST\r" << endl;
  li << "STRT\r" << endl;
 }

int Lockin::stopMess(){  //Messung stoppen und Anzahl daten im Puffer
  li << "PAUS\r" << endl;
  li << "SPTS ?\r" << endl;
  int num=0;
  li >> num;
  return(num);
}

float * Lockin::hohlData(int num){
 
switch(ltype){
  case 810 : {li << "TRCB ? 0," <<num<<"\r"<<endl; } break;
  case 830 : {li << "TRCB ? 1,0," <<num<<"\r"<<endl;}break;
  default : cout << "was mer will"<<endl;
 }

 
 
  num = num * sizeof(float);
  data = new float[num];
  char* chFld = (char*) data;
 
  int i = 0;
    while(i < num) {
        chFld[i] = li.get();
         i++;
    }
  return(data);
 }
int main(int argc, char* argv[]){

  string model;
  ofstream file;
  string s,s2;
  char* datei;
  char* datei2;
  char weiter;
  float tBase;
  float* data;
  int sparm,test=0,numdat;
  Lockin* a;
 if (argc > 1) a = new Lockin(argv[1]);
  else a = new Lockin();

a -> getConfig();
 
cout << "Modell" << a->ltype<<endl;

sparm = a -> srat;
 

cout << "\n\nMoegliche Sampling-Parameter 0 bis 13\n"
     << "1(1/8Hz)  4(1Hz)  7(8Hz)  10(64Hz)  13(512Hz)" << endl;
    cout << "\nSampling-Parameter? [" <<sparm << "] --> ";
    cin >> test;
    if (test>0 && test<15) sparm=test; //bei Bloedsinn alten Wert behalten
    a -> setSampl(sparm);              //neuen Parameter setzen
 
   //Zeitbasis berechnen aus Samplingrate
    tBase = 32;
    for (int i=0; i <= sparm; i++) tBase /= 2;

cin.get();
weiter='j';
while (weiter=='j' || weiter=='J'){
a->waitStart();
a->startMess();
a->waitStop();

numdat= a-> stopMess();
data = a -> hohlData(numdat);

file.open("data.tmp",ios::out);
 if (!file){
            cout << " irgentwelche Probleme!!!"<<endl; }
 else {
       for (int i=0;i<numdat;i++){
       file << i*tBase << "\t"<<data[i]<<endl;
     }

}
file.close();
file.open("ini.tmp");
  file << "\nLockin-Daten:\n" <<endl;
  file << "Phase:(Grad)\t "<< a->phase << endl;
  file << "Frequenz(Hz):\t "<< a->freq << endl;
  file << "Harmonic-mode:\t "<< a->harm << endl;
  file << "Time-constant (param):\t "<< a->timeconst << endl;
  file << "Sensitivity (param):\t "<< a->sens << endl;
  file << "Reserve-mode:\t "<< a->reserve << endl;
  file << "Amplitude(V):\t "<< a->ampl << endl;
  file << "Samplingrate(param):\t "<< a->srat << endl;
file.close();
 

cout << "Daten abgehohlt, Dateiname: ?\n" << endl;
cin >> datei;
a ->getConfig();
strncat(datei,".dat",4);
file.open(datei,ios::noreplace | ios::out);
if (!file){
            cout << datei<< " gibts schon Du Dussel!, Daten in data.tmp"<<endl;
           }
  else {
       for (int i=0;i<numdat;i++){
       file << i*tBase << "\t"<<data[i]<<endl;
     }
}
file.close();

strncat(datei,".ini",4);
 
file.open(datei,ios::out);
   file << "\nLockin-Daten:\n" <<endl;
   file << "Phase:(Grad)\t "<< a->phase << endl;
   file << "Frequenz(Hz):\t "<< a->freq << endl;
   file << "Harmonic-mode:\t "<< a->harm << endl;
   file << "Time-constant (param):\t "<< a->timeconst << endl;
   file << "Sensitivity (param):\t "<< a->sens << endl;
   file << "Reserve-mode:\t "<< a->reserve << endl;
   file << "Amplitude(V):\t "<< a->ampl << endl;
   file << "Samplingrate(param):\t "<< a->srat << endl;
file.close();

 cout << "Noch eine Messung? (J/N) ";
 cin>>weiter;
 cin.get();
}

delete a;
return 0;
}
 
 
 
 
 



last update 2.1.2001