NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

JSSC and arduino Lubuntu + Netbeans

 
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users
View previous topic :: View next topic  
Author Message
nando88



Joined: 16 Aug 2013
Posts: 3

PostPosted: Mon Apr 13, 2015 12:37 am    Post subject: JSSC and arduino Lubuntu + Netbeans Reply with quote

I'm using jssc library, for java & arduino communication.
I have a java app that opens the serial port and then sends a 1 as a byte to the arduino. Then the arduino sends a 1 as byte back to the java app, and the app should print "arduino", but what is printed instead is port open but no arduino.
Here is my arduino code:
Code:

//Setup message bytes

int pin = 31;
byte buf=1;


void setup() {
  Serial.begin(9600);
  pinMode(pin,OUTPUT);
}

void loop() {           
       //Detect Command type
       if(Serial.read()==(byte)1){       
       Serial.println(Serial.write(buf));
       delay(1000);
       //Serial.print((byte)2);
       }
       if(Serial.read()==(byte)49)
       digitalWrite(pin,HIGH);
       if(Serial.read()==(byte)48)
       digitalWrite(pin,LOW);
  }   


Here is my java code in netbeans:
Code:

package serialscan;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package TestBatery001;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
//import jPicUsb.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;
import javax.swing.JTextField;
import javax.swing.Painter;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicProgressBarUI;
//import Serialio.*;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortException;
import jssc.SerialPortList;

/**
 *
 * @author Faster
 */
public class Barra extends Thread {

    JLabel com;
    int i = 0;
    SerialPort port;   
    byte[] inBytes;

    public Barra(JLabel com) throws Exception {

        this.com = com;

    }

    public void run() {
        while (true) {
            //String puerto = "/dev/ttyACM" + i;
            String puerto = "/dev/ttyACM" + (i);
            port = new SerialPort(puerto);           
            System.setProperty("JSSC_NO_TIOCEXCL", "");
            try {
                if (!port.isOpened()) {
                    port.openPort();
                    TimeUnit.SECONDS.sleep(10);
                    port.writeByte((byte) 1); 
                    inBytes = port.readBytes();
                    //Read
                    System.out.println("1. port " + puerto + " is opened");
                }
            } catch (SerialPortException ex) {
                Logger.getLogger(Barra.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(Barra.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                if (!port.isOpened()) {
                    i++;

                    System.out.println("2. port " + puerto + " is not opened!");
                    if (i == 10) {
                        i = 0;
                    }
                } else if (port.isOpened()) {
                    TimeUnit.SECONDS.sleep(10);                   
                    if (inBytes != null && inBytes.length >= 1 && inBytes[0] == (byte) 1) {
                        System.out.println("arduino");
                        System.out.println("2. port " + puerto + " is opened!");
                    } else {
                        System.out.println("3. port " + puerto + " is opened! But NOT ARDUINO");
                    }

                }
            } catch (InterruptedException ex) {
                Logger.getLogger(Barra.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

By the way, I'm doing this on an ubuntu like distribution, that is why I have:
Code:

"/dev/ttyACM"+i

as a port, and I'm also changing ports, in order to see to which one is my arduino connected to.
The arduino receives the data fine, and it writes the byte on the serial port, because I can see it in the serial monitor. The thing that they told me on the arduino forum is that I'm not reading the data after I open the port, and I tried that, but with no luck.
Can someone please help me fix this problem?
Thanks.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo