Dynomotion

Group: DynoMotion Message: 12389 From: cnc_machines Date: 10/13/2015
Subject: FScanF
Greetings,

I am having some issues reading in a text file that is saved locally on the hard drive. One program uses fprintf to write the number to the file - I just cant open the text file and read it back in to another program. Can anyone help?

 Maybe part of my problem is the online examples are using #include <stdio.h>. Does #include "KMotionDef.h" use the same syntax as stdio.h?

Is there any example code that would read in a number from a text file and save it as a variable on KMotion?

Thanks,

Scott

Group: DynoMotion Message: 12392 From: Tom Kerekes Date: 10/14/2015
Subject: Re: FScanF
Hi Scott,
KFLOP has no file system on its own, but when connected to a PC with a PC App running it can do basic Disk Read or Write Operation.  Read capability was recently added in Version 4.33q.   See the KmotionDef.h file for supported functions:

// Note: standard C language printf
int printf(const char *format, ...);             // Print formatted string to console
int sprintf(char *s, const char *format, ...);     // Print formatted string to string

typedef int FILE;
FILE *fopen(const char*, const char*);           // Open a text file for writing on the PC (2nd param = "rt" or "wt")
int fprintf(FILE *f, const char * format, ...);           // Print formatted string to the PC's Disk File
int fclose(FILE *f);                           // Close the disk file on the PC

int Print(char *s);                              // Print a string to the console window
int PrintFloat(char *Format, double v);          // Print a double using printf format, ex "%8.3f\n"
int PrintInt(char *Format, int v);               // Print an integer using printf format, ex "result=%4d\n"
                                
int sscanf(const char *_str, const char *_fmt, ...); //scan string and convert to values

#define MAX_READ_DISK_LENGTH 1024 // max allowed length of disk file line length
extern volatile int read_disk_buffer_status; //status of read disk buffer 1=line available, 2=error, 3=eof
extern char read_disk_buffer[MAX_READ_DISK_LENGTH+1];
char *fgets(char *str, int n, FILE *file); //read string from PC disk file, str=buffer, n=buffer length, f=FILE pointer, returns NULL on error
int fscanf(FILE *f, const char *format, ...); //read sting from PC Disk file, convert values, returns number of items converted
int feof(FILE *f);   // End of file status for disk reading

Simple DiskReadWrite.c example

include "KMotionDef.h"

main()
{
    FILE *f;
    char s[256];
    double a=123.456,b=999.999,c=0.001;
    double x=0,y=0,z=0;
    int result;
    
    // write 3 comma separated values to a disk file
    f=fopen("c:\\Temp\\KFlopData.txt","wt");
    fprintf(s,"%f,%f,%f\n",a,b,c);
    fclose(f);
    
    // read them back in
    f=fopen("c:\\Temp\\KFlopData.txt","rt");
    if (!f)
    {
        printf("Unable to open file\n");
        return;
    }
    
    // read a line and convert 3 doubles
    result=fscanf(f,"%lf,%lf,%lf",&x,&y,&z);
    fclose(f);
    
    printf("# values converted = %d, x=%f, y=%f, z=%f\n",result,x,y,z);
}

Group: DynoMotion Message: 12393 From: cnc_machines Date: 10/14/2015
Subject: Re: FScanF
Thanks Tom. I will go ahead and try this. What is the latest stable platform that I can move to? I will be updating 7 machines in production and want to be sure to avoid as many issues as possible.