KMotion Quick Reference |
KMotionDLL |
Send Commands WriteLineReadLine
Board Locks
Console
Coff Loader
Compiler
USB
|
int WriteLine(int board, const char *s); Writes a null terminated string of characters to a specified KMotion Board. There is no wait for any response. Return Value 0 if successful, non-zero if unsuccessful (invalid board specified) Parameters board
s
Example
#include "KMotionDLL.h" CKMotionDLL KM;
if (KM.WriteLine(0, "Move0=1000") MyError();
int WriteLineReadLine(int board, const char *s, char *response); Writes a null terminated string of characters to a specified KMotion Board. Waits for a response string. This command is thread safe. It waits for the token for the specified board, sends the command, waits for the response, then releases the board. Return Value 0 if successful, non-zero if unsuccessful (invalid board specified, timeout on the response) Parameters board
s
response
Example
#include "KMotionDLL.h" CKMotionDLL KM; char resp[256];
while { if (KM.WriteLineReadLine(0, "CheckDone0",resp) MyError();
if (strcmp(resp,"1")==0) break;
}
int ReadLineTimeOut(int board,char *buf, int TimeOutms); Waits for a response string from a previously issued command. Note in a multi-process or multi thread environment the KMotion board should be locked prior to issuing a command that has a response(s), Otherwise there is a possibility that another process or thread may receive the expected response. Return Value 0 if successful, non-zero if unsuccessful (invalid board specified, timeout on the response) Parameters board
buf
TimeOutms
Example
#include "KMotionDLL.h" CKMotionDLL KM; char resp1[256]; char resp2[256]; char resp3[256];
// first get the token for the board to allow uninterrupted access int WaitToken(int board);
Return Value 0 if successful, non-zero if unsuccessful (invalid board specified) Parameters board
Example
#include "KMotionDLL.h" CKMotionDLL KM; char resp1[256]; char resp2[256]; char resp3[256];
// first get the token for the board to allow uninterrupted access
int KMotionLock(int board);
Attempts to obtain the token of the specified KMotion board.. Call this function whenever uninterrupted access to a KMotion board is required. For example before a command where several lines of response will be returned. Release the token as quickly as possible by calling the ReleaseToken function as all other access to the locked board will be blocked until released. This function is similar to the WaitToken function, except that it returns immediately (instead of waiting) if the board is already locked. Return Value KMOTION_LOCKED=0, // (and token is locked) if KMotion is available for use Parameters board
Example
#include "KMotionDLL.h" CKMotionDLL KM; char resp1[256]; char resp2[256]; char resp3[256]; int result; // first get the token for the board to allow uninterrupted access
do
{
result = KM.KMotionLock(0);
if (result == KMOTION_NOT_CONNECTED) MyError(); if (result == KMOTION_IN_USE) DoOtherProcessing(); } // tell the board to send 24 (32 bit) words at offset 0 if (KM.WriteLine(0,"GetGatherHex 0 24")) MyError(); // receive the data (8 hex words per line) if (KM.ReadLineTimeout(0,resp1)) MyError(); if (KM.ReadLineTimeout(0,resp2)) MyError(); if (KM.ReadLineTimeout(0,resp3)) MyError(); // release our access to the board KM.ReleaseToken(board); void ReleaseToken(int board);
Return Value none - the function can not fail Parameters board
Example
#include "KMotionDLL.h" CKMotionDLL KM; char resp1[256]; char resp2[256]; char resp3[256]; int result; // first get the token for the board to allow uninterrupted access
do
{
result = KM.KMotionLock(0);
if (result == KMOTION_NOT_CONNECTED) MyError(); if (result == KMOTION_IN_USE) DoOtherProcessing(); } // tell the board to send 24 (32 bit) words at offset 0 if (KM.WriteLine(0,"GetGatherHex 0 24")) MyError(); // receive the data (8 hex words per line) if (KM.ReadLineTimeout(0,resp1)) MyError(); if (KM.ReadLineTimeout(0,resp2)) MyError(); if (KM.ReadLineTimeout(0,resp3)) MyError(); // release our access to the board KM.ReleaseToken(board);
int Failed(int board); This function should be called whenever an error is detected with a KMotion board. This function disconnects the driver, flags the board as disconnected, and displays the error message shown below. A user program may typically detect a timeout error or invalid data error if the KMotion board is powered down or unplugged while communication is in progress. Calling this function will force any subsequent attempts to access the board to wait for a board to be connected, re-connect, flush any buffers, etc...
"Read Failed - Auto Disconnect"
Return Value always 0 - the function can not fail Parameters board
Example #include "KMotionDLL.h" CKMotionDLL KM; if (KM.KMotionLock(0) == KMOTION_LOCKED) // see if we can get access { // upload bulk status if (UploadStatus()) { // error reading status KM.Failed(0); } KM.ReleaseToken(0); } int LoadCoff(int board, const char *Name, unsigned int *EntryPoint, bool PackToFlash);
This function downloads a compiled C program to the memory of the specified KMotion board.
C Programs that run in the KMotion Board are normally compiled using the included and integrated compiler in the KMotion Application. Using the KMotion Application the user's C Program should be loaded into a selected thread and compiled. This will automatically generate a COFF executable with the same name and in the same directory as the C Source code, but with a .out extension. It is the users responsibility to keep track of which thread the COFF executable was compiled to execute in.
This function is used to download the COFF executable to the KMotion's memory. The entry point of the executable will be extracted from the file and retuned to the caller. This entry point address should then be passed to the KMotion board and associated with the same thread that the file was compiled to execute in. The entry point should be passed using the "EntryPoint" command (See the example below).
The downloaded code may then be executed by issuing the "Execute" command Return Value returns 0 - if successful Parameters board
Name
EntryPoint
PackToFlash
Example #include "KMotionDLL.h" CKMotionDLL KM; unsigned int EntryPoint;
if (KM.LoadCoff(0, "C:\\test.out", &EntryPoint, false)) return 1;
int ServiceConsole(int board);
This function should be called at regular intervals. If console data is available a call back to the Console Handler will occur with one line of data.
Return Value returns 0 - if successful Parameters board
Example #include "KMotionDLL.h" CKMotionDLL KM; int ConsoleHandler(const char *buf) { MyLogData(buf); return 0; } KM.SetConsoleCallback(0, ConsoleHandler); KM.ServiceConsole(0);
int SetConsoleCallback(int board, CONSOLE_HANDLER *ch);
Sets the user provided console callback function. Return Value returns 0 - if successful Parameters board
ch
Example #include "KMotionDLL.h" CKMotionDLL KM; int ConsoleHandler(const char *buf) { MyLogData(buf); return 0; } KM.SetConsoleCallback(0, ConsoleHandler); KM.ServiceConsole(0); int CompileAndLoadCoff(int board, const char *Name, int Thread); or int CompileAndLoadCoff(int board, const char *Name, int Thread, char *Err, int MaxErrLen);
Compiles the specified C Program file, downloads the object code to the specified Thread space, and sets the Entry Point, for the specified thread. Two versions of the function are supplied; one returns any error messages, the other does not.
The
downloaded code may then be executed by issuing the
Execute
command. Return Value returns 0 - if successful Parameters board
Name
Thread
Err
MaxErrLen
Example #include "KMotionDLL.h" CKMotionDLL KMotion;
if (KM.CompileAndLoadCoff(0, "C:\\MyProgram.c", 1) MyError(); if (KM.WriteLine(0, "Execute1") MyError(); int ListLocations(int *nlocations, int *list);
Return Value returns 0 - if successful Parameters nlocations
List
Example #include "KMotionDLL.h" CKMotionDLL KM;
int n_boards; int BoardList[256];
if (KM.ListLocations(&n_boards, BoardList) MyError();
|