gclib  2.0.8
Communications API for Galil controllers and PLCs
GclibJava.java
Go to the documentation of this file.
1 
18 package gclibjava;
19 
20 import java.nio.charset.Charset; //Charset for string conversions
21 
22 //JNA imports
23 import com.sun.jna.Library;
24 import com.sun.jna.Native;
25 import com.sun.jna.Pointer; //g
26 import com.sun.jna.ptr.PointerByReference; //for GCon* in GOpen()
27 import com.sun.jna.ptr.IntByReference; //for GSize* in GCommand()
28 import com.sun.jna.ptr.ByteByReference; //for GStatus* in GInterrupt()
29 import java.util.ArrayList;
30 import java.util.List; //List<Double>
31 
36 public 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 
56  @Override
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 
86  int GArrayDownload(Pointer g, String arrayName, int first, int last, String buffer);
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);
90  int GFirmwareDownload(Pointer g, String filePath);
91  int GInterrupt(Pointer g, ByteByReference statusByte);
92  int GMessage(Pointer g, byte[] response, int len);
93  int GOpen(String address, PointerByReference g);
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 
111  public void GArrayDownload(String arrayName, List<Double> data) throws GclibJavaException
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 
129  public List<Double> GArrayUpload(String arrayName) throws GclibJavaException
130  {
131  ec(Gclib.SYNC_INSTANCE.GArrayUpload(gclibHandle, arrayName, -1, -1, 1, trafficBuffer, trafficBuffer.length));
132  String[] elements = cstringToString(trafficBuffer).split(", ");
133  List<Double> doubleList = new ArrayList();
134  for (String s : elements)
135  {
136  try
137  {
138  doubleList.add(Double.parseDouble(s));
139  }
140  catch (NumberFormatException e)
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 
167  public String GCommand(String command) throws GclibJavaException
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 
190  public void GFirmwareDownload(String filePath) throws GclibJavaException
191  {
192  ec(Gclib.SYNC_INSTANCE.GFirmwareDownload(gclibHandle, filePath));
193  }
194 
207  public byte GInterrupt() throws GclibJavaException
208  {
209  ByteByReference statusByte = new ByteByReference();
210  ec(Gclib.SYNC_INSTANCE.GInterrupt(gclibHandle, statusByte));
211  return statusByte.getValue();
212  }
213 
235  public String GMessage() throws GclibJavaException
236  {
237  ec(Gclib.SYNC_INSTANCE.GMessage(gclibHandle, trafficBuffer, trafficBuffer.length));
238  return cstringToString(trafficBuffer);
239  }
240 
250  public void GOpen(String address) throws GclibJavaException
251  {
252  if (connected)
253  GClose();
254 
255  PointerByReference ptrRef = new PointerByReference();
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 
282  public void GProgramDownload(String program) throws GclibJavaException
283  {
284  GProgramDownload(program, "");
285  }
286 
294  public String GProgramUpload() throws GclibJavaException
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);
321  int GArrayDownloadFile(Pointer g, String filePath);
322  int GArrayUploadFile(Pointer g, String filePath, String names);
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);
328  int GProgramUploadFile(Pointer g, String filePath);
329  void GSleep(int timeout_ms);
330  int GTimeout(Pointer g, short timeout_ms);
331  int GVersion(byte[] response, int len);
332  int GSetServer(String server_name);
333  int GServerStatus(byte[] response, int len);
334  int GListServers(byte[] response, int len);
335  int GPublishServer(String server_name, int publish, int save);
336  int GRemoteConnections(byte[] response, int len);
337  }
338 
339  // -------------------------------------------------------------------------
340  // gclibo functions
341  // -------------------------------------------------------------------------
342 
359  public String GAddresses() throws GclibJavaException
360  {
361  ec(Gclibo.SYNC_INSTANCE.GAddresses(trafficBuffer, trafficBuffer.length));
362  return cstringToString(trafficBuffer);
363  }
364 
373  public void GArrayDownloadFile(String filePath) throws GclibJavaException
374  {
375  ec(Gclibo.SYNC_INSTANCE.GArrayDownloadFile(gclibHandle, filePath));
376  }
377 
391  public void GArrayUploadFile(String filePath, String names) throws GclibJavaException
392  {
393  ec(Gclibo.SYNC_INSTANCE.GArrayUploadFile(gclibHandle, filePath, names));
394  }
395 
404  public void GArrayUploadFile(String filePath) throws GclibJavaException
405  {
406  GArrayUploadFile(filePath, "");
407  }
408 
419  public void GAssign(String ipAddress, String macAddress) throws GclibJavaException
420  {
421  ec(Gclibo.SYNC_INSTANCE.GAssign(ipAddress, macAddress));
422  }
423 
432  public String GInfo() throws GclibJavaException
433  {
434  ec(Gclibo.SYNC_INSTANCE.GInfo(gclibHandle, trafficBuffer, trafficBuffer.length));
435  return cstringToString(trafficBuffer);
436  }
437 
449  public String GIpRequests() throws GclibJavaException
450  {
451  ec(Gclibo.SYNC_INSTANCE.GIpRequests(trafficBuffer, trafficBuffer.length));
452  return cstringToString(trafficBuffer);
453  }
454 
464  public void GProgramDownloadFile(String filePath, String preprocessor) throws GclibJavaException
465  {
466  ec(Gclibo.SYNC_INSTANCE.GProgramDownloadFile(gclibHandle, filePath, preprocessor));
467  }
468 
476  public void GProgramDownloadFile(String filePath) throws GclibJavaException
477  {
478  GProgramDownloadFile(filePath, "");
479  }
480 
489  public void GProgramUploadFile(String filePath) throws GclibJavaException
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 
529  public String GVersion() throws GclibJavaException
530  {
531  ec(Gclibo.SYNC_INSTANCE.GVersion(trafficBuffer, trafficBuffer.length));
532  return cstringToString(trafficBuffer);
533  }
534 
542  public void GSetServer(String server_name) throws GclibJavaException
543  {
544  ec(Gclibo.SYNC_INSTANCE.GSetServer(server_name));
545  }
546 
555  public String GServerStatus() throws GclibJavaException
556  {
557  ec(Gclibo.SYNC_INSTANCE.GServerStatus(trafficBuffer, trafficBuffer.length));
558  return cstringToString(trafficBuffer);
559  }
560 
568  public String GListServers() throws GclibJavaException
569  {
570  ec(Gclibo.SYNC_INSTANCE.GListServers(trafficBuffer, trafficBuffer.length));
571  return cstringToString(trafficBuffer);
572  }
573 
583  public void GPublishServer(String server_name, int publish, int save) throws GclibJavaException
584  {
585  ec(Gclibo.SYNC_INSTANCE.GPublishServer(server_name, publish, save));
586  }
587 
595  public String GRemoteConnections() throws GclibJavaException
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)
Definition: GclibJava.java:542
void GProgramDownloadFile(String filePath, String preprocessor)
Definition: GclibJava.java:464
void GProgramUploadFile(String filePath)
Definition: GclibJava.java:489
void GPublishServer(String server_name, int publish, int save)
Definition: GclibJava.java:583
void GProgramDownload(String program, String preprocessor)
Definition: GclibJava.java:270
String GCommand(String command)
Definition: GclibJava.java:167
List< Double > GArrayUpload(String arrayName)
Definition: GclibJava.java:129
void GProgramDownload(String program)
Definition: GclibJava.java:282
void GSleep(int timeout_ms)
Definition: GclibJava.java:502
String GRemoteConnections()
Definition: GclibJava.java:595
void GProgramDownloadFile(String filePath)
Definition: GclibJava.java:476
void GArrayDownload(String arrayName, List< Double > data)
Definition: GclibJava.java:111
void GOpen(String address)
Definition: GclibJava.java:250
String GProgramUpload()
Definition: GclibJava.java:294
String GServerStatus()
Definition: GclibJava.java:555
void GFirmwareDownload(String filePath)
Definition: GclibJava.java:190
void GArrayUploadFile(String filePath)
Definition: GclibJava.java:404
void GArrayDownloadFile(String filePath)
Definition: GclibJava.java:373
void GTimeout(short timeout_ms)
Definition: GclibJava.java:515
void GAssign(String ipAddress, String macAddress)
Definition: GclibJava.java:419
void GArrayUploadFile(String filePath, String names)
Definition: GclibJava.java:391
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
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33