Updated to Version 1.2.2

This commit is contained in:
hedgecrw85 2015-04-30 18:16:39 -05:00
parent 6b08b2150e
commit f49e8803a4
22 changed files with 122 additions and 21 deletions

14
INSTALL
View File

@ -106,31 +106,31 @@ Maven:
<dependency> <dependency>
<groupId>com.fazecast</groupId> <groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId> <artifactId>jSerialComm</artifactId>
<version>1.2.0</version> <version>1.2.2</version>
</dependency> </dependency>
Ivy: Ivy:
<dependency org="com.fazecast" name="jSerialComm" rev="1.2.0"/> <dependency org="com.fazecast" name="jSerialComm" rev="1.2.2"/>
Grape: Grape:
@Grapes( @Grapes(
@Grab(group='com.fazecast', module='jSerialComm', version='1.2.0') @Grab(group='com.fazecast', module='jSerialComm', version='1.2.2')
) )
Gradle: Gradle:
'com.fazecast:jSerialComm:1.2.0' 'com.fazecast:jSerialComm:1.2.2'
Buildr: Buildr:
'com.fazecast:jSerialComm:jar:1.2.0' 'com.fazecast:jSerialComm:jar:1.2.2'
SBT: SBT:
libraryDependencies += "com.fazecast" % "jSerialComm" % "1.2.0" libraryDependencies += "com.fazecast" % "jSerialComm" % "1.2.2"
Leiningen: Leiningen:
[com.fazecast/jSerialComm "1.2.0"] [com.fazecast/jSerialComm "1.2.2"]

View File

@ -4,7 +4,7 @@ apply plugin: 'maven'
group = 'com.fazecast' group = 'com.fazecast'
archivesBaseName = 'jSerialComm' archivesBaseName = 'jSerialComm'
version = '1.1.1' version = '1.2.2'
sourceCompatibility = 1.6 sourceCompatibility = 1.6
targetCompatibility = 1.6 targetCompatibility = 1.6

View File

@ -74,6 +74,39 @@ void getFriendlyName(const char* productFile, char* friendlyName)
} }
} }
void getDriverName(const char* directoryToSearch, char* friendlyName)
{
friendlyName[0] = '\0';
// Open the directory
DIR *directoryIterator = opendir(directoryToSearch);
if (!directoryIterator)
return;
// Read all sub-directories in the current directory
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// Check if entry is a valid sub-directory
if (directoryEntry->d_name[0] != '.')
{
// Get the readable part of the driver name
strcpy(friendlyName, "USB-to-Serial Port (");
char *startingPoint = strchr(directoryEntry->d_name, ':');
if (startingPoint != NULL)
strcat(friendlyName, startingPoint+1);
else
strcat(friendlyName, directoryEntry->d_name);
strcat(friendlyName, ")");
break;
}
directoryEntry = readdir(directoryIterator);
}
// Close the directory
closedir(directoryIterator);
}
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch) void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch)
{ {
// Open the directory // Open the directory
@ -106,7 +139,17 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
strcat(productFile, directoryEntry->d_name); strcat(productFile, directoryEntry->d_name);
strcat(productFile, "/device/../product"); strcat(productFile, "/device/../product");
getFriendlyName(productFile, friendlyName); getFriendlyName(productFile, friendlyName);
if (friendlyName[0] != '\0') if (friendlyName[0] == '\0')
{
// Get friendly name based on the driver loaded
strcpy(productFile, fullPathToSearch);
strcat(productFile, directoryEntry->d_name);
strcat(productFile, "/driver/module/drivers");
getDriverName(productFile, friendlyName);
if (friendlyName[0] != '\0')
push_back(comPorts, systemName, friendlyName);
}
else
push_back(comPorts, systemName, friendlyName); push_back(comPorts, systemName, friendlyName);
// Clean up memory // Clean up memory

View File

@ -33,6 +33,7 @@ typedef struct charPairVector
} charPairVector; } charPairVector;
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString); void push_back(struct charPairVector* vector, const char* firstString, const char* secondString);
void getDriverName(const char* directoryToSearch, char* friendlyName);
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch); void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch);
void getFriendlyName(const char* productFile, char* friendlyName); void getFriendlyName(const char* productFile, char* friendlyName);
unsigned int getBaudRateCode(int baudRate); unsigned int getBaudRateCode(int baudRate);

View File

