Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.8.1 experimental style #54

Open
wants to merge 7 commits into
base: 2.8.1-experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions src/main/java/jssc/SerialNativeAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

public class SerialNativeAccess {
private int osType = -1;
private static SerialNativeInterface sni = new SerialNativeInterface();
private static final SerialNativeInterface sni = new SerialNativeInterface();
private static SerialNativeAccess instance = null;

public static SerialNativeAccess getInstance() {
Expand Down Expand Up @@ -79,36 +79,40 @@ else if(osName.equals("Mac OS X") || osName.equals("Darwin")){//os.name "Darwin"
osName = "mac_os_x";
osType = SerialNativeInterface.OS_MAC_OS_X;
}//<- since 0.9.0

if(architecture.equals("i386") || architecture.equals("i686")){
architecture = "x86";
}
else if(architecture.equals("amd64") || architecture.equals("universal")){//os.arch "universal" since 2.6.0
architecture = "x86_64";
}
else if(architecture.equals("arm")) {//since 2.1.0
String floatStr = "sf";
if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){
floatStr = "hf";
}
else {
try {
Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe");
BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream()));
String buffer = "";
while((buffer = reader.readLine()) != null && !buffer.isEmpty()){
if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){
floatStr = "hf";
break;
switch (architecture) {
case "i386":
case "i686":
architecture = "x86";
break;
case "amd64":
case "universal":
//os.arch "universal" since 2.6.0
architecture = "x86_64";
break;
case "arm":
//since 2.1.0
String floatStr = "sf";
if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){
floatStr = "hf";
}
else {
try {
Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe");
BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream()));
String buffer;
while((buffer = reader.readLine()) != null && !buffer.isEmpty()){
if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){
floatStr = "hf";
break;
}
}
reader.close();
}
reader.close();
}
catch (Exception ex) {
//Do nothing
}
}
architecture = "arm" + floatStr;
catch (Exception ex) {
//Do nothing
}
} architecture = "arm" + floatStr;
break;
}

