Fala ai cara, blz?
Eu to mexendo com serial a algum tempinho utilizando javax.comm ou RXTX… pois bem, para a javax.comm funcionar corretamente, utiliza esta classe aqui abaixo, ela vai fazer a instalacao correta dos arquivos, lembrando que todos os 3 arquivos tem que estar na mesma pasta que o .class executado.
import java.io.*;
/**
* Validates that the javax.comm package has been properly installed on this JRE.
*/
public class InstallComm {
public static final String javaPath = System.getProperty("java.home");
public static final String sep = System.getProperty("file.separator");
public static final String dllPath = javaPath+sep+"bin"+sep;
public static final String propPath = javaPath+sep+"lib"+sep;
public static final String jarPath = javaPath+sep+"lib"+sep+"ext"+sep;
public InstallComm() {
}
/**
* Returns true if all of the javax.comm package files are properly installed
*/
public static boolean validate() {
String javaPath = System.getProperty("java.home");
System.out.println("javaPath: "+javaPath);
String sep = System.getProperty("file.separator");
File dll = new File(javaPath+sep+"bin"+sep+"win32com.dll");
File prop = new File(javaPath+sep+"lib"+sep+"javax.comm.properties");
File jar = new File(javaPath+sep+"lib"+sep+"ext"+sep+"comm.jar");
return dll.exists() && prop.exists() && jar.exists();
}
/**
* Installs the javax.comm files into their correct paths
*/
public static void install() {
String javaPath = System.getProperty("java.home");
String sep = System.getProperty("file.separator");
String bin = javaPath+sep+"bin"+sep;
String lib = javaPath+sep+"lib"+sep;
File dll = new File("c:"+sep+"win32com.dll");
File prop = new File("c:"+sep+"javax.comm.properties");
File jar = new File("c:"+sep+"comm.jar");
File new_dll = new File(javaPath+sep+"bin"+sep+"win32com.dll");
File new_prop = new File(javaPath+sep+"lib"+sep+"javax.comm.properties");
File new_jar = new File(javaPath+sep+"lib"+sep+"ext"+sep+"comm.jar");
System.out.println("Copying "+dll.getName()+" to "+new_dll.getPath());
copyFile(dll, new_dll);
System.out.println("Copying "+prop.getName()+" to "+new_prop.getPath());
copyFile(prop, new_prop);
System.out.println("Copying "+jar.getName()+" to "+new_jar.getPath());
copyFile(jar, new_jar);
}
private static boolean copyFile(File source, File result) {
try {
System.out.println("cmd.exe /C copy \""+source.getPath()+"\" \""+result.getAbsolutePath().replaceAll(result.getName(), "")+"\"");
System.out.println(Runtime.getRuntime().exec("cmd.exe /C copy \""+source.getPath()+"\" \""+result.getAbsolutePath().replaceAll(result.getName(), "")+"\""));
return true;
} catch (IOException ioe) {
try {
System.out.println("command.com /C copy \""+source.getPath()+"\" \""+result.getAbsolutePath().replaceAll(result.getName(), "")+"\"");
System.out.println(Runtime.getRuntime().exec("cmd.exe /C copy \""+source.getPath()+"\" \""+result.getAbsolutePath().replaceAll(result.getName(), "")+"\""));
return true;
} catch (IOException ioe2) {
System.out.println("An error occured: " + ioe2.getMessage());
return false;
}
}
}
public static void main(String[] args) {
if(!validate()) {
System.out.println("javax.comm not installed. Installing files...");
install();
} else {
System.out.println("javax.comm installed. ");
}
}
}
Eu peguei este codigo a algum tempo e fiz algumas alteracoes nele, como ele num é meu originalmente podem usar como quiserem…
Após fazer a instalação correta, rode o programa abaixo e veja se funciona corretamente…
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.comm.CommPortIdentifier;
public class PortasDisponiveis {
private Enumeration listaDePortas;
public PortasDisponiveis() {
listaDePortas = CommPortIdentifier.getPortIdentifiers();
}
public boolean isEmpty() {
return !listaDePortas.hasMoreElements();
}
public List<String> listarPortas() {
List<String> lista = new ArrayList<String>();
int i = 0;
while(listaDePortas.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier) listaDePortas.nextElement();
lista.add(port.getName());
System.out.println("[" + i + "] " + port.getName());
i++;
}
return lista;
}
public static void main(String[] args) {
System.out.println("<--- CommPortIdentifier TESTE --->");
PortasDisponiveis portas = new PortasDisponiveis();
//System.out.println(portas.isEmpty());
portas.listarPortas();
System.out.println("<--- FINALIZADO --->");
}
}
Esse ultimo codigo eu fiz, mas pode usar como quiser, se econtrar algum problema avisa ai…
espero ter ajudado! Abracos!
Boa sorte com a porta serial… mas mais chato ainda é mexer com a paralela…