@ -72,6 +72,39 @@ void getFriendlyName(const char* productFile, char* friendlyName)
} }
} }
void getDriverName(const char* directoryToSearch, char* friendlyName)
{
friendlyName[0] = '\0';
// Open the directory
DIR *directoryIterator = opendir(directoryToSearch);
if (!directoryIterator)
return;
// Read all sub-directories in the current directory
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// Check if entry is a valid sub-directory
if (directoryEntry->d_name[0] != '.')
{
// Get the readable part of the driver name
strcpy(friendlyName, "USB-to-Serial Port (");
char *startingPoint = strchr(directoryEntry->d_name, ':');
if (startingPoint != NULL)
strcat(friendlyName, startingPoint+1);
else
strcat(friendlyName, directoryEntry->d_name);
strcat(friendlyName, ")");
break;
}
directoryEntry = readdir(directoryIterator);
}
// Close the directory
closedir(directoryIterator);
}
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch) void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch)
{ {
// Open the directory // Open the directory
@ -104,21 +137,31 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
strcat(productFile, directoryEntry->d_name); strcat(productFile, directoryEntry->d_name);
strcat(productFile, "/device/../product"); strcat(productFile, "/device/../product");
getFriendlyName(productFile, friendlyName); getFriendlyName(productFile, friendlyName);
if (friendlyName[0] == '\0') // Must be a physical platform port if (friendlyName[0] == '\0') // Must be a physical (or emulated) port
{ {
// Ensure that the platform port is actually open // See if this is a USB-to-Serial converter based on the driver loaded
struct serial_struct serialInfo = { 0 }; strcpy(productFile, fullPathToSearch);
int fd = open(systemName, O_RDWR | O_NONBLOCK | O_NOCTTY); strcat(productFile, directoryEntry->d_name);
if (fd >= 0) strcat(productFile, "/driver/module/drivers");
getDriverName(productFile, friendlyName);
if (friendlyName[0] == '\0') // Must be a physical port
{ {
if ((ioctl(fd, TIOCGSERIAL, &serialInfo) == 0) && (serialInfo.type != PORT_UNKNOWN)) // Ensure that the platform port is actually open
struct serial_struct serialInfo = { 0 };
int fd = open(systemName, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd >= 0)
{ {
strcpy(friendlyName, "Physical Port "); if ((ioctl(fd, TIOCGSERIAL, &serialInfo) == 0) && (serialInfo.type != PORT_UNKNOWN))
strcat(friendlyName, directoryEntry->d_name+3); {
push_back(comPorts, systemName, friendlyName); strcpy(friendlyName, "Physical Port ");
strcat(friendlyName, directoryEntry->d_name+3);
push_back(comPorts, systemName, friendlyName);
}
close(fd);
} }
close(fd);
} }
else
push_back(comPorts, systemName, friendlyName);
} }
else else
push_back(comPorts, systemName, friendlyName); push_back(comPorts, systemName, friendlyName);

View File

@ -33,6 +33,7 @@ typedef struct charPairVector
} charPairVector; } charPairVector;
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString); void push_back(struct charPairVector* vector, const char* firstString, const char* secondString);
void getDriverName(const char* directoryToSearch, char* friendlyName);
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch); void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch);
void getFriendlyName(const char* productFile, char* friendlyName); void getFriendlyName(const char* productFile, char* friendlyName);
unsigned int getBaudRateCode(int baudRate); unsigned int getBaudRateCode(int baudRate);

View File

@ -38,13 +38,14 @@ import java.util.Date;
* This class provides native access to serial ports and devices without requiring external libraries or tools. * This class provides native access to serial ports and devices without requiring external libraries or tools.
* *
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt; * @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 1.2.0 * @version 1.2.2
* @see java.io.InputStream * @see java.io.InputStream
* @see java.io.OutputStream * @see java.io.OutputStream
*/ */
public final class SerialPort public final class SerialPort
{ {
// Static initializer loads correct native library for this machine // Static initializer loads correct native library for this machine
private static boolean isAndroid = false;
static static
{ {
String OS = System.getProperty("os.name").toLowerCase(); String OS = System.getProperty("os.name").toLowerCase();
@ -75,6 +76,7 @@ public final class SerialPort
} }
catch (Exception e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
isAndroid = true;
if (libraryPath.isEmpty()) if (libraryPath.isEmpty())
libraryPath = "Android/armeabi"; libraryPath = "Android/armeabi";
fileName = "libjSerialComm.so"; fileName = "libjSerialComm.so";
@ -249,6 +251,17 @@ public final class SerialPort
*/ */
public final boolean openPort() public final boolean openPort()
{ {
// If this is an Android application, we must explicitly allow serial port access to this library
if (isAndroid)
{
try
{
String grantPermissions = "chmod 666 " + getSystemPortName() + "\nexit\n";
Process process = Runtime.getRuntime().exec("su -c " + grantPermissions);
process.waitFor();
} catch (Exception e) { return false; }
}
try { Thread.sleep(500); } catch (Exception e) {} try { Thread.sleep(500); } catch (Exception e) {}
if (!isOpened && openPortNative()) if (!isOpened && openPortNative())
{ {

View File

@ -32,7 +32,7 @@ import java.util.Scanner;
* This class provides a test case for the jSerialComm library. * This class provides a test case for the jSerialComm library.
* *
* @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt; * @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt;
* @version 1.0 * @version 2.0
* @see java.io.InputStream * @see java.io.InputStream
* @see java.io.OutputStream * @see java.io.OutputStream
*/ */