Advertisement
Guest User

xd

a guest
Nov 4th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. import jssc.SerialPort;
  11. import jssc.SerialPortException;
  12.  
  13. public class main extends TimerTask{
  14.    
  15.     static SerialPort serialPort = new SerialPort("COM4");
  16.  
  17.     static Connection con = null;
  18.    
  19.     byte sync = 0b01010101;
  20.     byte addr = 0b01110011;
  21.     byte servo=0;
  22.     byte led=0;
  23.    
  24.    
  25.     @Override
  26.     public void run() {
  27.    
  28.         PreparedStatement statement;
  29.         try {
  30.             statement = con.prepareStatement("select * from control");
  31.    
  32.         ResultSet result = statement.executeQuery();
  33.  
  34.         while(result.next())
  35.         {
  36.            
  37.         System.out.println(result.getString(1) + " " + result.getString(2) +" " + result.getString(3));
  38.         servo = (byte) Integer.parseInt(result.getString(2));
  39.         led = (byte) Integer.parseInt(result.getString(3));
  40.    
  41.         }
  42.        
  43.        
  44.         } catch (SQLException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         }
  48.        
  49.        
  50.         try {
  51.            
  52.             serialPort.writeByte(sync);
  53.             serialPort.writeByte(addr);
  54.             serialPort.writeByte(led);
  55.             serialPort.writeByte(servo);
  56.         }
  57.         catch (SerialPortException ex) {
  58.             System.out.println(ex);
  59.         }
  60.                
  61.     }
  62.    
  63.    
  64.    
  65.     public static void main(String[] args) throws Exception {
  66.  
  67.         // TODO Auto-generated method stub
  68.        
  69.         Class.forName("com.mysql.jdbc.Driver");
  70.  
  71.         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/arduino","root","");
  72.  
  73.         try {
  74.             serialPort.openPort();//Open serial port
  75.             serialPort.setParams(SerialPort.BAUDRATE_9600,
  76.                                  SerialPort.DATABITS_8,
  77.                                  SerialPort.STOPBITS_1,
  78.                                  SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
  79.         }
  80.         catch (SerialPortException ex) {
  81.             System.out.println(ex);
  82.         }
  83.        
  84.        
  85.         TimerTask timerTask = new main();
  86.        
  87.         Timer timer = new Timer(true);
  88.            
  89.         timer.scheduleAtFixedRate(timerTask, 0, 3);
  90.        
  91.        
  92.         try {
  93.             Thread.sleep(120000);
  94.         } catch (InterruptedException e) {
  95.             e.printStackTrace();
  96.         }
  97.        
  98.         timer.cancel();
  99.         serialPort.closePort();//Close serial port
  100.         con.close();
  101.        
  102.        
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement