Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. package portaserie;
  2.  
  3. import gnu.io.CommPortIdentifier;
  4. import gnu.io.SerialPort;
  5. import gnu.io.NoSuchPortException;
  6. import gnu.io.PortInUseException;
  7. import gnu.io.UnsupportedCommOperationException;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.BufferedReader;
  12. import java.io.BufferedWriter;
  13. import java.io.OutputStream;
  14. import java.io.OutputStreamWriter;
  15. import static java.lang.Thread.sleep;
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.Enumeration;
  22.  
  23.  
  24. public class Porta_Serie {
  25.  
  26. private static BufferedWriter bw;
  27. private static BufferedReader br;
  28. private static OutputStream os ;
  29. private static InputStream is ;
  30. private static InputStreamReader isr;
  31. private static OutputStreamWriter osr ;
  32. private static int baudRate = 115200;
  33. private static String Porta = "COM3";
  34. String login = "root";
  35. String password = "";
  36. String url = "jdbc:mysql://localhost:3306/moveis";
  37. Connection c;
  38.  
  39. public Porta_Serie() throws InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
  40.  
  41.  
  42. Class.forName("com.mysql.jdbc.Driver").newInstance();
  43. c = DriverManager.getConnection(url, login, password);
  44.  
  45. Enumeration PortasCOM = CommPortIdentifier.getPortIdentifiers();
  46. while (PortasCOM.hasMoreElements()) {
  47. CommPortIdentifier porta = (CommPortIdentifier) PortasCOM.nextElement();
  48. System.out.println(porta.getName());
  49. }
  50.  
  51. try {
  52.  
  53. CommPortIdentifier porta = CommPortIdentifier.getPortIdentifier(Porta);
  54. SerialPort portaCOM = (SerialPort) porta.open("Teste_Ready", 1000);
  55.  
  56. portaCOM.setSerialPortParams(
  57. baudRate,
  58. SerialPort.DATABITS_8,
  59. SerialPort.STOPBITS_1,
  60. SerialPort.PARITY_NONE);
  61.  
  62. System.out.println("Em principio programou a porta" + Porta );
  63.  
  64. is = portaCOM.getInputStream();
  65. os = portaCOM.getOutputStream();
  66.  
  67. isr = new InputStreamReader(is);
  68. osr = new OutputStreamWriter(os);
  69.  
  70. br = new BufferedReader(isr);
  71. bw = new BufferedWriter(osr);
  72.  
  73. // portaCOM.close();
  74. } catch (IOException ex) {
  75. System.out.println("Erro de entrada/saida");
  76. // muitas vezes ocorre este erro porque no windows da erro se nao houver dados no buffer
  77. ex.printStackTrace();
  78. } catch (NoSuchPortException ex) {
  79. System.out.println("Nao encontrou a porta especificada");
  80. ex.printStackTrace();
  81. } catch (PortInUseException ex) {
  82. System.out.println("Porta especificada ja em uso");
  83. ex.printStackTrace();
  84. } catch (UnsupportedCommOperationException ex) {
  85. System.out.println("Parametros Incorretos/Incompativeis");
  86. }
  87. }
  88.  
  89. public void test_connection() throws IOException, InterruptedException
  90. {
  91. bw.write("ate1\r");
  92. bw.flush();
  93. sleep(500);
  94. read_buffer();
  95. }
  96.  
  97. public void read_buffer() throws IOException
  98. {
  99. String line;
  100.  
  101. while(true)
  102. {
  103. line = br.readLine();
  104. System.out.println(line);
  105.  
  106. if(line != null && (line.equals("OK")) || line.equals("ERROR"))
  107. break;
  108.  
  109.  
  110. }
  111.  
  112. }
  113.  
  114. public void enter_pin() throws IOException, InterruptedException
  115. {
  116. bw.write("at+cpin=\""+ 7777 +"\"\r");
  117. bw.flush();
  118. sleep(500);
  119. read_buffer();
  120.  
  121. bw.write("at+cpin=\""+"?"+"\"\r");
  122. bw.flush();
  123. sleep(500);
  124. read_buffer();
  125.  
  126.  
  127. }
  128. public void text_mode() throws IOException, InterruptedException
  129. {
  130. bw.write("at+cmgf=1\r");
  131. bw.flush();
  132. sleep(500);
  133. read_buffer();
  134. }
  135.  
  136. public void Send_SMS() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException, InterruptedException
  137. {
  138. String a;
  139. System.out.println("aqui");
  140. ResultSet rs;
  141. Statement ps;
  142. String query = "select * from clientes";
  143. ps = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  144. ResultSet.CONCUR_READ_ONLY);
  145. rs = ps.executeQuery(query);
  146. String Message = "Vote";
  147.  
  148. while(rs.next())
  149. {
  150. bw.write("at+cmgs=\""+rs.getString(2)+"\"\r");
  151. bw.flush();
  152. sleep(500);
  153. bw.write("Vote sim ou nao");
  154. bw.flush();
  155. sleep(500);
  156. os.write(26);
  157. os.flush();
  158. sleep(4000);
  159.  
  160. read_buffer();
  161. }
  162.  
  163. }
  164.  
  165. public void Configure_Notification() throws IOException, InterruptedException
  166. {
  167. String s = "1,1,0,0,1";
  168. boolean a;
  169. bw.write("at+cmni=\"" + s + "\"\r");
  170. bw.flush();
  171. sleep(500);
  172.  
  173.  
  174. }
  175.  
  176. public void Read_SMS() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException, InterruptedException
  177. {
  178.  
  179. String line_r = new String();
  180. ResultSet rs;
  181. Statement ps;
  182. String query = "select * from Votation";
  183. ps = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  184. ResultSet.CONCUR_READ_ONLY);
  185.  
  186. int yes = 0;
  187. int no = 0;
  188.  
  189. for(;;)
  190. {
  191.  
  192. while(is.available() < 1)
  193. {
  194.  
  195. }
  196. bw.write("at+cmgl=\""+"rec unread"+"\"\r");
  197. bw.flush();
  198. sleep(500);
  199. line_r = Read_Buffer_SMS();
  200.  
  201. String[] parts = string.split("-");
  202.  
  203. query = query + "asd";
  204. rs = ps.executeQuery(query);
  205.  
  206.  
  207. }
  208. }
  209.  
  210. public String [] Read_Buffer_SMS() throws IOException{
  211.  
  212.  
  213. String [] line = new String[10];
  214. int i = 0;
  215. while(true)
  216. { line [i] = br.readLine();
  217. i = i + 1;
  218. System.out.println(line[i]);
  219.  
  220. if(line != null && (line.equals("OK")) || line.equals("ERROR"))
  221. break;
  222.  
  223.  
  224. }
  225.  
  226.  
  227. return line;
  228.  
  229. }
  230.  
  231. public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
  232.  
  233.  
  234. Porta_Serie porta_Serie = new Porta_Serie();
  235. porta_Serie.test_connection();
  236. //porta_Serie.enter_pin();
  237.  
  238. porta_Serie.text_mode();
  239. porta_Serie.Send_SMS();
  240.  
  241.  
  242. /* c = porta_Serie.Send_SMS();
  243.  
  244. porta_Serie.Configure_Notification();
  245.  
  246. porta_Serie.Read_SMS(); */
  247.  
  248. }
  249.  
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement