How to save sensor values ​​to a file

Runtime
Anonymous
2019-08-25
2019-08-31
  • Anonymous - 2019-08-25

    Originally created by: kjsys1202@naver.com

    Using Raspberry Pi, I can receive sensor values ​​using SPI and Ethernet communication.

    But I want to save the received value as a file that can be opened in window instead of being stored in codesys (just like '.txt' or .excel').

    Is there a way to do it?

    Best regards
    Han

     
  • omeo

    omeo - 2019-08-31

    I have a simple csv log program use caa.file and systime library.No additional license required.

    PROGRAM PRG_DataToCSV
    VAR
       xStartRecord:BOOL;
       xStopRecord:BOOL;
       tDelayTime:TIME:=T#3S;
       sTemplateFileName:FILE.CAA.FILENAME:='C:/Log/template.csv';//template csv head
       fileOpen:FILE.Open;
       fileClose:FILE.Close;
       fileCopy:FILE.Copy;
       fileWrite:FILE.Write;
       iState:INT;
       dtDateAndTime:DT;
       dtDate:DATE;
       xEnable: BOOL;
       sFileWriteName:File.CAA.FILENAME;
       udiUtcTime:UDINT;
       udiResult:UDINT;
       udiUtcTimeLocal:UDINT;
       ton_Delay:TON;
       hFile:File.CAA.HANDLE;
       arData:ARRAY[1..10] OF REAL;
       iDataSize:INT:=10;
       i:INT;
       sWriteStr:STRING:='$n';
    END_VAR
    
    CASE iState OF
       0://get act date
          IF xStartRecord THEN
             udiUtcTime:=SysTimeRtcGet(udiResult);
             udiResult := SysTimeRtcConvertUtcToLocal(udiUtcTime, udiUtcTimeLocal);
             dtDateAndTime:=UDINT_TO_DT(udiUtcTimeLocal);
             dtDate:=DT_TO_DATE(dtDateAndTime);
             sFileWriteName:=CONCAT('C:/Log/',RIGHT(DATE_TO_STRING(dtDate),10));//<- need change
             sFileWriteName:=CONCAT(sFileWriteName,'.csv');
             IF udiUtcTimeLocal <> 0 THEN
                iState:=10;
             END_IF
          END_IF
       10://open act file or create file
          fileOpen(xExecute:=TRUE,sFileName:=sFileWriteName,xExclusive:=FALSE,eFileMode:= File.MODE.MAPPD);
          
          IF fileOpen.xDone THEN
             iState:=50;//have file
             hFile:=FileOpen.hFile;
             fileOpen(xExecute:=FALSE);
          ELSIF fileOpen.xError THEN
             iState:=20;//no file,create it
             fileOpen(xExecute:=FALSE);
          END_IF
       20://create file
          fileCopy(xExecute:=TRUE,sFileNameDest:=sFileWriteName,sFileNameSource:=sTemplateFileName,xOverWrite:= TRUE);
          IF FileCopy.xDone OR FileCopy.xError THEN
             iState:=10;
             fileCopy(xExecute:=FALSE);
          END_IF
       50://generate string
          //write data
          arData[1]:=GVL.Data[1];
          arData[2]:=GVL.Data[2];
          arData[3]:=GVL.Data[3];
          arData[4]:=GVL.Data[4];
          arData[5]:=GVL.Data[5];
          arData[6]:=GVL.Data[6];
          arData[7]:=GVL.Data[7];
          arData[8]:=GVL.Data[8];
          arData[9]:=GVL.Data[9];
          arData[10]:=GVL.Data[10];
          //act local time
          udiUtcTime:=SysTimeRtcGet(udiResult);
          udiResult := SysTimeRtcConvertUtcToLocal(udiUtcTime, udiUtcTimeLocal);
          dtDateAndTime:=UDINT_TO_DT(udiUtcTimeLocal);
          sWriteStr:=CONCAT(sWriteStr,RIGHT(DT_TO_STRING(dtDateAndTime),8));
          sWriteStr:=CONCAT(sWriteStr,',');
          //add string
          FOR i := 1 TO iDataSize DO
             sWriteStr:=CONCAT(sWriteStr,REAL_TO_STRING(DINT_TO_REAL(TRUNC(arData[i]*100))/100));
             IF i <> iDataSize THEN
                sWriteStr:=CONCAT(sWriteStr,',');
             ELSIF i = iDataSize THEN
                iState:=60;
             END_IF
          END_FOR
       60://write to file
          fileWrite(xExecute:=TRUE,hFile:=hFile,pBuffer:=ADR(sWriteStr),szSize:=INT_TO_UDINT(LEN(sWriteStr)));
          IF fileWrite.xDone THEN
             iState:=70;
             sWriteStr:='$n';
             fileWrite(xExecute:=FALSE);
          ELSIF fileWrite.xError THEN
             iState:= 32767;
             FileWrite(xExecute:= FALSE);
          END_IF
       70://write delay
          ton_Delay(IN:=TRUE,PT:=tDelayTime);
          IF ton_Delay.Q THEN
             ton_Delay(IN:=FALSE);
             iState:=50;
          ELSIF xStopRecord THEN
             ton_Delay(IN:=FALSE);
             iState:=100;
          END_IF
       100:
          FileClose(xExecute:=TRUE,hFile:=hFile);
          IF FileClose.xDone OR FileClose.xError THEN
             FileClose(xExecute:=FALSE);
             xStopRecord:=FALSE;
             xStartRecord:=FALSE;
             iState:=0;
          END_IF
    END_CASE
    
     

Log in to post a comment.