Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package portaserie;
- import gnu.io.CommPortIdentifier;
- import gnu.io.SerialPort;
- import gnu.io.NoSuchPortException;
- import gnu.io.PortInUseException;
- import gnu.io.UnsupportedCommOperationException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import static java.lang.Thread.sleep;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Enumeration;
- public class Porta_Serie {
- private static BufferedWriter bw;
- private static BufferedReader br;
- private static OutputStream os ;
- private static InputStream is ;
- private static InputStreamReader isr;
- private static OutputStreamWriter osr ;
- private static int baudRate = 115200;
- private static String Porta = "COM3";
- String login = "root";
- String password = "";
- String url = "jdbc:mysql://localhost:3306/moveis";
- Connection c;
- public Porta_Serie() throws InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- c = DriverManager.getConnection(url, login, password);
- Enumeration PortasCOM = CommPortIdentifier.getPortIdentifiers();
- while (PortasCOM.hasMoreElements()) {
- CommPortIdentifier porta = (CommPortIdentifier) PortasCOM.nextElement();
- System.out.println(porta.getName());
- }
- try {
- CommPortIdentifier porta = CommPortIdentifier.getPortIdentifier(Porta);
- SerialPort portaCOM = (SerialPort) porta.open("Teste_Ready", 1000);
- portaCOM.setSerialPortParams(
- baudRate,
- SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);
- System.out.println("Em principio programou a porta" + Porta );
- is = portaCOM.getInputStream();
- os = portaCOM.getOutputStream();
- isr = new InputStreamReader(is);
- osr = new OutputStreamWriter(os);
- br = new BufferedReader(isr);
- bw = new BufferedWriter(osr);
- // portaCOM.close();
- } catch (IOException ex) {
- System.out.println("Erro de entrada/saida");
- // muitas vezes ocorre este erro porque no windows da erro se nao houver dados no buffer
- ex.printStackTrace();
- } catch (NoSuchPortException ex) {
- System.out.println("Nao encontrou a porta especificada");
- ex.printStackTrace();
- } catch (PortInUseException ex) {
- System.out.println("Porta especificada ja em uso");
- ex.printStackTrace();
- } catch (UnsupportedCommOperationException ex) {
- System.out.println("Parametros Incorretos/Incompativeis");
- }
- }
- public void test_connection() throws IOException, InterruptedException
- {
- bw.write("ate1\r");
- bw.flush();
- sleep(500);
- read_buffer();
- }
- public void read_buffer() throws IOException
- {
- String line;
- while(true)
- {
- line = br.readLine();
- System.out.println(line);
- if(line != null && (line.equals("OK")) || line.equals("ERROR"))
- break;
- }
- }
- public void enter_pin() throws IOException, InterruptedException
- {
- bw.write("at+cpin=\""+ 7777 +"\"\r");
- bw.flush();
- sleep(500);
- read_buffer();
- bw.write("at+cpin=\""+"?"+"\"\r");
- bw.flush();
- sleep(500);
- read_buffer();
- }
- public void text_mode() throws IOException, InterruptedException
- {
- bw.write("at+cmgf=1\r");
- bw.flush();
- sleep(500);
- read_buffer();
- }
- public void Send_SMS() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException, InterruptedException
- {
- String a;
- System.out.println("aqui");
- ResultSet rs;
- Statement ps;
- String query = "select * from clientes";
- ps = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs = ps.executeQuery(query);
- String Message = "Vote";
- while(rs.next())
- {
- bw.write("at+cmgs=\""+rs.getString(2)+"\"\r");
- bw.flush();
- sleep(500);
- bw.write("Vote sim ou nao");
- bw.flush();
- sleep(500);
- os.write(26);
- os.flush();
- sleep(4000);
- read_buffer();
- }
- }
- public void Configure_Notification() throws IOException, InterruptedException
- {
- String s = "1,1,0,0,1";
- boolean a;
- bw.write("at+cmni=\"" + s + "\"\r");
- bw.flush();
- sleep(500);
- }
- public void Read_SMS() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException, InterruptedException
- {
- String line_r = new String();
- ResultSet rs;
- Statement ps;
- String query = "select * from Votation";
- ps = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- int yes = 0;
- int no = 0;
- for(;;)
- {
- while(is.available() < 1)
- {
- }
- bw.write("at+cmgl=\""+"rec unread"+"\"\r");
- bw.flush();
- sleep(500);
- line_r = Read_Buffer_SMS();
- String[] parts = string.split("-");
- query = query + "asd";
- rs = ps.executeQuery(query);
- }
- }
- public String [] Read_Buffer_SMS() throws IOException{
- String [] line = new String[10];
- int i = 0;
- while(true)
- { line [i] = br.readLine();
- i = i + 1;
- System.out.println(line[i]);
- if(line != null && (line.equals("OK")) || line.equals("ERROR"))
- break;
- }
- return line;
- }
- public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
- Porta_Serie porta_Serie = new Porta_Serie();
- porta_Serie.test_connection();
- //porta_Serie.enter_pin();
- porta_Serie.text_mode();
- porta_Serie.Send_SMS();
- /* c = porta_Serie.Send_SMS();
- porta_Serie.Configure_Notification();
- porta_Serie.Read_SMS(); */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement