我通过此代码从Java的COM端口接收一些输入。

import java.io.*;
import java.util.*;

import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

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();
            }
        }
    }
}

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);}
        break;
    }
}}


输入如下:

447646

447647

447648

447649

我需要在名为“ asset”的MySql表中输入这些值。包含两个字段ID,TIMESTAMP。
ID是上面的串行输入,时间戳是输入发生的时间。

MySql查询将是什么样的?

st.executeUpdate("INSERT into asset VALUES(What to put here??);


一些帮助会非常好

更新代码:

好的,这是我的SerialEvent完整代码:

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 Id = new String(readBuffer);
            long timet = new getTime();
            st.executeUpdate("INSERT into asset(id,timet) VALUES("+id+","+timet+"");
            con.close();
        }
        catch(Exception ex){
            System.out.println(ex);
                    }
                    break;
        }
    }

最佳答案

String id = new String(readBuffer);

String query = "INSERT into asset(ID,TIMESTAMP) VALUES(?,?)";
PreparedStatement pstm = con.prepareStatement(query);
pstm.setString(1, id);
pstm.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
pstm.executeUpdate();

关于java - 从COM到MySql的串行输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16346665/

10-11 22:34
查看更多