Skip to content

Commit

Permalink
added install script & example
Browse files Browse the repository at this point in the history
  • Loading branch information
strangesast committed Oct 2, 2020
1 parent 561ce66 commit 6855264
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example/build/
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from debian:buster-slim

copy . .

run ./install.sh
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Fanuc FOCAS Library
Header and runtime files for CNC communication
Header and runtime files for CNC communication

An example of its use can be found in `example/`
1 change: 1 addition & 0 deletions example/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
24 changes: 24 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.13.4)
project(AnotherFanucExample LANGUAGES C VERSION 0.1)

add_subdirectory(src)

set_target_properties(fanuc_example
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
#add_executable(fanuc_example src/main.c)
#target_link_libraries(fanuc_example m fwlib32 pthread)

#LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/libfolder")

install (TARGETS fanuc_example
RUNTIME DESTINATION bin
LIBRARY DESTINATION bin
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
DESTINATION /usr/local/bin)
26 changes: 26 additions & 0 deletions example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from debian:buster-slim as base

workdir /tmp
copy install.sh fwlib32.h libfwlib32* example/extra-deps.sh ./
run ./install.sh && ./extra-deps.sh

from base as builder

run apt-get update && apt-get install -y \
build-essential \
cmake

workdir /usr/src/app

copy example/ .

run mkdir build && \
cd build && \
cmake .. && \
make

from base

copy --from=builder /usr/src/app/build/bin/fanuc_example /usr/local/bin/

cmd fanuc_example
11 changes: 11 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Fanuc fwlib example
An example that connects to a machine specified by MACHINE_IP & MACHINE_PORT in `main.c`

# Docker Instructions
From the `example/` directory: `docker run --rm --network=host -it $(docker build -f Dockerfile ..)`

# Manual Instructions
0. Install fwlib32 (`install.sh` may work)
1. `mkdir build && cd build`
2. `cmake ..`
3. `make`
5 changes: 5 additions & 0 deletions example/extra-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
arch=$(arch)
if [ "$arch" = "x86_64" ]; then
dpkg --add-architecture i386 && apt-get update && apt-get install -y gcc-multilib
fi
8 changes: 8 additions & 0 deletions example/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_executable(fanuc_example main.c)

#add_library(fwlib32 SHARED IMPORTED)
#set_property(TARGET fwlib32 PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libfwlib32.so)

set_target_properties(fanuc_example PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

target_link_libraries(fanuc_example m fwlib32 pthread)
51 changes: 51 additions & 0 deletions example/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include "fwlib32.h"
#define MACHINE_IP "127.0.0.1"
#define MACHINE_PORT 8193

unsigned short libh;

void cleanup() {
if (cnc_freelibhndl(libh) != EW_OK)
fprintf(stderr, "Failed to free library handle!\n");
cnc_exitprocess();
}

int main() {
unsigned long cncIDs[4];
char cncID[36];

if (cnc_startupprocess(0, "focas.log") != EW_OK) {
fprintf(stderr, "Failed to create required log file!\n");
exit(EXIT_FAILURE);
return 1;
}

printf("connecting to machine at %s:%d...\n", MACHINE_IP, MACHINE_PORT);
if (cnc_allclibhndl3(MACHINE_IP, MACHINE_PORT, 10, &libh) != EW_OK) {
fprintf(stderr, "Failed to connect to cnc!\n");
exit(EXIT_FAILURE);
return 1;
}
atexit(cleanup);

if (cnc_rdcncid(libh, cncIDs) != EW_OK) {
fprintf(stderr, "Failed to read cnc id!\n");
exit(EXIT_FAILURE);
return 1;
}

if (sizeof(long) == 4) {
sprintf(cncID, "%08lx-%08lx-%08lx-%08lx", cncIDs[0] & 0xffffffff,
cncIDs[1] & 0xffffffff, cncIDs[2] & 0xffffffff,
cncIDs[3] & 0xffffffff);
} else {
sprintf(cncID, "%08lx-%08lx-%08lx-%08lx", cncIDs[0] & 0xffffffff,
cncIDs[0] >> 32 & 0xffffffff, cncIDs[1] & 0xffffffff,
cncIDs[1] >> 32 & 0xffffffff);
}
printf("machine id: %s\n", cncID);

exit(EXIT_SUCCESS);
}
17 changes: 17 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
version=1.0.5
platform=linux
arch=$(arch)
base="$(dirname "$(realpath "$0")")"
if [ "$arch" = "x86_64" ] || [ "$arch" = "x86" ]; then
arch=x86
elif [ "$arch" = "armv7l" ]; then
arch=armv7
else
echo "unsupported arch"
exit 1
fi
cp fwlib32.h /usr/include/
cp "$base/libfwlib32-$platform-$arch.so.$version" "/usr/local/lib/libfwlib32.so.$version"
ln -sf /usr/local/lib/libfwlib32.so.$version /usr/local/lib/libfwlib32.so
ldconfig

0 comments on commit 6855264

Please sign in to comment.