Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Timer;
- import java.util.TimerTask;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import jssc.SerialPort;
- import jssc.SerialPortException;
- public class main extends TimerTask{
- static SerialPort serialPort = new SerialPort("COM4");
- static Connection con = null;
- byte sync = 0b01010101;
- byte addr = 0b01110011;
- byte servo=0;
- byte led=0;
- @Override
- public void run() {
- PreparedStatement statement;
- try {
- statement = con.prepareStatement("select * from control");
- ResultSet result = statement.executeQuery();
- while(result.next())
- {
- System.out.println(result.getString(1) + " " + result.getString(2) +" " + result.getString(3));
- servo = (byte) Integer.parseInt(result.getString(2));
- led = (byte) Integer.parseInt(result.getString(3));
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- serialPort.writeByte(sync);
- serialPort.writeByte(addr);
- serialPort.writeByte(led);
- serialPort.writeByte(servo);
- }
- catch (SerialPortException ex) {
- System.out.println(ex);
- }
- }
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/arduino","root","");
- try {
- serialPort.openPort();//Open serial port
- serialPort.setParams(SerialPort.BAUDRATE_9600,
- SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
- }
- catch (SerialPortException ex) {
- System.out.println(ex);
- }
- TimerTask timerTask = new main();
- Timer timer = new Timer(true);
- timer.scheduleAtFixedRate(timerTask, 0, 3);
- try {
- Thread.sleep(120000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- timer.cancel();
- serialPort.closePort();//Close serial port
- con.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement