-1

I am attempting to create an applet to replace my ActiveX control because it recently started having problems even though it has worked for years. All this will do is read data from a serial port, a barcode scanner in my case, and pass the data to an input box. Also any pointers on ways I could make this better would be greatly appreciated.

Code:

package checkin;

import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import netscape.javascript.*;
import java.applet.Applet;

public class MainSerial extends Applet {

static SerialPort port;
static JSObject window;

public static void main(String[] args) { //WARNING
    SerialPort port = new SerialPort("COM1");
    try {
        port.openPort();
        port.setParams(9600, 8, 1, 0);
        int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;
        port.setEventsMask(mask);
        port.addEventListener(new SerialPortReader()); //ERROR
    } catch (SerialPortException ex) {
        window.eval("alert('Could not open port.\nThe port may be in use by another program.')");
    }
}

class SerialPortReader implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR()) {
            try {
                byte buffer[] = port.readBytes();

                int x = 0;
                int count = buffer.length;
                StringBuilder data = new StringBuilder();

                while(x < count) {
                    data.append(buffer[x]);
                    x++;
                }

                window.eval("getSerialData('" + data.toString() + "')");
            } catch (SerialPortException ex) {
                window.eval("alert('Port could not be read.')");
            }
        }
    }
}

}

Error Thrown with Eclipse:

The serializable class MainSerial does not declare a static final serialVersionUID field of type Long

Warning thrown with Eclipse:

No enclosing instance of MainSerial is accessible. Must qualify the allocation with an enclosing instance of type MainSerial.

This is my first attempt at Java, please excuse my ignorance. What doesn't make sense to me is why do I see these errors in Eclipse but not NetBeans? Also, if the line

port.addEventListener(new SerialPortReader());

Is in that specific parent why would I need to reinitialize it? I'm sure I'm missing something small and probably quite obviously but any help is greatly appreciated. The reason I am attempting to create an applet is due to the increasing problems with my ActiveX control.

2

2 Answers 2

1

Eclipse's compiler has a lot of configurable warnings and errors that other compilers may or may not have. In general the default settings are quite reasonable and you should try to fix them if possible. Many can easily be fixed using the Quick Fix feature: right-click on an error in the Markers (or Problems) view and choose Quick Fix.... enter image description here

Alternatively, you can right-click on the underline (yellow or red) in the code where the error or warning is, and choose Quick Fix.... enter image description here

Not every warning/error has a quick fix, but many do.

If you want to disable a particular warning or error, right-click your project and open the Properties dialog. In there, look under Java Compiler > Errors/Warnings to find all of the configurable ones. You'll have to Enable project specific settings unless you want the same settings to apply to all projects in your workspace.

0
1

Eclipse typically throws a warning around ServialVersionUID when you create a serializable object without specifying a serial version ID. having serial version IDs is a best practice, not a strict requirement, and differend IDEs behave differently about it. More info on why you may want it can be found here. Please note: this should be a warning, not an error. If it is a warning, there are three ways of avoiding this:

  • Ask Eclipse to generate a default ID for you (it will generate private static final long serialVersionUID = 1L;)
  • Generate one your self
  • Tell Eclipse to ignore the warning (adding the @SuppressWarnings("serial") annotation)

For the other issue, there is a wide liteerature out there. Here is a link you may want to read.

Hope this helps.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.