Galera peguei um tuto aqui do GUJ sobre como ler e escrever na porta Serial, porém não consigo ler da Porta. Segue o código. Se puderem ajudar agradeço.
package SrCom;
public class Stest2 extends SerialCom {
public Stest2() {
super();
}
public static void main(String[] args) {
Stest2 st2 = new Stest2();
if (st2.PortaExiste("/dev/ttyS0") == true) {
System.out.println("Iniciando comunicação para leitura!");
SComm sc2 = new SComm("/dev/ttyS0", 9600, 2000);
sc2.HabilitarLeitura();
sc2.ObterIdDaPorta();
sc2.AbrirPorta();
sc2.LerDados();
sc2.FecharCom();
}
Stest2 st = new Stest2();
if (st.PortaExiste("/dev/ttyS0") == true) {
System.out.println("\nIniciando comunicação para escrita!");
SComm sc = new SComm("/dev/ttyS0", 9600, 2000);
sc.HabilitarEscrita();
sc.ObterIdDaPorta();
sc.AbrirPorta();
sc.EnviarUmaString("Olá mundo!");
sc.FecharCom();
}
}
}
//*****************************************************************************
//COMUNICAÇÃO SERIAL UTILIZANDO A API DA SUN
//*****************************************************************************
// CLASSE Scomm.java É PARTE DO PACKAGE SrCom
//*****************************************************************************
//AUTOR : Daniel V. Gomes
//EMAIL: dansovg@ig.com.br
//DATA INICIAL: 29/04/04
//DATA ATUAL: 03/05/04
//*****************************************************************************
//NOTA: Você pode utilizar este código a vontade mas não me responsabilizo por
//erros durante sua execução. Quaisquer dúvidas ou sugestões,
//envie-me por email.
//*****************************************************************************
package SrCom;
import javax.comm.*;
import java.io.*;
//classe Principal
public class SComm implements Runnable, SerialPortEventListener {
//propriedades
private String Porta;
public String Dadoslidos;
public int nodeBytes;
private int baudrate;
private int timeout;
private CommPortIdentifier cp;
private SerialPort porta;
private OutputStream saida;
private InputStream entrada;
private Thread threadLeitura;
//indicadores
private boolean IDPortaOK; //true porta EXISTE
private boolean PortaOK;// true porta aberta
private boolean Leitura;
private boolean Escrita;
//construtor default paridade : par
//baudrate: 9600 bps stopbits: 2 COM 1
public SComm() {
Porta = "/dev/ttyS0";
baudrate = 9600;
timeout = 1000;
}
;
//um Objeto ComObj é passado ao construtor
//com detalhes de qual porta abrir
//e informações sobre configurações
public SComm(String p, int b, int t) {
this.Porta = p;
this.baudrate = b;
this.timeout = t;
}
;
//habilita escrita de dados
public void HabilitarEscrita() {
Escrita = true;
Leitura = false;
}
//habilita leitura de dados
public void HabilitarLeitura() {
Escrita = false;
Leitura = true;
}
//Obtém o ID da PORTA
public void ObterIdDaPorta() {
try {
cp = CommPortIdentifier.getPortIdentifier(Porta);
if (cp == null) {
System.out.println("A " + Porta + " nao existe!");
System.out.println("ERRO!Abortando...");
IDPortaOK = false;
System.exit(1);
}
IDPortaOK = true;
} catch (Exception e) {
System.out.println("Erro durante o procedimento. STATUS" + e);
IDPortaOK = false;
System.exit(1);
}
}
//Abre a comunicação da porta
public void AbrirPorta() {
try {
porta = (SerialPort) cp.open("SComm", timeout);
PortaOK = true;
System.out.println("Porta aberta com sucesso!");
//configurar parâmetros
porta.setSerialPortParams(baudrate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_2,
SerialPort.PARITY_NONE);
} catch (Exception e) {
PortaOK = false;
System.out.println("Erro ao abrir a porta! STATUS: " + e);
System.exit(1);
}
}
//função que envie um bit para a porta serial
public void EnviarUmaString(String msg) {
if (Escrita == true) {
try {
saida = porta.getOutputStream();
System.out.println("FLUXO OK!");
} catch (Exception e) {
System.out.println("Erro.STATUS: " + e);
}
try {
System.out.println("Enviando um byte para " + Porta);
System.out.println("Enviando : " + msg);
saida.write(msg.getBytes());
Thread.sleep(100);
saida.flush();
} catch (Exception e) {
System.out.println("Houve um erro durante o envio. ");
System.out.println("STATUS: " + e);
System.exit(1);
}
} else {
System.exit(1);
}
}
//leitura de dados na serial
public void LerDados() {
if (Escrita == false) {
try {
entrada = porta.getInputStream();
System.out.println("FLUXO OK!");
} catch (Exception e) {
System.out.println("Erro.STATUS: " + e);
System.exit(1);
}
try {
porta.addEventListener(this);
System.out.println("SUCESSO. Porta aguardando...");
} catch (Exception e) {
System.out.println("Erro ao criar listener: ");
System.out.println("STATUS: " + e);
System.exit(1);
}
porta.notifyOnDataAvailable(true);
try {
threadLeitura = new Thread(this);
threadLeitura.start();
} catch (Exception e) {
System.out.println("Erro ao iniciar leitura: " + e);
}
}
}
//método RUN da thread de leitura
public void run() {
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println("Erro. Status = " + e);
}
}
//gerenciador de eventos de leitura na serial
public void serialEvent(SerialPortEvent ev) {
switch (ev.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] bufferLeitura = new byte[20];
try {
while (entrada.available() > 0) {
nodeBytes = entrada.read(bufferLeitura);
}
Dadoslidos = new String(bufferLeitura);
if (bufferLeitura.length == 0) {
System.out.println("Nada lido!");
} else if (bufferLeitura.length == 1) {
System.out.println("Apenas um byte foi lido!");
} else {
System.out.println(Dadoslidos);
}
} catch (Exception e) {
System.out.println("Erro durante a leitura: " + e);
}
System.out.println("n.o de bytes lidos : " + nodeBytes);
break;
}
}
//função que fecha a conexão
public void FecharCom() {
try {
porta.close();
System.out.println("CONEXAO FECHADA>>FIM..");
} catch (Exception e) {
System.out.println("ERRO AO FECHAR. STATUS: " + e);
System.exit(0);
}
}
//Acessores
public String obterPorta() {
return Porta;
}
public int obterBaudrate() {
return baudrate;
}
}
package SrCom;
import javax.comm.*;
import java.util.*;
public class SerialCom {
//*********************************
//Variáveis
//*********************************
//variáveis para identificar portas
protected String[] portas;
protected Enumeration listaDePortas;
//construtor
public SerialCom(){
listaDePortas = CommPortIdentifier.getPortIdentifiers();
}
//retorna as portas disponíveis
public String[] ObterPortas(){
return portas;
}
//Copia portas para um Array
protected void ListarPortas(){
int i = 0;
portas = new String[10];
while (listaDePortas.hasMoreElements()) {
CommPortIdentifier ips =
(CommPortIdentifier)listaDePortas.nextElement();
portas[i] = ips.getName();
i++;
}
}
//pesquisa se a Porta existe
public boolean PortaExiste(String COMp){
String temp;
boolean e = false;
while (listaDePortas.hasMoreElements()) {
CommPortIdentifier ips =
(CommPortIdentifier)listaDePortas.nextElement();
temp = ips.getName();
if (temp.equals(COMp)== true) {
e = true;
}
}
return e;
}
//imprime as portas disponíveis
protected void ImprimePortas(){
for (int i = 0 ; i < portas.length ; i ++ ) {
if (portas[i] != null ) {
System.out.print(portas[i] + " ");
}
}
System.out.println(" ");
}
}//FIM DA CLASSE