0

所以我有这个程序,它从 COM 端口读取串行输入并将其放在名为资产的 MySql 数据库中。

代码:

import java.io.*;
import java.util.*;
import gnu.io.*;
import java.sql.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static Connection con=null;
static String url = "jdbc:mysql://localhost:3306/track";
static String uid="root";
static String pwd="root";

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
    portId = (CommPortIdentifier) portList.nextElement();
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
         if (portId.getName().equals("COM1")) {
    //                if (portId.getName().equals("/dev/term/a")) {
            SimpleRead reader = new SimpleRead();
                try
                {

                    Class.forName("com.mysql.jdbc.Driver");
                    con =     DriverManager.getConnection(url,uid,pwd);
                }
                catch(Exception s){
                    System.out.println(s);
                    }

    }
}
}
 }
 public SimpleRead() {
 try {
     serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
 } catch (PortInUseException e) {System.out.println(e);}
 try {
    inputStream = serialPort.getInputStream();
 } catch (IOException e) {System.out.println(e);}
 try {
    serialPort.addEventListener(this);
 } catch (TooManyListenersException e) {System.out.println(e);}
 serialPort.notifyOnDataAvailable(true);
 try {
    serialPort.setSerialPortParams(9600,
        SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
  } catch (UnsupportedCommOperationException e) {System.out.println(e);}
  readThread = new Thread(this);
  readThread.start();
  }

public void run() {
try {
    Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[20];

    try {
        while (inputStream.available() > 0) {
            int numBytes = inputStream.read(readBuffer);
        }
        System.out.print(new String(readBuffer));
    } catch (IOException e) {System.out.println(e);}

    Statement st=null;
    try{
        st=con.createStatement();

    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }

    try{
        String idd = new String(readBuffer);
        String query = "INSERT INTO asset (id) VALUES(?)";
        PreparedStatement pstm = con.prepareStatement(query);
        pstm.setString(1, idd);
        pstm.executeUpdate();
        pstm.close();
        con.close();

        //st.executeUpdate("insert into asset(id) VALUES('"+idd+"'");
        //con.close();
    }
    catch(Exception ex){
        System.out.println(ex);
                }
                break;
    }
}
}

因此,每当从 COM 端口接收数据时,上述代码都会在 mysql 表中创建一个条目。

预期的条目应该是这样的:

预期图像

我得到什么实际输出:

真实图像

实际发生的情况是,来自 com 端口的一个输入在表中创建了两个条目,一个带有串行输入和时间戳,另一个只有时间戳。

我想要实现的是每个 COM 输入只有 1 个条目。像预期的图像。

4

1 回答 1

0

在将 ID 插入数据库之前检查 ID -

    String idd = new String(readBuffer);
    if(!idd.equals("") || !idd.equals(null)){
       String query = "INSERT INTO asset (id) VALUES(?)";
       PreparedStatement pstm = con.prepareStatement(query);
       pstm.setString(1, idd);
       pstm.executeUpdate();
       pstm.close();
       con.close();
    }
于 2013-05-02T21:56:15.693 回答