gclib 2.0.9
Communications API for Galil controllers and PLCs
 
Loading...
Searching...
No Matches
GclibJava.java
Go to the documentation of this file.
1
18package gclibjava;
19
20import java.nio.charset.Charset; //Charset for string conversions
21
22//JNA imports
23import com.sun.jna.Library;
24import com.sun.jna.Native;
25import com.sun.jna.Pointer; //g
26import com.sun.jna.ptr.PointerByReference; //for GCon* in GOpen()
27import com.sun.jna.ptr.IntByReference; //for GSize* in GCommand()
28import com.sun.jna.ptr.ByteByReference; //for GStatus* in GInterrupt()
29import java.util.ArrayList;
30import java.util.List; //List<Double>
31
36public class GclibJava {
37
38 Pointer gclibHandle; //handle for gclib's connection
39 Boolean connected = false; //we use a bool to indicate connection status
40 byte[] trafficBuffer = new byte[524288]; //Most reads/writes to Galil hardware are small. This size will hold the largest array or program upload/download possible.
41
45 public GclibJava()
46 {
47 System.setProperty("jna.library.path", "C:\\Program Files (x86)\\Galil\\gclib\\dll\\x64");
48 }
49
57 protected void finalize() throws Throwable
58 {
59 try {
60 if (connected)
61 GClose();
62 } finally {
63 super.finalize();
64 }
65 }
66
67 // -------------------------------------------------------------------------
68 // JNA for gclib
69 // -------------------------------------------------------------------------
74 interface Gclib extends Library {
75 Gclib INSTANCE = (Gclib)
76 Native.loadLibrary("gclib",
77 Gclib.class);
78 /*
79 Limit calls to one at a time
80 Warning: gclibo library calls gclib. Therefore, calls to Gclib and
81 Gclibo interfaces should not be concurrent.
82 */
83 Gclib SYNC_INSTANCE = (Gclib)
84 Native.synchronizedLibrary(INSTANCE);
85
87 int GArrayUpload(Pointer g, String arrayName, int first, int last, int delim, byte[] response, int len);
88 int GCommand(Pointer g, String command, byte[] response, int len, IntByReference bytesReturned);
89 int GClose(Pointer g);
92 int GMessage(Pointer g, byte[] response, int len);
94 int GProgramDownload(Pointer g, String program, String preprocessor);
95 int GProgramUpload(Pointer g, byte[] response, int len);
96 }
97
98 // -------------------------------------------------------------------------
99 // gclib functions
100 // -------------------------------------------------------------------------
101
112 {
113 String buf = new String();
114 buf = data.stream().map((d) -> d.toString() + ",").reduce(buf, String::concat);
115
116 ec(Gclib.SYNC_INSTANCE.GArrayDownload(gclibHandle, arrayName, -1, -1,
117 buf.substring(0, buf.length() - 1)));//remove trailing comma
118 }
119
130 {
131 ec(Gclib.SYNC_INSTANCE.GArrayUpload(gclibHandle, arrayName, -1, -1, 1, trafficBuffer, trafficBuffer.length));
132 String[] elements = cstringToString(trafficBuffer).split(", ");
134 for (String s : elements)
135 {
136 try
137 {
138 doubleList.add(Double.parseDouble(s));
139 }
141 {
142 throw new GclibJavaException( -10002, e.getMessage()); //G_BAD_VALUE_RANGE
143 }
144 }
145 return doubleList;
146 }
147
151 public void GClose()
152 {
153 Gclib.SYNC_INSTANCE.GClose(gclibHandle);
154 connected = false;
155 }
156
168 {
169 IntByReference ptrInt = new IntByReference(); //for bytes read
170 ec(Gclib.SYNC_INSTANCE.GCommand(gclibHandle, command, trafficBuffer, trafficBuffer.length, ptrInt));
171 String response = cstringToString(trafficBuffer);
172
173 int index = response.lastIndexOf("\r\n:");
174 if (index > 0)
175 response = response.substring(0, index); //trim trailing crlf:
176
177 return response;
178 }
179
191 {
192 ec(Gclib.SYNC_INSTANCE.GFirmwareDownload(gclibHandle, filePath));
193 }
194
208 {
210 ec(Gclib.SYNC_INSTANCE.GInterrupt(gclibHandle, statusByte));
211 return statusByte.getValue();
212 }
213
236 {
237 ec(Gclib.SYNC_INSTANCE.GMessage(gclibHandle, trafficBuffer, trafficBuffer.length));
238 return cstringToString(trafficBuffer);
239 }
240
251 {
252 if (connected)
253 GClose();
254
256 ec(Gclib.SYNC_INSTANCE.GOpen(address, ptrRef));
257 gclibHandle = ptrRef.getValue();
258 connected = true;
259 }
260
270 public void GProgramDownload(String program, String preprocessor) throws GclibJavaException
271 {
272 ec(Gclib.SYNC_INSTANCE.GProgramDownload(gclibHandle, program, preprocessor));
273 }
274
286
295 {
296 ec(Gclib.SYNC_INSTANCE.GProgramUpload(gclibHandle, trafficBuffer, trafficBuffer.length));
297 return cstringToString(trafficBuffer);
298 }
299
300 // -------------------------------------------------------------------------
301 // JNA for gclibo
302 // -------------------------------------------------------------------------
303
308 interface Gclibo extends Library {
309 Gclibo INSTANCE = (Gclibo)
310 Native.loadLibrary("gclibo",
311 Gclibo.class);
312 /*
313 Limit calls to one at a time
314 Warning: gclibo library calls gclib. Therefore, calls to Gclib and
315 Gclibo interfaces should not be concurrent.
316 */
317 Gclibo SYNC_INSTANCE = (Gclibo)
318 Native.synchronizedLibrary(INSTANCE);
319
320 int GAddresses(byte[] response, int len);
323 int GAssign(String ip, String mac);
324 void GError(int rc, byte[] response, int len);
325 int GInfo(Pointer g, byte[] response, int len);
326 int GIpRequests(byte[] response, int len);
327 int GProgramDownloadFile(Pointer g, String filePath, String preprocessor);
329 void GSleep(int timeout_ms);
330 int GTimeout(Pointer g, short timeout_ms);
331 int GVersion(byte[] response, int len);
333 int GServerStatus(byte[] response, int len);
334 int GListServers(byte[] response, int len);
336 int GRemoteConnections(byte[] response, int len);
337 }
338
339 // -------------------------------------------------------------------------
340 // gclibo functions
341 // -------------------------------------------------------------------------
342
360 {
361 ec(Gclibo.SYNC_INSTANCE.GAddresses(trafficBuffer, trafficBuffer.length));
362 return cstringToString(trafficBuffer);
363 }
364
374 {
375 ec(Gclibo.SYNC_INSTANCE.GArrayDownloadFile(gclibHandle, filePath));
376 }
377
392 {
393 ec(Gclibo.SYNC_INSTANCE.GArrayUploadFile(gclibHandle, filePath, names));
394 }
395
408
420 {
421 ec(Gclibo.SYNC_INSTANCE.GAssign(ipAddress, macAddress));
422 }
423
433 {
434 ec(Gclibo.SYNC_INSTANCE.GInfo(gclibHandle, trafficBuffer, trafficBuffer.length));
435 return cstringToString(trafficBuffer);
436 }
437
450 {
451 ec(Gclibo.SYNC_INSTANCE.GIpRequests(trafficBuffer, trafficBuffer.length));
452 return cstringToString(trafficBuffer);
453 }
454
465 {
466 ec(Gclibo.SYNC_INSTANCE.GProgramDownloadFile(gclibHandle, filePath, preprocessor));
467 }
468
480
490 {
491 ec(Gclibo.SYNC_INSTANCE.GProgramUploadFile(gclibHandle, filePath));
492 }
493
502 public void GSleep(int timeout_ms)
503 {
504 Gclibo.SYNC_INSTANCE.GSleep(timeout_ms);
505 }
506
515 public void GTimeout(short timeout_ms) throws GclibJavaException
516 {
517 ec(Gclibo.SYNC_INSTANCE.GTimeout(gclibHandle, timeout_ms));
518 }
519
530 {
531 ec(Gclibo.SYNC_INSTANCE.GVersion(trafficBuffer, trafficBuffer.length));
532 return cstringToString(trafficBuffer);
533 }
534
543 {
544 ec(Gclibo.SYNC_INSTANCE.GSetServer(server_name));
545 }
546
556 {
557 ec(Gclibo.SYNC_INSTANCE.GServerStatus(trafficBuffer, trafficBuffer.length));
558 return cstringToString(trafficBuffer);
559 }
560
569 {
570 ec(Gclibo.SYNC_INSTANCE.GListServers(trafficBuffer, trafficBuffer.length));
571 return cstringToString(trafficBuffer);
572 }
573
584 {
585 ec(Gclibo.SYNC_INSTANCE.GPublishServer(server_name, publish, save));
586 }
587
596 {
597 ec(Gclibo.SYNC_INSTANCE.GRemoteConnections(trafficBuffer, trafficBuffer.length));
598 return cstringToString(trafficBuffer);
599 }
600
601 // -------------------------------------------------------------------------
602 // Helper functions
603 // -------------------------------------------------------------------------
604
605 //convert gclib's C strings to Java strings.
606 String cstringToString(byte[] cbuf)
607 {
608 Charset charset = Charset.forName("UTF-8");
609 int i;
610 for (i = 0; i < cbuf.length && cbuf[i] != 0; i++){}//search for gclib's null terminator
611 return new String(cbuf, 0, i, charset);
612 }
613
614 //Error checker for gclib return code
615 void ec(int returnCode) throws GclibJavaException
616 {
617 if (returnCode != 0)
618 {
619 //lookup human-readable string
620 Gclibo.SYNC_INSTANCE.GError(returnCode, trafficBuffer, trafficBuffer.length);
621 throw new GclibJavaException(returnCode, cstringToString(trafficBuffer));
622 }
623 }
624
625}
void GSetServer(String server_name)
void GProgramDownloadFile(String filePath, String preprocessor)
void GProgramUploadFile(String filePath)
void GPublishServer(String server_name, int publish, int save)
void GProgramDownload(String program, String preprocessor)
String GCommand(String command)
void GProgramDownload(String program)
void GSleep(int timeout_ms)
String GRemoteConnections()
void GProgramDownloadFile(String filePath)
void GArrayDownload(String arrayName, List< Double > data)
void GOpen(String address)
void GFirmwareDownload(String filePath)
void GArrayUploadFile(String filePath)
void GArrayDownloadFile(String filePath)
void GTimeout(short timeout_ms)
List< Double > GArrayUpload(String arrayName)
void GAssign(String ipAddress, String macAddress)
void GArrayUploadFile(String filePath, String names)
GCLIB_DLL_EXPORTED GReturn GCALL GPublishServer(GCStringIn name, GOption publish, GOption save)
Uses GUtility(), G_UTIL_GCAPS_PUBLISH_SERVER to publish local gcaps server to the local network.
Definition gclibo.c:189
GCLIB_DLL_EXPORTED GReturn GCALL GArrayDownloadFile(GCon g, GCStringIn file_path)
Array download from file.
Definition arrays.c:380
GCLIB_DLL_EXPORTED GReturn GCALL GFirmwareDownload(GCon g, GCStringIn filepath)
Upgrade firmware.
GCLIB_DLL_EXPORTED GReturn GCALL GAssign(GCStringIn ip, GCStringIn mac)
Uses GUtility(), G_UTIL_GCAPS_ASSIGN or G_UTIL_ASSIGN to assign an IP address over the Ethernet to a ...
Definition gclibo.c:70
GCLIB_DLL_EXPORTED GReturn GCALL GArrayUploadFile(GCon g, GCStringIn file_path, GCStringIn names)
Array upload to file.
Definition arrays.c:408
GCLIB_DLL_EXPORTED GReturn GCALL GProgramDownloadFile(GCon g, GCStringIn file_path, GCStringIn preprocessor)
Program download from file.
Definition gclibo.c:387
GCLIB_DLL_EXPORTED void GCALL GSleep(unsigned int timeout_ms)
Uses GUtility() and G_UTIL_SLEEP to provide a blocking sleep call which can be useful for timing-base...
Definition gclibo.c:24
GCLIB_DLL_EXPORTED GReturn GCALL GTimeout(GCon g, short timeout_ms)
Uses GUtility() and G_UTIL_TIMEOUT_OVERRIDE to set the library timeout.
Definition gclibo.c:65
GCLIB_DLL_EXPORTED GReturn GCALL GCommand(GCon g, GCStringIn command, GBufOut buffer, GSize buffer_len, GSize *bytes_returned)
Performs a command-and-response transaction on the connection.
GCLIB_DLL_EXPORTED GReturn GCALL GArrayDownload(GCon g, const GCStringIn array_name, GOption first, GOption last, GCStringIn buffer)
Downloads array data to a pre-dimensioned array in the controller's array table.
GCLIB_DLL_EXPORTED void GCALL GError(GReturn rc, GCStringOut error, GSize error_len)
Provides a human-readable description string for return codes.
Definition gclibo.c:459
GCLIB_DLL_EXPORTED GReturn GCALL GProgramUploadFile(GCon g, GCStringIn file_path)
Program upload to file.
Definition gclibo.c:430
GCLIB_DLL_EXPORTED GReturn GCALL GProgramDownload(GCon g, GCStringIn program, GCStringIn preprocessor)
Downloads a program to the controller's program buffer.
GCLIB_DLL_EXPORTED GReturn GCALL GSetServer(GCStringIn server_name)
Uses GUtility(), G_UTIL_GCAPS_SET_SERVER to set the new active server.
Definition gclibo.c:128
GCLIB_DLL_EXPORTED GReturn GCALL GOpen(GCStringIn address, GCon *g)
Open a connection to a Galil Controller.
GCLIB_DLL_EXPORTED GReturn GCALL GArrayUpload(GCon g, const GCStringIn array_name, GOption first, GOption last, GOption delim, GBufOut buffer, GSize buffer_len)
Uploads array data from the controller's array table.
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition examples.h:33
GReturn vector(GCon g, char *file)
Puts controller into Vector Mode and accepts a file defining vector points.
Definition vector.cpp:36