2017-05-08 101 views
0

对于我的组最终项目,我们使用的是带有ph传感器的Arduino。我有从ph传感器制造商提供的提供的代码,其工作良好。我有从Arduino到Java的通信,并且还显示代码在输出中运行时的信息。 mysql语句是正确的,所有的字段都被添加。但是,当来自for循环的数据写入数据库时​​,它会执行一些操作,并创建一个大数。我正在考虑使用数组列表来获取数据,然后平均执行。我需要有in.read();分配给数组?如何将数据从Arduino转换为平均计算的mysql数据库

 package Control; 

    import Model.fluidTest; 
    import java.io.*; 
    import java.util.*; 
    import com.fazecast.jSerialComm.SerialPort; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 


    public class ComPortReader { 

/** 
* 
* @param args 
* @throws FileNotFoundException 
* @throws UnsupportedEncodingException 
*/ 
    public static void main(String[] args) throws FileNotFoundException, 
    UnsupportedEncodingException, ClassNotFoundException, SQLException, 
    IOException { 
    //********************************************************************** 
    /** 
    * These are preparation for the database to be connected and write data 
    * to database. 
    * 
    */ 
    PreparedStatement reading = null; 
    Connection con = null; 
    //********************************************************************** 
    /** 
    * This section of code will be used to take the information from the 
    * for loop and place it into a array list to perform the average of the 
    * ph input from the Arduino. 
    * 
    * The input is being added together while uploading to the MYSQL 
    * database which is producing incorrect ph level readings. 
    */ 

    // float average 
    //********************************************************************** 
    /** 
    * This is to open the communication port on the computer to talk to the 
    * input device. 
    */ 
    SerialPort comPort = SerialPort.getCommPorts()[0]; 
    comPort.openPort(); 
    comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 
    0); 
    InputStream in = comPort.getInputStream(); 
    try { 
     for (int j = 0; j < 100; j++) { 
     System.out.print((char) in.read()); 
     } 
     in.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    /** 
    * This begins the query to the MYSQL database we are connecting to the 
    * database using default parameters while using user defined user login 
    * and password. 
    */ 




    try {// Try catch to possibly write data to a mysql database. 
     con = 
    DriverManager.getConnection("jdbc:mysql://localhost:3306/phlevel? 
    relaxAutoReconnect=false&useSSL=false&relaxAutoCommit=true", 

     String sql = "INSERT INTO 
    phlevel.inputreadings(id,phInput,javaReading) values(?,?,?)"; 
     reading = con.prepareStatement(sql); 
     reading.setInt(1, 0); // Creates a new ID Field For performed test. 
     reading.setFloat(2, in.read()); 
     // Creates new Float Field for phLevel 
     reading.setString(3, "New Reading---->"); 
     [enter image description here][1]// Creates Label for reading   
     reading.executeUpdate(); 

     // End MYSQL update QUERY 

     con.commit();//End connection to mysql 
     reading.close();// Finish statements to mysql 
     comPort.closePort(); // Close the Serial port connected to ardiuno. 

    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 

} 
} 

回答

0
 final int SAMPLE_DATA_COUNT = 100; 

     float averagePH = 0; 

     for (int j = 0; j < SAMPLE_DATA_COUNT; j++) { 
     System.out.print((char) in.read()); 
     averagePH += Float.parseFloat(in.read()); 
     } 

     averagePH /= SAMPLE_DATA_COUNT; // Insert this value to SQL 
     ... 
     reading.setFloat(2, averagePH); 

我觉得这条线

reading.setInt(1, 0); 

不应该有摆在首位,该ID(主键)列应设置为自动递增,你不需要在插入数据时指定它。

+0

我用你的例子,它工作得很好。我只需要再添加一件东西就可以工作。我收到一个错误。感谢大家帮助我奠定基础,找到解决方案'averagePH + = Float.parseFloat(Integer.toString(in.read()));' –

相关问题