From e90f6a1ca5755a3d835306e9c0fe83372b4efebf Mon Sep 17 00:00:00 2001 From: Sam Zagrobelny Date: Wed, 7 Apr 2021 11:48:12 -0400 Subject: [PATCH] added minimal c example with gcc instructions --- README.md | 12 ++++++++- examples/c-minimal/README.md | 3 +++ examples/c-minimal/main.c | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 examples/c-minimal/README.md create mode 100644 examples/c-minimal/main.c diff --git a/README.md b/README.md index 72b12f1..04e6931 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,14 @@ [![Docker Hub](https://img.shields.io/docker/v/strangesast/fwlib?sort=date)](https://hub.docker.com/r/strangesast/fwlib) Header and runtime files for CNC communication -Examples of its use may be found in `examples/` +# Docker +Build the base image with `docker build .` + +Build an example with `docker build examples/c/Dockerfile` + +# `examples/` +Link or rename appropriate `libfwlib\*.so` (based on platform) to `libfwlib32.so.1` and `libfwlib32.so` + +On linux x86\_64 for example: `ln -s libfwlib32-linux-x64.so.1.0.5 libfwlib32.so` + +More instructions in each example folder diff --git a/examples/c-minimal/README.md b/examples/c-minimal/README.md new file mode 100644 index 0000000..ab58396 --- /dev/null +++ b/examples/c-minimal/README.md @@ -0,0 +1,3 @@ +# Instructions + +From the root of this repository: `gcc -L./ -Wl,-rpath . examples/c-minimal/main.c -lfwlib32 -lm -lpthread -o fanuc_minimal` diff --git a/examples/c-minimal/main.c b/examples/c-minimal/main.c new file mode 100644 index 0000000..2c29b40 --- /dev/null +++ b/examples/c-minimal/main.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include "../../fwlib32.h" +#define MACHINE_HOST "127.0.0.1" +#define MACHINE_PORT 8193 + +int main(int argc, char *argv[]) { + int allocated = 0; + int ret = 0; + unsigned short libh; + char cnc_id[40] = ""; + uint32_t cnc_ids[4]; + +#ifndef _WIN32 + if (cnc_startupprocess(0, "focas.log") != EW_OK) { + fprintf(stderr, "Failed to create required log file!\n"); + return 1; + } +#endif + + printf("connecting to machine at %s:%d...\n", MACHINE_HOST, MACHINE_PORT); + if ((ret = cnc_allclibhndl3(MACHINE_HOST, MACHINE_PORT, 10, &libh)) != EW_OK) { + fprintf(stderr, "Failed to connect to cnc! (%d)\n", ret); + ret = 1; + goto cleanup; + } + allocated = 1; + + if (cnc_rdcncid(libh, (unsigned long *)cnc_ids) != EW_OK) { + fprintf(stderr, "Failed to read cnc id!\n"); + ret = 1; + goto cleanup; + } + + snprintf(cnc_id, 40, "%08x-%08x-%08x-%08x", cnc_ids[0], cnc_ids[1], + cnc_ids[2], cnc_ids[3]); + +cleanup: + if (allocated && cnc_freelibhndl(libh) != EW_OK) + fprintf(stderr, "Failed to free library handle!\n"); +#ifndef _WIN32 + cnc_exitprocess(); +#endif + + printf("machine id: %s\n", cnc_id); +}