CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Wed Nov 13, 2019 7:36 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Aug 25, 2019 10:21 am 
Offline

Joined: Tue Apr 09, 2019 2:31 pm
Posts: 9
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


Top
   
PostPosted: Mon Aug 26, 2019 9:15 am 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 169
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


Top
   
PostPosted: Sat Aug 31, 2019 6:21 am 
Offline

Joined: Sat Apr 15, 2017 6:24 am
Posts: 11
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



Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 3 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited