0
Good people, I will try to explain my problem. I am developing a little software in Netbeans that communicates with Arduino via Serial Port.
My problem is that in Netbeans IDE I am sending String, and I need to convert this String to a whole and I’m not getting it. The Serial port is declared as integer. But it still does not receive the Netbeans String.
I’m not sure I was clear on the question, but please help me.
Arduino code:
void loop(){
// Loop Função main
if(Serial.available()>0){ // Vericando se Existe conexão
//declarando variável que irá recebe comandos do NetBeans
int byteEntrada = 0; // A variável byteEntrada irá recebe Bits do NetBeans , Esses Bits sera transforma em comandos para o Acendimento de LEDS.
int Porta1 =0;
int Porta2 =0; // A variável Por1, por2,port3, Será ultizada para indicar as portas que vão se usada no Arduino.
int Porta3 =0;
byteEntrada = Serial.read(); // Fazendo Leitura da porta Serial para o Comando byteEntrada
Porta1 = Serial.read(); // Fazendo Leitura da porta Serial para o Comando porta1
Porta2 = Serial.read(); // Fazendo Leitura da porta Serial para o Comando porta2
Porta3 = Serial.read(); // Fazendo Leitura da porta Serial para o Comando porta3
if(byteEntrada == '1')
{ // Se o Bit que veio do NetBeans for igual a 1
Desligado(Porta1,Porta2,Porta3,'h','h','l'); // Sera passado por parametro as portas, e a situação para cada led se ( H ) ligado se (L) desligado.
}
}
}
Here is the Netbeans code you are sending:
public void enviarDados(String dados, String p1,String p2,String p3){
try{
output.write(dados.getBytes());
output.write(p1.getBytes());
}catch(IOException e){
Exibir_ERRO("Erro");
System.exit(ERROR);
}
}
The function Desligado
:
void Desligado (int x,int y,int z,char st1,char st2,char st3){
if ( st1 == 'l' ) {
digitalWrite (x,LOW);
} else {
digitalWrite (x,HIGH);
}
if ( st2 == 'l' ) {
digitalWrite (y,HIGH);
} else {
digitalWrite (y,HIGH);
}
if ( st3 == 'l' ) {
digitalWrite (z,HIGH);
} else {
digitalWrite (z,HIGH);
}
}
The part of Netbeans that is configuring communication with the Arduino:
public void iniciarConexao(){
CommPortIdentifier portaId = null;
Enumeration portaEnum = CommPortIdentifier.getPortIdentifiers();
while(portaEnum.hasMoreElements()){
CommPortIdentifier atualPortaId =(CommPortIdentifier) portaEnum.nextElement();
if(porta.equals(atualPortaId.getName())){
portaId=atualPortaId;
break;
}
}
if(portaId == null){
Exibir_ERRO("Não se pode conectar a porta");
System.exit(ERROR);
}
try{
serialPort = (SerialPort) portaId.open(this.getClass().getName(), timeOut);
//parametros da porta serial
serialPort.setSerialPortParams(dataRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
output = serialPort.getOutputStream();
}catch(Exception e){
Exibir_ERRO(e.getMessage());
System.exit(ERROR);
}
}
public void enviarDados(String dados,String p1,String p2,String p3){
try{
output.write(dados.getBytes());
output.write(p1.getBytes());
output.write(p2.getBytes());
output.write(p3.getBytes());
}catch(IOException e){
Exibir_ERRO("Erro");
System.exit(ERROR);
}
}
Hello Jefferson. Just by the code and description of your question is not very clear what you are trying to do and what problem you are facing. What is
output
? That’s theOutputStream
of the door? As theSerialPort
is configured on the java side? Where are you reading the C-side string? Maybe this example of Soen clarify a few things.– Anthony Accioly
What is the content of these strings you are trying to upload? Why does the upload method use the parameters
dados
andp1
, but does not use thep2
and thep3
?– Victor Stafusa
sorry if I was unclear in my question , this output would be to send to Arduino, @Anthony Accioly *? public void startConexao(){ Commportidentifier portaId = null; Enumeration portaEnum = Commportidentifier.getPortIdentifiers(); while(portaEnum.hasMoreElements()){
 CommPortIdentifier atualPortaId =(CommPortIdentifier) portaEnum.nextElement();
 if(porta.equals(atualPortaId.getName())){
 portaId=atualPortaId;
 break;
 }
 }
 **
– Jefferson Santos
@Victorstafusa I am trying to send the following command , The data is referring to the command if you turn on or delete the LED, already P1, P2,P3, is configuring which pin will be connected.
– Jefferson Santos
@Jeffersonsantos Edit the question to make it clearer. I don’t understand the strings, what are the possible contents of each string? It seems to me that these strings should be
boolean
s.– Victor Stafusa
Enter the function code
Desligado
from the West as well.– Victor Stafusa
@Victorstafusa I changed the question I know if now ta but clear
– Jefferson Santos
@Jeffersonsantos I already have an answer in half, but there’s still one thing left for me to finish it: Who is calling the method
enviarDados
?– Victor Stafusa