Olá, segue um exemplo abaixo com uma Thread como conectar e enviar e receber bytes pela porta de comunicação. Nunca testei com o Tung. E caso nao houver conecção troque a linha de código “porta = new SerialPort(porta.USB,115200,8,false,1);” por porta = new SerialPort(porta.DEFAULT,115200,8,false,1);
Em /docs procure por SerialPort que tem as explicações…
Abraços…
import waba.ui.*;
import waba.fx.*;
import waba.io.*;
import waba.applet.*;
import waba.sys.*;
import waba.util.*;
public class testePorta extends MainWindow {
Button b1Enviar,b2Sair;
Label labelPorta;
Label labeltesteScroll;
//Radio rad;
//Check ch;
Edit tf1,tf2;
ScrollBar sb1,sb2;
SerialPort porta;
ProjetoThread t;
String labelstatus;
String sTransmite;
String sRecebe="";
byte bufferRecebimento[] = new byte[100];
boolean eRecebido=false;
boolean eTransmitido=false;
int inumBytes=0;
public testePorta() {
setDoubleBuffer(true);
setBorderStyle(TAB_ONLY_BORDER);
setTitle("Robô");
add(b1Enviar = new Button("Enviar"),5,30);
add(tf1 = new Edit("1234567890123456789"), 50, 29);
add(b2Sair = new Button("Sair"),140,140);
add(tf2 = new Edit("1234567890123456789"), 50, 49);
add(labeltesteScroll = new Label("Valores de Scroll"),10,95);
}
public void onStart() {
porta = new SerialPort(porta.USB,115200,8,false,1);
if (porta.isOpen()==false)
labelstatus = "Falha na conecção";
else
if (porta.isOpen()==true){
labelstatus = "Conectado 115,2 Kbps";
porta.setFlowControl(false);
porta.setReadTimeout(100);//100ms para esperar bytes
t = new ProjetoThread();
addThread(t, true);
_runThreads();
}
add(labelPorta = new Label(""+labelstatus),70,0);
add(sb1 = new ScrollBar(ScrollBar.HORIZONTAL));
sb1.setRect(LEFT,CENTER,waba.sys.Settings.screenWidth/2, PREFERRED);
add(sb2 = new ScrollBar(ScrollBar.VERTICAL));
sb2.setRect(RIGHT - 50, CENTER + 20, PREFERRED,waba.sys.Settings.screenHeight/2);
sb1.setValues(50, 10, 0, 110);
sb1.setUnitIncrement(10);
sb2.setValues(50, 10, 0, 110);
// 50 = valor inicial
// 10 = vai de 10 em 10 por toque longo
// 0 = inicial
// 110 = valor final -10 = 100... 10 é o tamanho do botao;
tf1.requestFocus();
}
public void onEvent(Event e) {
if (e.type == ControlEvent.PRESSED){
if (e.target == b1Enviar ){
sTransmite = tf1.getText();
if (sTransmite!=""){
eTransmitido = true;
tf1.requestFocus();
}
else
eTransmitido = false;
}
else
if (e.target == b2Sair){
porta.close();
killThreads();
waba.sys.Vm.sleep(10);
MainWindow.getMainWindow().exit(0);
}
else
if (e.target == sb1){
int i;
i= sb1.getValue();
labeltesteScroll.setText(""+i);
tf1.requestFocus();
}
}
}
class ProjetoThread implements waba.sys.Thread{
public void run() {
if (eTransmitido==true){
porta.writeBytes(sTransmite.getBytes(), 0, sTransmite.length());
eTransmitido = false;
tf1.setText("");
}
inumBytes = porta.readCheck();
porta.readBytes(bufferRecebimento, 0, inumBytes);
if (inumBytes>0){
for (int i=0; i<inumBytes;i++)
sRecebe += (char) bufferRecebimento[i];
escreveBytesRecebidos();
inumBytes = 0;
}
waba.sys.Vm.sleep(10);
}
public void started() {
waba.sys.Vm.sleep(10);
}
public void stopped() {
}
}
public void escreveBytesRecebidos(){
tf2.setText(sRecebe);
tf1.requestFocus();
sRecebe="";
}
public void onPaint(Graphics g) {
}
}
[color=“red”]Quando vc for postar codigo, coloque-o entre as tags [ code] e [ /code], assim ele ficará identado.[/color]