Changes

KFLOP C Programs

71 bytes added, 02:28, 19 October 2015
C Programs provide a powerful and flexible capability to perform almost any sequence of operations within KFLOP.   In most cases after you have tested and tuned all your hardware using the KMotion.exe setup program all the settings and initialization steps required for your system can be placed into an Initialization C Program so that your system can be fully initialized simply by executing the Initialization program.   Using a C Program offers full flexibility to initialize your system however and in whatever order you wish.  In most common cases an existing example can be used with simple modification of values specific to your system.  The KMotion.exe Setup program has some [http://dynomotion..com/Help/ConfigurationScreen/ConfigurationScreen.htm#Utilities automatic capability] to translate the Axes Screen Values that were determined by you during configuration and testing into C Code. 
===Initialization C Program===
The Initialization C Program will normally perform operations of the following type:
# Save the file
# Test after a power cycle if the C Program initializes all Axes Properly
 
===<span id="C_Programming_References" class="mw-headline">C Programming References</span>===
See the links listed [http://dynomotion..com/Support.html here].<br /><br />
===<span id="Simplest_C_Program" class="mw-headline">Simplest C Program</span>===
include "KMotionDef.h"<br />main()<br />{<br />    printf("Hello World!\n");  // send message to console<br />}
 
// Note: standard C language printf <br />int printf(const char *format, ...);             // Print formatted string to console<br />int sprintf(char *s, const char *format, ...);     // Print formatted string to string<br /><br />typedef int FILE;<br />FILE *fopen(const char*, const char*);           // Open a text file for writing on the PC (2nd param = "rt" or "wt") <br />int fprintf(FILE *f, const char * format, ...);           // Print formatted string to the PC's Disk File<br />int fclose(FILE *f);                           // Close the disk file on the PC<br /><br />int Print(char *s);                              // Print a string to the console window<br />int PrintFloat(char *Format, double v);          // Print a double using printf format, ex "%8.3f\n"<br />int PrintInt(char *Format, int v);               // Print an integer using printf format, ex "result=%4d\n"<br />                                <br />int sscanf(const char *_str, const char *_fmt, ...); //scan string and convert to values <br /><br />#define MAX_READ_DISK_LENGTH 1024 // max allowed length of disk file line length<br />extern volatile int read_disk_buffer_status; //status of read disk buffer 1=line available, 2=error, 3=eof <br />extern char read_disk_buffer[MAX_READ_DISK_LENGTH+1];<br />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<br />int fscanf(FILE *f, const char *format, ...); //read sting from PC Disk file, convert values, returns number of items converted<br />int feof(FILE *f);   // End of file status for disk reading
 
===<span id="Simple_DiskReadWrite.c_example" class="mw-headline">Simple DiskReadWrite.c example</span>===
include "KMotionDef.h"<br /><br />main()<br />{<br />    FILE *f;<br />    char s[256];<br />    double a=123.456,b=999.999,c=0.001;<br />    double x=0,y=0,z=0;<br />    int result;<br />    <br />    // write 3 comma separated values to a disk file<br />    f=fopen("c:\\Temp\\KFlopData.txt","wt");<br />    fprintf(s,"%f,%f,%f\n",a,b,c);<br />    fclose(f);<br />    <br />    // read them back in<br />    f=fopen("c:\\Temp\\KFlopData.txt","rt");<br />    if (!f)<br />    {<br />        printf("Unable to open file\n");<br />        return;<br />    }<br />    <br />    // read a line and convert 3 doubles<br />    result=fscanf(f,"%lf,%lf,%lf",&x,&y,&z);<br />    fclose(f);<br />    <br />    printf("# values converted = %d, x=%f, y=%f, z=%f\n",result,x,y,z);<br />}
Bureaucrat, administrator
448
edits