CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

How to save sensor values ​​to a file
https://forum.codesys.com/viewtopic.php?f=21&t=9953
Page 1 of 1

Author:  kjsys1202@naver.com [ Sun Aug 25, 2019 10:21 am ]
Post subject:  How to save sensor values ​​to a file

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

Author:  dFx [ Mon Aug 26, 2019 9:15 am ]
Post subject:  Re: How to save sensor values ​​to a file

for built in function see https://help.codesys.com/webapp/idx-AC_ ... n=3.5.14.0
or for managing the file format by yourself see https://help.codesys.com/webapp/idx-CAA ... =3.5.13.40

Author:  omeo [ Sat Aug 31, 2019 6:21 am ]
Post subject:  Re: How to save sensor values ​​to a file

I have a simple csv log program use caa.file and systime library.No additional license required.
Code:
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



Code:

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


Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/