libFolderPath = libRootFolder + fileSeparator + ".jssc" + fileSeparator + osName;
Expand Down Expand Up @@ -232,6 +236,7 @@ private static boolean extractLib(String libFilePath, String osName, String libN
/**
* Get OS type (OS_LINUX || OS_WINDOWS || OS_SOLARIS)
*
* @return
* @since 0.8
*/
public int getOsType() {
Expand Down
99 changes: 44 additions & 55 deletions src/main/java/org/scream3r/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
*/
public class SerialPort {

private SerialNativeInterface serialInterface;
private final SerialNativeInterface serialInterface;
private SerialPortEventListener eventListener;
private long portHandle;
private String portName;
private final String portName;
private boolean portOpened = false;
private boolean maskAssigned = false;
private boolean eventListenerAdded = false;
Expand Down Expand Up @@ -234,6 +234,7 @@ else if(stopBits == 3){
* parameter <b>"PURGE_RXCLEAR | PURGE_TXCLEAR"</b>.
* <br><b>Note: </b>some devices or drivers may not support this function
*
* @param flags
* @return If the operation is successfully completed, the method returns true, otherwise false.
*
* @throws SerialPortException
Expand All @@ -257,6 +258,7 @@ public boolean purgePort(int flags) throws SerialPortException {
* For example if messages about data receipt and CTS and DSR status changing
* shall be received, it is required to set the mask - <b>"MASK_RXCHAR | MASK_CTS | MASK_DSR"</b>
*
* @param mask
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -267,24 +269,14 @@ public boolean setEventsMask(int mask) throws SerialPortException {
SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_SOLARIS ||
SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0
linuxMask = mask;
if(mask > 0){
maskAssigned = true;
}
else {
maskAssigned = false;
}
maskAssigned = mask > 0;
return true;
}
boolean returnValue = serialInterface.setEventsMask(portHandle, mask);
if(!returnValue){
throw new SerialPortException(portName, "setEventsMask()", SerialPortException.TYPE_CANT_SET_MASK);
}
if(mask > 0){
maskAssigned = true;
}
else {
maskAssigned = false;
}
maskAssigned = mask > 0;
return returnValue;
}

Expand Down Expand Up @@ -317,6 +309,7 @@ private int getLinuxMask() {
/**
* Change RTS line state. Set "true" for switching ON and "false" for switching OFF RTS line
*
* @param enabled
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -329,6 +322,7 @@ public boolean setRTS(boolean enabled) throws SerialPortException {
/**
* Change DTR line state. Set "true" for switching ON and "false" for switching OFF DTR line
*
* @param enabled
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -341,6 +335,7 @@ public boolean setDTR(boolean enabled) throws SerialPortException {
/**
* Write byte array to port
*
* @param buffer
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -353,6 +348,7 @@ public boolean writeBytes(byte[] buffer) throws SerialPortException {
/**
* Write single byte to port
*
* @param singleByte
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -367,6 +363,7 @@ public boolean writeByte(byte singleByte) throws SerialPortException {
/**
* Write String to port
*
* @param string
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -381,9 +378,12 @@ public boolean writeString(String string) throws SerialPortException {
/**
* Write String to port
*
* @param string
* @param charsetName
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
* @throws java.io.UnsupportedEncodingException
*
* @since 2.8.0
*/
Expand All @@ -395,6 +395,7 @@ public boolean writeString(String string, String charsetName) throws SerialPortE
/**
* Write int value (in range from 0 to 255 (0x00 - 0xFF)) to port
*
* @param singleInt
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand All @@ -409,6 +410,7 @@ public boolean writeInt(int singleInt) throws SerialPortException {
/**
* Write int array (in range from 0 to 255 (0x00 - 0xFF)) to port
*
* @param buffer
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand Down Expand Up @@ -474,6 +476,7 @@ public String readHexString(int byteCount) throws SerialPortException {
* Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
*
* @param byteCount count of bytes for reading
* @param separator
*
* @return byte array with "byteCount" length converted to Hexadecimal String
*
Expand Down Expand Up @@ -629,6 +632,7 @@ public String readHexString(int byteCount, int timeout) throws SerialPortExcepti
* Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
*
* @param byteCount count of bytes for reading
* @param separator
* @param timeout timeout in milliseconds
*
* @return byte array with "byteCount" length converted to Hexadecimal String
Expand Down Expand Up @@ -739,6 +743,7 @@ public String readHexString() throws SerialPortException {
/**
* Read all available bytes from port like a Hex String with setted separator
*
* @param separator
* @return If input buffer is empty <b>null</b> will be returned, else byte array with all data from port converted to Hex String
*
* @throws SerialPortException
Expand Down Expand Up @@ -822,6 +827,7 @@ public int getOutputBufferBytesCount() throws SerialPortException {
* Set flow control mode. For required mode use variables with prefix <b>"FLOWCONTROL_"</b>.
* Example of hardware flow control mode(RTS/CTS): setFlowControlMode(FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT);
*
* @param mask
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
Expand Down Expand Up @@ -905,12 +911,7 @@ public int[] getLinesStatus() throws SerialPortException {
*/
public boolean isCTS() throws SerialPortException {
checkPortOpened("isCTS()");
if(serialInterface.getLinesStatus(portHandle)[0] == 1){
return true;
}
else {
return false;
}
return serialInterface.getLinesStatus(portHandle)[0] == 1;
}

/**
Expand All @@ -922,12 +923,7 @@ public boolean isCTS() throws SerialPortException {
*/
public boolean isDSR() throws SerialPortException {
checkPortOpened("isDSR()");
if(serialInterface.getLinesStatus(portHandle)[1] == 1){
return true;
}
else {
return false;
}
return serialInterface.getLinesStatus(portHandle)[1] == 1;
}

/**
Expand All @@ -939,12 +935,7 @@ public boolean isDSR() throws SerialPortException {
*/
public boolean isRING() throws SerialPortException {
checkPortOpened("isRING()");
if(serialInterface.getLinesStatus(portHandle)[2] == 1){
return true;
}
else {
return false;
}
return serialInterface.getLinesStatus(portHandle)[2] == 1;
}

/**
Expand All @@ -956,12 +947,7 @@ public boolean isRING() throws SerialPortException {
*/
public boolean isRLSD() throws SerialPortException {
checkPortOpened("isRLSD()");
if(serialInterface.getLinesStatus(portHandle)[3] == 1){
return true;
}
else {
return false;
}
return serialInterface.getLinesStatus(portHandle)[3] == 1;
}

/**
Expand All @@ -970,6 +956,7 @@ public boolean isRLSD() throws SerialPortException {
* be in charge for handling of occurred events. This method will independently
* set the mask in <b>"MASK_RXCHAR"</b> state if it was not set beforehand
*
* @param listener
* @throws SerialPortException
*/
public void addEventListener(SerialPortEventListener listener) throws SerialPortException {
Expand All @@ -982,6 +969,8 @@ public void addEventListener(SerialPortEventListener listener) throws SerialPort
* charge for handling of occurred events. Also events mask shall be sent to
* this method, to do it use variables with prefix <b>"MASK_"</b> for example <b>"MASK_RXCHAR"</b>
*
* @param listener
* @param mask
* @see #setEventsMask(int) setEventsMask(int mask)
*
* @throws SerialPortException
Expand Down Expand Up @@ -1108,17 +1097,17 @@ private class EventThread extends Thread {
public void run() {
while(!threadTerminated){
int[][] eventArray = waitEvents();
for(int i = 0; i < eventArray.length; i++){
if(eventArray[i][0] > 0 && !threadTerminated){
eventListener.serialEvent(new SerialPortEvent(portName, eventArray[i][0], eventArray[i][1]));
for (int[] event : eventArray) {
if (event[0] > 0 && !threadTerminated) {
eventListener.serialEvent(new SerialPortEvent(portName, event[0], event[1]));
//FIXME
/*if(methodErrorOccurred != null){
try {
methodErrorOccurred.invoke(eventListener, new Object[]{new SerialPortException("port", "method", "exception")});
}
catch (Exception ex) {
System.out.println(ex);
}
try {
methodErrorOccurred.invoke(eventListener, new Object[]{new SerialPortException("port", "method", "exception")});
}
catch (Exception ex) {
System.out.println(ex);
}
}*/
}
}
Expand Down Expand Up @@ -1160,9 +1149,9 @@ private class LinuxEventThread extends EventThread {
//Need to get initial states
public LinuxEventThread(){
int[][] eventArray = waitEvents();
for(int i = 0; i < eventArray.length; i++){
int eventType = eventArray[i][0];
int eventValue = eventArray[i][1];
for (int[] event : eventArray) {
int eventType = event[0];
int eventValue = event[1];
switch(eventType){
case INTERRUPT_BREAK:
interruptBreak = eventValue;
Expand Down Expand Up @@ -1202,10 +1191,10 @@ public void run() {
int mask = getLinuxMask();
boolean interruptTxChanged = false;
int errorMask = 0;
for(int i = 0; i < eventArray.length; i++){
for (int[] event : eventArray) {
boolean sendEvent = false;
int eventType = eventArray[i][0];
int eventValue = eventArray[i][1];
int eventType = event[0];
int eventValue = event[1];
if(eventType > 0 && !super.threadTerminated){
switch(eventType){
case INTERRUPT_BREAK:
Expand Down Expand Up @@ -1284,10 +1273,10 @@ public void run() {
sendEvent = true;
}
break;
/*case MASK_RXFLAG:
/*case MASK_RXFLAG:
//Do nothing at this moment
if(((mask & MASK_RXFLAG) == MASK_RXFLAG) && (eventValue > 0)){
sendEvent = true;
sendEvent = true;
}
break;*/
case MASK_TXEMPTY:
Expand Down
Loading