gclib 2.1.20
Galil Communications Library
All Classes Files Functions Variables Typedefs Macros Modules Pages
GclibJava.java
Go to the documentation of this file.
1
22package gclibjava;
23
24import java.nio.charset.Charset; //Charset for string conversions
25
26//JNA imports
27import com.sun.jna.Library;
28import com.sun.jna.Native;
29import com.sun.jna.Pointer; //g
30import com.sun.jna.ptr.PointerByReference; //for GCon* in GOpen()
31import com.sun.jna.ptr.IntByReference; //for GSize* in GCommand()
32import com.sun.jna.ptr.ByteByReference; //for GStatus* in GInterrupt()
33import java.util.ArrayList;
34import java.util.List; //List<Double>
35
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 }
48
55 @Override
56 protected void finalize() throws Throwable
57 {
58 try {
59 if (connected)
60 GClose();
61 } finally {
62 super.finalize();
63 }
64 }
65
66 // -------------------------------------------------------------------------
67 // JNA for gclib
68 // -------------------------------------------------------------------------
73 interface Gclib extends Library {
74 Gclib INSTANCE = (Gclib)
75 Native.loadLibrary("gclib",
76 Gclib.class);
77 /*
78 Limit calls to one at a time
79 Warning: gclibo library calls gclib. Therefore, calls to Gclib and
80 Gclibo interfaces should not be concurrent.
81 */
82 Gclib SYNC_INSTANCE = (Gclib)
83 Native.synchronizedLibrary(INSTANCE);
84
85 int GArrayDownload(Pointer g, String arrayName, int first, int last, String buffer);
86 int GArrayUpload(Pointer g, String arrayName, int first, int last, int delim, byte[] response, int len);
87 int GCommand(Pointer g, String command, byte[] response, int len, IntByReference bytesReturned);
88 int GClose(Pointer g);
89 int GFirmwareDownload(Pointer g, String filePath);
90 int GInterrupt(Pointer g, ByteByReference statusByte);
91 int GMessage(Pointer g, byte[] response, int len);
92 int GOpen(String address, PointerByReference g);
93 int GProgramDownload(Pointer g, String program, String preprocessor);
94 int GProgramUpload(Pointer g, byte[] response, int len);
95 }
96
97 // -------------------------------------------------------------------------
98 // gclib functions
99 // -------------------------------------------------------------------------
100
110 public void GArrayDownload(String arrayName, List<Double> data) throws GclibJavaException
111 {
112 String buf = new String();
113 buf = data.stream().map((d) -> d.toString() + ",").reduce(buf, String::concat);
114
115 ec(Gclib.SYNC_INSTANCE.GArrayDownload(gclibHandle, arrayName, -1, -1,
116 buf.substring(0, buf.length() - 1)));//remove trailing comma
117 }
118
132 public void GArrayDownload(String arrayName, List<Double> data, int first, int last) throws GclibJavaException
133 {
134 String buf = new String();
135 buf = data.stream().map((d) -> d.toString() + ",").reduce(buf, String::concat);
136
137 ec(Gclib.SYNC_INSTANCE.GArrayDownload(gclibHandle, arrayName, first, last,
138 buf.substring(0, buf.length() - 1)));//remove trailing comma
139 }
140
150 public List<Double> GArrayUpload(String arrayName) throws GclibJavaException
151 {
152 ec(Gclib.SYNC_INSTANCE.GArrayUpload(gclibHandle, arrayName, -1, -1, 1, trafficBuffer, trafficBuffer.length));
153 String[] elements = cstringToString(trafficBuffer).split(", ");
154 List<Double> doubleList = new ArrayList();
155 for (String s : elements)
156 {
157 try
158 {
159 doubleList.add(Double.parseDouble(s));
160 }
161 catch (NumberFormatException e)
162 {
163 throw new GclibJavaException( -10002, e.getMessage()); //G_BAD_VALUE_RANGE
164 }
165 }
166 return doubleList;
167 }
168
182 public List<Double> GArrayUpload(String arrayName, int first, int last) throws GclibJavaException
183 {
184 ec(Gclib.SYNC_INSTANCE.GArrayUpload(gclibHandle, arrayName, first, last, 1, trafficBuffer, trafficBuffer.length));
185 String[] elements = cstringToString(trafficBuffer).split(", ");
186 List<Double> doubleList = new ArrayList();
187 for (String s : elements)
188 {
189 try
190 {
191 doubleList.add(Double.parseDouble(s));
192 }
193 catch (NumberFormatException e)
194 {
195 throw new GclibJavaException( -10002, e.getMessage()); //G_BAD_VALUE_RANGE
196 }
197 }
198 return doubleList;
199 }
200
204 public void GClose()
205 {
206 Gclib.SYNC_INSTANCE.GClose(gclibHandle);
207 connected = false;
208 }
209
220 public String GCommand(String command) throws GclibJavaException
221 {
222 IntByReference ptrInt = new IntByReference(); //for bytes read
223 ec(Gclib.SYNC_INSTANCE.GCommand(gclibHandle, command, trafficBuffer, trafficBuffer.length, ptrInt));
224 String response = cstringToString(trafficBuffer);
225
226 int index = response.lastIndexOf("\r\n:");
227 if (index > 0)
228 response = response.substring(0, index); //trim trailing crlf:
229
230 return response;
231 }
232
243 public void GFirmwareDownload(String filePath) throws GclibJavaException
244 {
245 ec(Gclib.SYNC_INSTANCE.GFirmwareDownload(gclibHandle, filePath));
246 }
247
260 public byte GInterrupt() throws GclibJavaException
261 {
262 ByteByReference statusByte = new ByteByReference();
263 ec(Gclib.SYNC_INSTANCE.GInterrupt(gclibHandle, statusByte));
264 return statusByte.getValue();
265 }
266
288 public String GMessage() throws GclibJavaException
289 {
290 ec(Gclib.SYNC_INSTANCE.GMessage(gclibHandle, trafficBuffer, trafficBuffer.length));
291 return cstringToString(trafficBuffer);
292 }
293
301 public void GOpen(String address) throws GclibJavaException
302 {
303 if (connected)
304 GClose();
305
306 PointerByReference ptrRef = new PointerByReference();
307 ec(Gclib.SYNC_INSTANCE.GOpen(address, ptrRef));
308 gclibHandle = ptrRef.getValue();
309 connected = true;
310 }
311
321 public void GProgramDownload(String program, String preprocessor) throws GclibJavaException
322 {
323 ec(Gclib.SYNC_INSTANCE.GProgramDownload(gclibHandle, program, preprocessor));
324 }
332 public void GProgramDownload(String program) throws GclibJavaException
333 {
334 GProgramDownload(program, "");
335 }
336
344 public String GProgramUpload() throws GclibJavaException
345 {
346 ec(Gclib.SYNC_INSTANCE.GProgramUpload(gclibHandle, trafficBuffer, trafficBuffer.length));
347 return cstringToString(trafficBuffer);
348 }
349
350 // -------------------------------------------------------------------------
351 // JNA for gclibo
352 // -------------------------------------------------------------------------
353
358 interface Gclibo extends Library {
359 Gclibo INSTANCE = (Gclibo)
360 Native.loadLibrary("gclibo",
361 Gclibo.class);
362 /*
363 Limit calls to one at a time
364 Warning: gclibo library calls gclib. Therefore, calls to Gclib and
365 Gclibo interfaces should not be concurrent.
366 */
367 Gclibo SYNC_INSTANCE = (Gclibo)
368 Native.synchronizedLibrary(INSTANCE);
369
370 int GAddresses(byte[] response, int len);
371 int GArrayDownloadFile(Pointer g, String filePath);
372 int GArrayUploadFile(Pointer g, String filePath, String names);
373 int GAssign(String ip, String mac);
374 void GError(int rc, byte[] response, int len);
375 int GInfo(Pointer g, byte[] response, int len);
376 int GIpRequests(byte[] response, int len);
377 int GProgramDownloadFile(Pointer g, String filePath, String preprocessor);
378 int GProgramUploadFile(Pointer g, String filePath);
379 void GSleep(int timeout_ms);
380 int GTimeout(Pointer g, short timeout_ms);
381 int GVersion(byte[] response, int len);
382 int GSetServer(String server_name);
383 int GServerStatus(byte[] response, int len);
384 int GListServers(byte[] response, int len);
385 int GPublishServer(String server_name, int publish, int save);
386 int GRemoteConnections(byte[] response, int len);
387 }
388
389 // -------------------------------------------------------------------------
390 // gclibo functions
391 // -------------------------------------------------------------------------
392
409 public String GAddresses() throws GclibJavaException
410 {
411 ec(Gclibo.SYNC_INSTANCE.GAddresses(trafficBuffer, trafficBuffer.length));
412 return cstringToString(trafficBuffer);
413 }
414
423 public void GArrayDownloadFile(String filePath) throws GclibJavaException
424 {
425 ec(Gclibo.SYNC_INSTANCE.GArrayDownloadFile(gclibHandle, filePath));
426 }
427
441 public void GArrayUploadFile(String filePath, String names) throws GclibJavaException
442 {
443 ec(Gclibo.SYNC_INSTANCE.GArrayUploadFile(gclibHandle, filePath, names));
444 }
445
454 public void GArrayUploadFile(String filePath) throws GclibJavaException
455 {
456 GArrayUploadFile(filePath, "");
457 }
458
469 public void GAssign(String ipAddress, String macAddress) throws GclibJavaException
470 {
471 ec(Gclibo.SYNC_INSTANCE.GAssign(ipAddress, macAddress));
472 }
473
482 public String GInfo() throws GclibJavaException
483 {
484 ec(Gclibo.SYNC_INSTANCE.GInfo(gclibHandle, trafficBuffer, trafficBuffer.length));
485 return cstringToString(trafficBuffer);
486 }
487
499 public String GIpRequests() throws GclibJavaException
500 {
501 ec(Gclibo.SYNC_INSTANCE.GIpRequests(trafficBuffer, trafficBuffer.length));
502 return cstringToString(trafficBuffer);
503 }
504
514 public void GProgramDownloadFile(String filePath, String preprocessor) throws GclibJavaException
515 {
516 ec(Gclibo.SYNC_INSTANCE.GProgramDownloadFile(gclibHandle, filePath, preprocessor));
517 }
518
526 public void GProgramDownloadFile(String filePath) throws GclibJavaException
527 {
528 GProgramDownloadFile(filePath, "");
529 }
530
539 public void GProgramUploadFile(String filePath) throws GclibJavaException
540 {
541 ec(Gclibo.SYNC_INSTANCE.GProgramUploadFile(gclibHandle, filePath));
542 }
543
552 public void GSleep(int timeout_ms)
553 {
554 Gclibo.SYNC_INSTANCE.GSleep(timeout_ms);
555 }
556
565 public void GTimeout(short timeout_ms) throws GclibJavaException
566 {
567 ec(Gclibo.SYNC_INSTANCE.GTimeout(gclibHandle, timeout_ms));
568 }
569
579 public String GVersion() throws GclibJavaException
580 {
581 ec(Gclibo.SYNC_INSTANCE.GVersion(trafficBuffer, trafficBuffer.length));
582 return cstringToString(trafficBuffer);
583 }
584
592 public void GSetServer(String server_name) throws GclibJavaException
593 {
594 ec(Gclibo.SYNC_INSTANCE.GSetServer(server_name));
595 }
596
605 public String GServerStatus() throws GclibJavaException
606 {
607 ec(Gclibo.SYNC_INSTANCE.GServerStatus(trafficBuffer, trafficBuffer.length));
608 return cstringToString(trafficBuffer);
609 }
610
618 public String GListServers() throws GclibJavaException
619 {
620 ec(Gclibo.SYNC_INSTANCE.GListServers(trafficBuffer, trafficBuffer.length));
621 return cstringToString(trafficBuffer);
622 }
623
633 public void GPublishServer(String server_name, int publish, int save) throws GclibJavaException
634 {
635 ec(Gclibo.SYNC_INSTANCE.GPublishServer(server_name, publish, save));
636 }
637
646 {
647 ec(Gclibo.SYNC_INSTANCE.GRemoteConnections(trafficBuffer, trafficBuffer.length));
648 return cstringToString(trafficBuffer);
649 }
650
651 // -------------------------------------------------------------------------
652 // Helper functions
653 // -------------------------------------------------------------------------
654
655 //convert gclib's C strings to Java strings.
656 String cstringToString(byte[] cbuf)
657 {
658 Charset charset = Charset.forName("UTF-8");
659 int i;
660 for (i = 0; i < cbuf.length && cbuf[i] != 0; i++){}//search for gclib's null terminator
661 return new String(cbuf, 0, i, charset);
662 }
663
664 //Error checker for gclib return code
665 void ec(int returnCode) throws GclibJavaException
666 {
667 if (returnCode != 0)
668 {
669 //lookup human-readable string
670 Gclibo.SYNC_INSTANCE.GError(returnCode, trafficBuffer, trafficBuffer.length);
671 throw new GclibJavaException(returnCode, cstringToString(trafficBuffer));
672 }
673 }
674
675}
GclibJava()
Constructor adds gclib to JNA's path.
void finalize()
The last line of defense to close connection.
String GAddresses()
Uses GUtility(), G_UTIL_GCAPS_ADDRESSES or G_UTIL_ADDRESSES to provide a listing of all available con...
String GInfo()
Uses GUtility() and G_UTIL_INFO to provide a useful connection string.
void GAssign(String ipAddress, String macAddress)
Uses GUtility(), G_UTIL_GCAPS_ASSIGN or G_UTIL_ASSIGN to assign an IP address over the Ethernet to a ...
void GTimeout(short timeout_ms)
Uses GUtility() and G_UTIL_TIMEOUT_OVERRIDE to set the library timeout.
void GOpen(String address)
Open a connection to a Galil Controller.
void GClose()
Closes a connection to a Galil Controller.
String GIpRequests()
Uses GUtility(), G_UTIL_GCAPS_IPREQUEST or G_UTIL_IPREQUEST to provide a list of all Galil controller...
String GCommand(String command)
Performs a command-and-response transaction on the connection.
List< Double > GArrayUpload(String arrayName)
Uploads array data from the controller's array table.
void GProgramUploadFile(String filePath)
Program upload to file.
void GArrayDownload(String arrayName, List< Double > data, int first, int last)
Downloads array data to a pre-dimensioned array in the controller's array table.
void GArrayUploadFile(String filePath)
Overload of GArrayUploadFile to upload all arrays.
void GArrayUploadFile(String filePath, String names)
Array upload to file.
void GProgramDownloadFile(String filePath, String preprocessor)
Program download from file.
void GProgramDownload(String program)
Downloads a program using default preprocessor options.
List< Double > GArrayUpload(String arrayName, int first, int last)
Uploads array data from the controller's array table.
void GProgramDownloadFile(String filePath)
Overload of GProgramDownloadFile to use default preprocessor options.
void GArrayDownloadFile(String filePath)
Array download from file.
void GFirmwareDownload(String filePath)
Upgrade firmware.
String GProgramUpload()
Uploads a program from the controller's program buffer.
void GProgramDownload(String program, String preprocessor)
Downloads a program to the controller's program buffer.
void GArrayDownload(String arrayName, List< Double > data)
Downloads array data to a pre-dimensioned array in the controller's array table.
void GSetServer(String server_name)
Connects gclib to a new gcaps server.
void GPublishServer(String server_name, int publish, int save)
Publishes or removes local gcaps server from the network.
String GServerStatus()
Retrieves the name of your local gcaps server and whether or not it is currently published Retrieves ...
String GRemoteConnections()
Returns a list of IP Addresses that currently have an open connection to your hardware.
String GListServers()
Retrieves a list of gcaps servers that are advertising themselves on the local network.
byte GInterrupt()
Provides access to PCI and UDP interrupts from the controller.
String GMessage()
Provides access to unsolicited messages from the controller.
String GVersion()
Uses GUtility(), G_UTIL_VERSION and G_UTIL_GCAPS_VERSION to provide the library and gcaps version num...
void GSleep(int timeout_ms)
Uses GUtility() and G_UTIL_SLEEP to provide a blocking sleep call which can be useful for timing-base...
The JNA interface to the gclib library.
The JNA interface to the open source, gclibo library.