Go Down

Topic: interfacing with java (Read 856 times) previous topic - next topic

soulchyld

Hi guys, Im still pretty new to the world of robotics and programming, I am trying to create a controller for my robot in java, Im using the demalanovue board, I have managed to find a sketch which works using hyperterminal and the serial monitor, however when I try to run it using my java program theres a little light labelled L next to the power, rx and tx leds that flashes a couple of times, giving me the impression the data is being recieved! does anybody know if my assumption is correct and have any ideas, btw, my board is a dfrobot version of the demalanovue not the actual!

a copy of my java code is shown below if anyone wishes to have a look,

import javax.comm.*;
import java.io.*;
import java.util.*;


public class ComControl {

    /**
     * @param args the command line arguments
     */

    static CommPortIdentifier portid;
    static CommPortIdentifier portid1;
    InputStream inputStream;
    OutputStream outputStream;
    SerialPort serialPort1, serialPort2;
    Thread readThread;
    protected int divertCode = 1;
    static String TimeStamp;


    public static void main(String[] args) {
        // TODO code application logic here
        try {
          portid = CommPortIdentifier.getPortIdentifier("COM3");
          portid1 = CommPortIdentifier.getPortIdentifier("COM4");
          ComControl reader = new ComControl();
      }

    catch
      (Exception e) {
      TimeStamp = new java.util.Date().toString();
      System.out.println(TimeStamp + ": COM3 " + portid);
      System.out.println(TimeStamp + ": COM4 " + portid1);
      System.out.println(TimeStamp + ": msg1 - " + e);
     }
    };

    public ComControl(){

        try {
            TimeStamp = new java.util.Date().toString();
            serialPort1 = (SerialPort) portid1.open("ComControl", 2000);
            System.out.println(TimeStamp + ": " + portid1.getName() + " opened for sending data");
           
           

        }

        catch (PortInUseException e) {}

        try {



        serialPort1.setSerialPortParams(9600,
                SerialPort.DATABITS_7,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_EVEN);

        } catch (UnsupportedCommOperationException e) {}


        try{
             outputStream = serialPort1.getOutputStream();
             outputStream.write(divertCode);
                   System.out.println(TimeStamp + ": data sent");
                   outputStream.close();

            } catch (IOException e) {}


        }

    }

PaulS

Quote
theres a little light labelled L next to the power, rx and tx leds that flashes a couple of times, giving me the impression the data is being recieved! does anybody know if my assumption is correct

That's the wrong impression. The TX LED will flash when you are sending data. The RX LED will flash when you are receiving data. The L LED will flash when you tell it to.

Quote
and have any ideas

I have an idea that you should be using the # button when posting code.

I have another idea that you might want to tell us what problems you have, if any.


soulchyld

Thanks, will use it to post code next time!
is that the LED that is connected to pin 13? I have not included it in my sketch on the arduino so I havent told it to flash!
it flashes a couple of times when I connect the board to my pc and everytime I run my java code! The main problem is that it doesnt respond when I run the code posted earlier!

PaulS

Opening a serial port connection causes the Arduino to reset. That takes a few milliseconds, at least. There is no delay in your java code to wait for the Arduino to complete the reset and be ready to read serial data, so the serial data that the java app sends is lost.

Closing a serial port connection also causes the Arduino to reset.

So, your java app is basically accomplishing nothing more than resetting the Arduino twice.

You need to change your java app to isolate opening the serial port from the sending of data. Open the port once. Then, you can send data multiple times. When the java app ends, close the serial port.

soulchyld

Thanks, I  have tried to isolate my code and keep getting an error "java.lang.nullpointerexception" does anybody know why this error comes, the documantation for this library that I have found os not very helpful!
Code: [Select]
public void start()
    {
   
        try{
            portid1.getPortIdentifier("COM4");
           // portid1.open("start", 2000);
           serialPort1 = (SerialPort) portid1.open("connect", 2000);
            JOptionPane.showMessageDialog(null,"Connection Established");
           
        }
        catch (Exception e)
        {
           JOptionPane.showMessageDialog(null,e);
       // System.out.println(e);
        }


I have tried both the uncommmented line and the commmented line of code and still get the  same error!

Hey Paul S Also I have tried adding up to a six second delay before sending the data to the board (using the previous code)
but it still didnt work!

PaulS

The portid1 object is initialized by the getPortIdentifier() call. Then, the open() method is called, which is most likely what is throwing the exception, indicating that the getPortIdentifier() call failed. Why? Most likely because COM4 is not available. Are you sure that that is the port that the Arduino is attached to, and that there are no other applications trying to use that port (IDE, Serial Monitor, etc.)?

soulchyld

Hi,  nothing else was connected at the time, if I comment out the lines of code that open the connection the getPortIdentifier()works, so it is something to do with the open() function, what are the required parameters for this function?
a nullpointerexception does mean incorrect parameters/arguments right?

Go Up