mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Merge "Move SerialPort to HAL and add SerialPort support for Java Squashed commit of the following:"
This commit is contained in:
@@ -8,8 +8,15 @@ package edu.wpi.first.wpilibj;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import edu.wpi.first.wpilibj.visa.Visa;
|
||||
import edu.wpi.first.wpilibj.visa.VisaException;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
|
||||
import edu.wpi.first.wpilibj.communication.UsageReporting;
|
||||
import edu.wpi.first.wpilibj.hal.HALLibrary;
|
||||
import edu.wpi.first.wpilibj.hal.HALUtil;
|
||||
import edu.wpi.first.wpilibj.hal.SerialPortJNI;
|
||||
|
||||
/**
|
||||
* Driver for the RS-232 serial port on the RoboRIO.
|
||||
@@ -25,8 +32,22 @@ import edu.wpi.first.wpilibj.visa.VisaException;
|
||||
*/
|
||||
public class SerialPort {
|
||||
|
||||
private int m_resourceManagerHandle;
|
||||
private int m_portHandle;
|
||||
private byte m_port;
|
||||
|
||||
public enum Port {
|
||||
kOnboard(0),
|
||||
kMXP(1);
|
||||
|
||||
private int value;
|
||||
|
||||
private Port(int value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue(){
|
||||
return this.value;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents the parity to use for serial communications
|
||||
@@ -166,22 +187,22 @@ public class SerialPort {
|
||||
* @param parity Select the type of parity checking to use.
|
||||
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
|
||||
*/
|
||||
public SerialPort(final int baudRate, final int dataBits, Parity parity, StopBits stopBits) throws VisaException {
|
||||
m_resourceManagerHandle = 0;
|
||||
m_portHandle = 0;
|
||||
|
||||
m_resourceManagerHandle = Visa.viOpenDefaultRM();
|
||||
|
||||
m_portHandle = Visa.viOpen(m_resourceManagerHandle, "ASRL1::INSTR", 0, 0);
|
||||
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_BAUD, baudRate);
|
||||
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_DATA_BITS, dataBits);
|
||||
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_PARITY, parity.value);
|
||||
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_STOP_BITS, stopBits.value);
|
||||
|
||||
public SerialPort(final int baudRate, Port port, final int dataBits, Parity parity, StopBits stopBits) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
m_port = (byte) port.getValue();
|
||||
|
||||
SerialPortJNI.serialInitializePort(m_port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
SerialPortJNI.serialSetBaudRate(m_port, baudRate, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
SerialPortJNI.serialSetDataBits(m_port, (byte) dataBits, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
SerialPortJNI.serialSetParity(m_port, (byte) parity.value, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
SerialPortJNI.serialSetStopBits(m_port, (byte) stopBits.value, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
|
||||
// Set the default read buffer size to 1 to return bytes immediately
|
||||
setReadBufferSize(1);
|
||||
|
||||
@@ -193,11 +214,7 @@ public class SerialPort {
|
||||
|
||||
disableTermination();
|
||||
|
||||
//viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this);
|
||||
//viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL);
|
||||
|
||||
// XXX: Resource Reporting Fixes
|
||||
// UsageReporting.report(UsageReporting.kResourceType_SerialPort,0);
|
||||
UsageReporting.report(tResourceType.kResourceType_SerialPort,0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,8 +224,8 @@ public class SerialPort {
|
||||
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
|
||||
* @param parity Select the type of parity checking to use.
|
||||
*/
|
||||
public SerialPort(final int baudRate, final int dataBits, Parity parity) throws VisaException {
|
||||
this(baudRate, dataBits, parity, StopBits.kOne);
|
||||
public SerialPort(final int baudRate, Port port, final int dataBits, Parity parity) {
|
||||
this(baudRate, port, dataBits, parity, StopBits.kOne);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,8 +235,8 @@ public class SerialPort {
|
||||
* @param baudRate The baud rate to configure the serial port.
|
||||
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
|
||||
*/
|
||||
public SerialPort(final int baudRate, final int dataBits) throws VisaException {
|
||||
this(baudRate, dataBits, Parity.kNone, StopBits.kOne);
|
||||
public SerialPort(final int baudRate, Port port, final int dataBits) {
|
||||
this(baudRate, port, dataBits, Parity.kNone, StopBits.kOne);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,17 +245,18 @@ public class SerialPort {
|
||||
*
|
||||
* @param baudRate The baud rate to configure the serial port.
|
||||
*/
|
||||
public SerialPort(final int baudRate) throws VisaException {
|
||||
this(baudRate, 8, Parity.kNone, StopBits.kOne);
|
||||
public SerialPort(final int baudRate, Port port) {
|
||||
this(baudRate, port, 8, Parity.kNone, StopBits.kOne);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
public void free() {
|
||||
//viUninstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this);
|
||||
Visa.viClose(m_portHandle);
|
||||
Visa.viClose(m_resourceManagerHandle);
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialClose(m_port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,8 +265,11 @@ public class SerialPort {
|
||||
* By default, flow control is disabled.
|
||||
* @param flowControl
|
||||
*/
|
||||
public void setFlowControl(FlowControl flowControl) throws VisaException {
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_FLOW_CNTRL, flowControl.value);
|
||||
public void setFlowControl(FlowControl flowControl) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialSetFlowControl(m_port, (byte) flowControl.value, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,14 +281,15 @@ public class SerialPort {
|
||||
*
|
||||
* @param terminator The character to use for termination.
|
||||
*/
|
||||
public void enableTermination(char terminator) throws VisaException {
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TERMCHAR_EN, true);
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TERMCHAR, terminator);
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_END_IN, Visa.VI_ASRL_END_TERMCHAR);
|
||||
public void enableTermination(char terminator) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialEnableTermination(m_port, terminator, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable termination and specify the termination character.
|
||||
* Enable termination with the default terminator '\n'
|
||||
*
|
||||
* Termination is currently only implemented for receive.
|
||||
* When the the terminator is received, the read() or readString() will return
|
||||
@@ -275,16 +297,18 @@ public class SerialPort {
|
||||
*
|
||||
* The default terminator is '\n'
|
||||
*/
|
||||
public void enableTermination() throws VisaException {
|
||||
public void enableTermination() {
|
||||
this.enableTermination('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable termination behavior.
|
||||
*/
|
||||
public void disableTermination() throws VisaException {
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TERMCHAR_EN, false);
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_END_IN, Visa.VI_ASRL_END_NONE);
|
||||
public void disableTermination() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialDisableTermination(m_port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -292,20 +316,13 @@ public class SerialPort {
|
||||
*
|
||||
* @return The number of bytes available to read.
|
||||
*/
|
||||
public int getBytesReceived() throws VisaException {
|
||||
return Visa.viGetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_AVAIL_NUM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output formatted text to the serial port.
|
||||
*
|
||||
* @deprecated use write(string.getBytes()) instead
|
||||
* @bug All pointer-based parameters seem to return an error.
|
||||
*
|
||||
* @param write A string to write
|
||||
*/
|
||||
public void print(String write) throws VisaException {
|
||||
Visa.viVPrintf(m_portHandle, write);
|
||||
public int getBytesReceived() {
|
||||
int retVal = 0;
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
retVal = SerialPortJNI.serialGetBytesRecieved(m_port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,7 +330,7 @@ public class SerialPort {
|
||||
*
|
||||
* @return The read string
|
||||
*/
|
||||
public String readString() throws VisaException {
|
||||
public String readString() {
|
||||
return readString(getBytesReceived());
|
||||
}
|
||||
|
||||
@@ -323,8 +340,8 @@ public class SerialPort {
|
||||
* @param count the number of characters to read into the string
|
||||
* @return The read string
|
||||
*/
|
||||
public String readString(int count) throws VisaException {
|
||||
byte[] out = Visa.viBufRead(m_portHandle, count);
|
||||
public String readString(int count) {
|
||||
byte[] out = read(count);
|
||||
try {
|
||||
return new String(out, 0, count, "US-ASCII");
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
@@ -339,20 +356,43 @@ public class SerialPort {
|
||||
* @param count The maximum number of bytes to read.
|
||||
* @return An array of the read bytes
|
||||
*/
|
||||
public byte[] read(final int count) throws VisaException {
|
||||
return Visa.viBufRead(m_portHandle, count);
|
||||
public byte[] read(final int count) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
ByteBuffer dataReceivedBuffer = ByteBuffer.allocateDirect(count);
|
||||
SerialPortJNI.serialRead(m_port, dataReceivedBuffer, count, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
byte[] retVal = new byte[count];
|
||||
dataReceivedBuffer.get(retVal);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write raw bytes to the buffer.
|
||||
* Write raw bytes to the serial port.
|
||||
*
|
||||
* @param buffer the buffer to read the bytes from.
|
||||
* @param buffer The buffer of bytes to write.
|
||||
* @param count The maximum number of bytes to write.
|
||||
* @return The number of bytes actually written into the port.
|
||||
*/
|
||||
public int write(byte[] buffer, int count) throws VisaException {
|
||||
return Visa.viBufWrite(m_portHandle, buffer, count);
|
||||
public int write(byte[] buffer, int count) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(count);
|
||||
dataToSendBuffer.put(buffer);
|
||||
int retVal = SerialPortJNI.serialWrite(m_port, dataToSendBuffer, count, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a string to the serial port
|
||||
*
|
||||
* @param data The string to write to the serial port.
|
||||
* @return The number of bytes actually written into the port.
|
||||
*/
|
||||
public int writeString(String data) {
|
||||
return write(data.getBytes(), data.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the timeout of the serial port.
|
||||
@@ -363,8 +403,11 @@ public class SerialPort {
|
||||
*
|
||||
* @param timeout The number of seconds to to wait for I/O.
|
||||
*/
|
||||
public void setTimeout(double timeout) throws VisaException {
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TMO_VALUE, (int) (timeout * 1e3));
|
||||
public void setTimeout(double timeout) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialSetTimeout(m_port, (float)timeout, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,8 +422,11 @@ public class SerialPort {
|
||||
*
|
||||
* @param size The read buffer size.
|
||||
*/
|
||||
void setReadBufferSize(int size) throws VisaException {
|
||||
Visa.viSetBuf(m_portHandle, Visa.VI_READ_BUF, size);
|
||||
public void setReadBufferSize(int size) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialSetReadBufferSize(m_port, size, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,8 +437,11 @@ public class SerialPort {
|
||||
*
|
||||
* @param size The write buffer size.
|
||||
*/
|
||||
void setWriteBufferSize(int size) throws VisaException {
|
||||
Visa.viSetBuf(m_portHandle, Visa.VI_WRITE_BUF, size);
|
||||
public void setWriteBufferSize(int size) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialSetWriteBufferSize(m_port, size, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -406,8 +455,11 @@ public class SerialPort {
|
||||
*
|
||||
* @param mode The write buffer mode.
|
||||
*/
|
||||
public void setWriteBufferMode(WriteBufferMode mode) throws VisaException {
|
||||
Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_WR_BUF_OPER_MODE, mode.value);
|
||||
public void setWriteBufferMode(WriteBufferMode mode) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialSetWriteMode(m_port, (byte)mode.value, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -416,8 +468,11 @@ public class SerialPort {
|
||||
* This is used when setWriteBufferMode() is set to kFlushWhenFull to force a
|
||||
* flush before the buffer is full.
|
||||
*/
|
||||
public void flush() throws VisaException {
|
||||
Visa.viFlush(m_portHandle, Visa.VI_WRITE_BUF);
|
||||
public void flush() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialFlush(m_port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -425,7 +480,10 @@ public class SerialPort {
|
||||
*
|
||||
* Empty the transmit and receive buffers in the device and formatted I/O.
|
||||
*/
|
||||
public void reset() throws VisaException {
|
||||
Visa.viClear(m_portHandle);
|
||||
public void reset() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SerialPortJNI.serialClear(m_port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SerialPortJNI extends JNIWrapper {
|
||||
public static native void serialInitializePort(byte port, IntBuffer status);
|
||||
public static native void serialSetBaudRate(byte port, int baud, IntBuffer status);
|
||||
public static native void serialSetDataBits(byte port, byte bits, IntBuffer status);
|
||||
public static native void serialSetParity(byte port, byte parity, IntBuffer status);
|
||||
public static native void serialSetStopBits(byte port, byte stopBits, IntBuffer status);
|
||||
public static native void serialSetWriteMode(byte port, byte mode, IntBuffer status);
|
||||
public static native void serialSetFlowControl(byte port, byte flow, IntBuffer status);
|
||||
public static native void serialSetTimeout(byte port, float timeout, IntBuffer status);
|
||||
public static native void serialEnableTermination(byte port, char terminator, IntBuffer status);
|
||||
public static native void serialDisableTermination(byte port, IntBuffer status);
|
||||
public static native void serialSetReadBufferSize(byte port, int size, IntBuffer status);
|
||||
public static native void serialSetWriteBufferSize(byte port, int size, IntBuffer status);
|
||||
public static native int serialGetBytesRecieved(byte port, IntBuffer status);
|
||||
public static native int serialRead(byte port, ByteBuffer buffer, int count, IntBuffer status);
|
||||
public static native int serialWrite(byte port, ByteBuffer buffer, int count, IntBuffer status);
|
||||
public static native void serialFlush(byte port, IntBuffer status);
|
||||
public static native void serialClear(byte port, IntBuffer status);
|
||||
public static native void serialClose(byte port, IntBuffer status);
|
||||
}
|
||||
@@ -1,161 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj.visa;
|
||||
|
||||
import com.sun.jna.Function;
|
||||
import com.sun.jna.NativeLibrary;
|
||||
import com.sun.jna.Pointer;
|
||||
import com.sun.jna.ptr.IntByReference;
|
||||
|
||||
/**
|
||||
* This port of visa.h includes only the functions and definitions used in
|
||||
* SerialPort.java
|
||||
* @author dtjones
|
||||
*/
|
||||
public class Visa {
|
||||
|
||||
/**
|
||||
* Stores the status of the last operation.
|
||||
*/
|
||||
private static int status;
|
||||
|
||||
/**
|
||||
* temp buffer for getting values by references
|
||||
*/
|
||||
private static final IntByReference pValue = new IntByReference(0);
|
||||
|
||||
public static final int VI_ATTR_ASRL_BAUD = (0x3FFF0021);
|
||||
public static final int VI_ATTR_ASRL_DATA_BITS = (0x3FFF0022);
|
||||
public static final int VI_ATTR_ASRL_PARITY = (0x3FFF0023);
|
||||
public static final int VI_ATTR_ASRL_STOP_BITS = (0x3FFF0024);
|
||||
public static final int VI_ATTR_ASRL_FLOW_CNTRL = (0x3FFF0025);
|
||||
public static final int VI_ATTR_TERMCHAR_EN = (0x3FFF0038);
|
||||
public static final int VI_ATTR_TERMCHAR = (0x3FFF0018);
|
||||
public static final int VI_ATTR_ASRL_END_IN = (0x3FFF00B3);
|
||||
public static final int VI_ASRL_END_TERMCHAR = 2;
|
||||
public static final int VI_ASRL_END_NONE = 0;
|
||||
public static final int VI_ATTR_ASRL_AVAIL_NUM = (0x3FFF00AC);
|
||||
public static final int VI_SUCCESS_TERM_CHAR = (0x3FFF0005);
|
||||
public static final int VI_SUCCESS_MAX_CNT = (0x3FFF0006);
|
||||
public static final int VI_ATTR_TMO_VALUE = (0x3FFF001A);
|
||||
public static final int VI_ATTR_WR_BUF_OPER_MODE = (0x3FFF002D);
|
||||
public static final short VI_READ_BUF = 1;
|
||||
public static final short VI_WRITE_BUF = 2;
|
||||
|
||||
private static final Function viOpenDefaultRMFn = NativeLibrary.getDefaultInstance().getFunction("viOpenDefaultRM");
|
||||
public synchronized static int viOpenDefaultRM() throws VisaException {
|
||||
status = viOpenDefaultRMFn.call1(pValue.getPointer());
|
||||
assertCleanStatus("viOpenDefaultRM");
|
||||
return pValue.getValue();
|
||||
}
|
||||
|
||||
private static final Function viOpenFn = NativeLibrary.getDefaultInstance().getFunction("viOpen");
|
||||
public synchronized static int viOpen(int sesn, String name, int mode,
|
||||
int timeout) throws VisaException {
|
||||
Pointer pName = new Pointer(name.length());
|
||||
pName.setBytes(0, name.getBytes(), 0, name.length());
|
||||
status = viOpenFn.call5(sesn, pName, mode, timeout, pValue.getPointer());
|
||||
assertCleanStatus("viOpen");
|
||||
pName.free();
|
||||
return pValue.getValue();
|
||||
}
|
||||
|
||||
private static final Function viSetAttributeFn = NativeLibrary.getDefaultInstance().getFunction("viSetAttribute");
|
||||
public static void viSetAttribute(int vi, int attrName, int attrValue) throws VisaException {
|
||||
status = viSetAttributeFn.call3(vi, attrName, attrValue);
|
||||
assertCleanStatus("viSetAttribute");
|
||||
}
|
||||
|
||||
public static void viSetAttribute(int vi, int attrName, boolean attrValue) throws VisaException {
|
||||
status = viSetAttributeFn.call3(vi, attrName, attrValue?1:0);
|
||||
assertCleanStatus("viSetAttribute");
|
||||
}
|
||||
|
||||
private static final Function viSetBufFn = NativeLibrary.getDefaultInstance().getFunction("viSetBuf");
|
||||
public static void viSetBuf(int vi, short buffer, int size) throws VisaException {
|
||||
status = viSetBufFn.call3(vi, buffer, size);
|
||||
assertCleanStatus("viSetBuf");
|
||||
}
|
||||
|
||||
private static final Function viCloseFn = NativeLibrary.getDefaultInstance().getFunction("viClose");
|
||||
public static void viClose(int vi) {
|
||||
viCloseFn.call1(vi);
|
||||
}
|
||||
|
||||
private static final Function viGetAttributeFn = NativeLibrary.getDefaultInstance().getFunction("viGetAttribute");
|
||||
public synchronized static int viGetAttribute(int vi, int attrName) throws VisaException {
|
||||
status = viGetAttributeFn.call3(vi, attrName, pValue.getPointer());
|
||||
assertCleanStatus("viGetAttribute");
|
||||
return pValue.getValue();
|
||||
}
|
||||
|
||||
//Doesn't work correctly. Correct parameters are vi, a format string, and a list of args
|
||||
private static final Function viVPrintfFn = NativeLibrary.getDefaultInstance().getFunction("viVPrintf");
|
||||
public static void viVPrintf(int vi, String write) throws VisaException {
|
||||
Pointer string = new Pointer(write.length());
|
||||
string.setBytes(0, write.getBytes(), 0, write.length());
|
||||
status = viVPrintfFn.call2(vi, string);
|
||||
string.free();
|
||||
assertCleanStatus("viPrintf");
|
||||
}
|
||||
|
||||
private static final Function viBufReadFn = NativeLibrary.getDefaultInstance().getFunction("viBufRead");
|
||||
public static byte[] viBufRead(int vi, int cnt) throws VisaException {
|
||||
Pointer bytes = new Pointer(cnt);
|
||||
Pointer retCnt = new Pointer(4);
|
||||
status = viBufReadFn.call4(vi, bytes, cnt, retCnt);
|
||||
switch (status) {
|
||||
case VI_SUCCESS_TERM_CHAR:
|
||||
case VI_SUCCESS_MAX_CNT:
|
||||
status = 0;
|
||||
break;
|
||||
default:
|
||||
assertCleanStatus("viBufRead");
|
||||
}
|
||||
|
||||
byte[] toReturn = new byte[cnt];
|
||||
bytes.getBytes(0, toReturn, 0, cnt);
|
||||
bytes.free();
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static final Function viBufWriteFn = NativeLibrary.getDefaultInstance().getFunction("viBufWrite");
|
||||
public synchronized static int viBufWrite(int vi, byte[] buf, int cnt) throws VisaException {
|
||||
Pointer string = new Pointer(buf.length);
|
||||
string.setBytes(0, buf, 0, buf.length);
|
||||
status = viBufWriteFn.call4(vi, string, cnt, pValue.getPointer());
|
||||
assertCleanStatus("viBufWrite");
|
||||
string.free();
|
||||
return pValue.getValue();
|
||||
}
|
||||
|
||||
private static final Function viFlushFn = NativeLibrary.getDefaultInstance().getFunction("viFlush");
|
||||
public static void viFlush(int vi, short mask) throws VisaException {
|
||||
status = viFlushFn.call2(vi, mask);
|
||||
assertCleanStatus("viFlush");
|
||||
}
|
||||
|
||||
private static final Function viClearFn = NativeLibrary.getDefaultInstance().getFunction("viClear");
|
||||
public static void viClear(int vi) throws VisaException {
|
||||
viClearFn.call1(vi);
|
||||
assertCleanStatus("viClear");
|
||||
}
|
||||
|
||||
protected static final void assertCleanStatus(String function) throws VisaException {
|
||||
if (status < 0) {
|
||||
throw new VisaException(function, status);
|
||||
} else if (status != 0) {
|
||||
System.out.println(VisaException.makeMessage(function, status));
|
||||
status = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private Visa() {
|
||||
/* do not instantiate utility class */
|
||||
}
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj.visa;
|
||||
|
||||
/**
|
||||
* Exception class which looks up visa error codes
|
||||
* @author dtjones
|
||||
*/
|
||||
public class VisaException extends Exception {
|
||||
|
||||
/**
|
||||
* Create a new VisaException
|
||||
* @param function the name of the function which returned the status code
|
||||
* @param errorCode the status code returned by the function
|
||||
*/
|
||||
public VisaException(String function, int errorCode) {
|
||||
super(makeMessage(function, errorCode));
|
||||
}
|
||||
|
||||
public static String makeMessage(String function, int errorCode) {
|
||||
return lookUpCode(errorCode) + " in function " + function;
|
||||
}
|
||||
|
||||
public static String lookUpCode(int errorCode) {
|
||||
|
||||
switch (errorCode) {
|
||||
case -1073807360:
|
||||
return "VI_ERROR_SYSTEM_ERROR";
|
||||
case -1073807346:
|
||||
return "VI_ERROR_INV_OBJECT";
|
||||
case -1073807345:
|
||||
return "VI_ERROR_RSRC_LOCKED";
|
||||
case -1073807344:
|
||||
return "VI_ERROR_INV_EXPR";
|
||||
case -1073807343:
|
||||
return "VI_ERROR_RSRC_NFOUND";
|
||||
case -1073807342:
|
||||
return "VI_ERROR_INV_RSRC_NAME";
|
||||
case -1073807341:
|
||||
return "VI_ERROR_INV_ACC_MODE";
|
||||
case -1073807339:
|
||||
return "VI_ERROR_TMO";
|
||||
case -1073807338:
|
||||
return "VI_ERROR_CLOSING_FAILED";
|
||||
case -1073807333:
|
||||
return "VI_ERROR_INV_DEGREE";
|
||||
case -1073807332:
|
||||
return "VI_ERROR_INV_JOB_ID";
|
||||
case -1073807331:
|
||||
return "VI_ERROR_NSUP_ATTR";
|
||||
case -1073807330:
|
||||
return "VI_ERROR_NSUP_ATTR_STATE";
|
||||
case -1073807329:
|
||||
return "VI_ERROR_ATTR_READONLY";
|
||||
case -1073807328:
|
||||
return "VI_ERROR_INV_LOCK_TYPE";
|
||||
case -1073807327:
|
||||
return "VI_ERROR_INV_ACCESS_KEY";
|
||||
case -1073807322:
|
||||
return "VI_ERROR_INV_EVENT";
|
||||
case -1073807321:
|
||||
return "VI_ERROR_INV_MECH";
|
||||
case -1073807320:
|
||||
return "VI_ERROR_HNDLR_NINSTALLED";
|
||||
case -1073807319:
|
||||
return "VI_ERROR_INV_HNDLR_REF";
|
||||
case -1073807318:
|
||||
return "VI_ERROR_INV_CONTEXT";
|
||||
case -1073807315:
|
||||
return "VI_ERROR_QUEUE_OVERFLOW";
|
||||
case -1073807313:
|
||||
return "VI_ERROR_NENABLED";
|
||||
case -1073807312:
|
||||
return "VI_ERROR_ABORT";
|
||||
case -1073807308:
|
||||
return "VI_ERROR_RAW_WR_PROT_VIOL";
|
||||
case -1073807307:
|
||||
return "VI_ERROR_RAW_RD_PROT_VIOL";
|
||||
case -1073807306:
|
||||
return "VI_ERROR_OUTP_PROT_VIOL";
|
||||
case -1073807305:
|
||||
return "VI_ERROR_INP_PROT_VIOL";
|
||||
case -1073807304:
|
||||
return "VI_ERROR_BERR";
|
||||
case -1073807303:
|
||||
return "VI_ERROR_IN_PROGRESS";
|
||||
case -1073807302:
|
||||
return "VI_ERROR_INV_SETUP";
|
||||
case -1073807301:
|
||||
return "VI_ERROR_QUEUE_ERROR";
|
||||
case -1073807300:
|
||||
return "VI_ERROR_ALLOC";
|
||||
case -1073807299:
|
||||
return "VI_ERROR_INV_MASK";
|
||||
case -1073807298:
|
||||
return "VI_ERROR_IO";
|
||||
case -1073807297:
|
||||
return "VI_ERROR_INV_FMT";
|
||||
case -1073807295:
|
||||
return "VI_ERROR_NSUP_FMT";
|
||||
case -1073807294:
|
||||
return "VI_ERROR_LINE_IN_USE";
|
||||
case -1073807290:
|
||||
return "VI_ERROR_NSUP_MODE";
|
||||
case -1073807286:
|
||||
return "VI_ERROR_SRQ_NOCCURRED";
|
||||
case -1073807282:
|
||||
return "VI_ERROR_INV_SPACE";
|
||||
case -1073807279:
|
||||
return "VI_ERROR_INV_OFFSET";
|
||||
case -1073807278:
|
||||
return "VI_ERROR_INV_WIDTH";
|
||||
case -1073807276:
|
||||
return "VI_ERROR_NSUP_OFFSET";
|
||||
case -1073807275:
|
||||
return "VI_ERROR_NSUP_VAR_WIDTH";
|
||||
case -1073807273:
|
||||
return "VI_ERROR_WINDOW_NMAPPED";
|
||||
case -1073807271:
|
||||
return "VI_ERROR_RESP_PENDING";
|
||||
case -1073807265:
|
||||
return "VI_ERROR_NLISTENERS";
|
||||
case -1073807264:
|
||||
return "VI_ERROR_NCIC";
|
||||
case -1073807263:
|
||||
return "VI_ERROR_NSYS_CNTLR";
|
||||
case -1073807257:
|
||||
return "VI_ERROR_NSUP_OPER";
|
||||
case -1073807256:
|
||||
return "VI_ERROR_INTR_PENDING";
|
||||
case -1073807254:
|
||||
return "VI_ERROR_ASRL_PARITY";
|
||||
case -1073807253:
|
||||
return "VI_ERROR_ASRL_FRAMING";
|
||||
case -1073807252:
|
||||
return "VI_ERROR_ASRL_OVERRUN";
|
||||
case -1073807250:
|
||||
return "VI_ERROR_TRIG_NMAPPED";
|
||||
case -1073807248:
|
||||
return "VI_ERROR_NSUP_ALIGN_OFFSET";
|
||||
case -1073807247:
|
||||
return "VI_ERROR_USER_BUF";
|
||||
case -1073807246:
|
||||
return "VI_ERROR_RSRC_BUSY";
|
||||
case -1073807242:
|
||||
return "VI_ERROR_NSUP_WIDTH";
|
||||
case -1073807240:
|
||||
return "VI_ERROR_INV_PARAMETER";
|
||||
case -1073807239:
|
||||
return "VI_ERROR_INV_PROT";
|
||||
case -1073807237:
|
||||
return "VI_ERROR_INV_SIZE";
|
||||
case -1073807232:
|
||||
return "VI_ERROR_WINDOW_MAPPED";
|
||||
case -1073807231:
|
||||
return "VI_ERROR_NIMPL_OPER";
|
||||
case -1073807229:
|
||||
return "VI_ERROR_INV_LENGTH";
|
||||
case -1073807215:
|
||||
return "VI_ERROR_INV_MODE";
|
||||
case -1073807204:
|
||||
return "VI_ERROR_SESN_NLOCKED";
|
||||
case -1073807203:
|
||||
return "VI_ERROR_MEM_NSHARED";
|
||||
case -1073807202:
|
||||
return "VI_ERROR_LIBRARY_NFOUND";
|
||||
case -1073807201:
|
||||
return "VI_ERROR_NSUP_INTR";
|
||||
case -1073807200:
|
||||
return "VI_ERROR_INV_LINE";
|
||||
case -1073807199:
|
||||
return "VI_ERROR_FILE_ACCESS";
|
||||
case -1073807198:
|
||||
return "VI_ERROR_FILE_IO";
|
||||
case -1073807197:
|
||||
return "VI_ERROR_NSUP_LINE";
|
||||
case -1073807196:
|
||||
return "VI_ERROR_NSUP_MECH";
|
||||
case -1073807195:
|
||||
return "VI_ERROR_INTF_NUM_NCONFIG";
|
||||
case -1073807194:
|
||||
return "VI_ERROR_CONN_LOST";
|
||||
case -1073807193:
|
||||
return "VI_ERROR_MACHINE_NAVAIL";
|
||||
case -1073807192:
|
||||
return "VI_ERROR_NPERMISSION";
|
||||
default:
|
||||
return "Unknown error code";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>VISA</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=MacRoman">
|
||||
</head>
|
||||
<body>
|
||||
Provides classes to access I/O functions in visa.h.
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user