Serial Communication with Precision Scale

3

I'm developing an application to measure the flow of solder that passes through the board on a production line, but I'm having a bit of a hard time connecting the precision balance with the PC via a serial port. I'm using JAVA language and api javax.comm, if anyone can help me or if I know another way to make this communication, help me !!!

    
asked by anonymous 03.07.2015 / 15:55

1 answer

1

Well I work a lot with serial here and I will pass you some tips to the javax.comm API. It is well deefasada, I recommend using java rxtx (not hard to find), so it works have to use their respective DLLs according to oo Java installed (x64 OS and Java x86, x86 DLL), here are some examples of codes:

import gnu.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.ArrayList;

public class serial{
    public SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

/**
 * Get the serial ports available
 * @return The existing ports
 */
public String[] getPortas (){
    ArrayList<String> portas = new ArrayList<String>();
    //System.out.println(java.library.path);
    CommPortIdentifier serialPortId;
    //static CommPortIdentifier sSerialPortId;
    Enumeration<?> enumComm;
    //SerialPort serialPort;
    enumComm = CommPortIdentifier.getPortIdentifiers();
    while (enumComm.hasMoreElements()) {
        serialPortId = (CommPortIdentifier) enumComm.nextElement();
        if(serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            portas.add(serialPortId.getName());
        }
    }
    return portas.toArray(new String[portas.size()]);
}

/**
 * Connect to the given port
 *
 * @param portName The name from the port to be connected
 * @throws NoSuchPortException
 * @throws PortInUseException
 * @throws IOException
 */
public void connect(String portName) throws NoSuchPortException, PortInUseException, IOException {
        // Obtain a CommPortIdentifier object for the port you want to open
        CommPortIdentifier portId =
                CommPortIdentifier.getPortIdentifier(portName);

        // Get the port's ownership
        serialPort =
                (SerialPort) portId.open("Demo application", 5000);

        // Set the parameters of the connection.
        setSerialPortParameters();

        // Open the input and output streams for the connection. If they won't
        // open, close the port before throwing an exception.
        outStream = serialPort.getOutputStream();
        inStream = serialPort.getInputStream();

        serialPort.notifyOnDataAvailable(true);
}

public InputStream getSerialInputStream() {
    return inStream;
}

public OutputStream getSerialOutputStream() {
    return outStream;
}

private void setSerialPortParameters() throws IOException {
    int baudRate = 9600; // 19200bps
    try {
        // Set serial port to 19200bps-8N1
        serialPort.setSerialPortParams(
                baudRate,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        serialPort.setFlowControlMode(
                SerialPort.FLOWCONTROL_NONE);
    } catch (UnsupportedCommOperationException ex) {
        throw new IOException("Unsupported serial port parameter");
    }
}


public void disconnect() {
    if (serialPort != null) {
        try {
            outStream.close();
            inStream.close();
            serialPort.notifyOnDataAvailable(false);
            serialPort.removeEventListener();
        } catch (IOException ex) {
            // don't care
        }
        // Close the port.
        serialPort.close();
    }
}

public void enviaString (String b) throws IOException{
    enviaBytes(b.getBytes());
}


public void enviaBytes (byte[] b){
    try {
        outStream.write(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

With this class it is possible to send and receive Serial data, in the main class it is necessary to connect to some port (the available ports can be seen with the method getPortas in the class above), and create the listener to receive the data. / p>

serial porta = new serial();
porta.connect(n);
porta.serialPort.addEventListener(new SerialPortEventListener() {

                                @Override
                                public void serialEvent(SerialPortEvent arg0) {
                                    switch (arg0.getEventType()) {
                                    case SerialPortEvent.DATA_AVAILABLE:
                                        leSerial();
                                        break;
                                    }
                                }
                            });

private void leSerial() {
    InputStream inStream = porta.getSerialInputStream();
    try {
        int availableBytes = inStream.available();
        byte dadosRecebidos[] = new byte[availableBytes];
        if (availableBytes > 0) {
            // Read the serial port
            inStream.read(dadosRecebidos, 0, availableBytes);
        }
    } catch (IOException e) {

    }
}

and to send data use the methods of the serial class enviaString and sendBytes.

    
08.07.2015 / 16:17