Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. Enumeration commports = CommPortIdentifier.getPortIdentifiers();
  2. CommPortIdentifier myCPI = null;
  3. Scanner mySC = null;
  4.  
  5. while (commports.hasMoreElements()) {
  6. myCPI = (CommPortIdentifier) commports.nextElement();
  7. if (myCPI.getName().equals("COM3"));
  8. break;
  9. }
  10.  
  11. CommPort puerto = null;
  12.  
  13. try {
  14. puerto = myCPI.open("Puerto Serial",1000);
  15. } catch (Exception e) {
  16. JOptionPane.showMessageDialog(jPanel1, "Error B1", "ERROR", JOptionPane.ERROR_MESSAGE);
  17. }
  18.  
  19. SerialPort mySP = (SerialPort) puerto;
  20. try {
  21. mySP.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
  22. } catch (Exception e) {
  23. JOptionPane.showMessageDialog(jPanel1, "Error B2", "ERROR", JOptionPane.ERROR_MESSAGE);
  24. }
  25.  
  26. try {
  27. Class.forName("com.mysql.jdbc.Driver");
  28. } catch (ClassNotFoundException ex) {
  29. JOptionPane.showMessageDialog(jPanel1, "Error B3", "ERROR", JOptionPane.ERROR_MESSAGE);
  30. }
  31. Connection conexion = null;
  32. try {
  33. conexion = DriverManager.getConnection("jdbc:mysql://localhost/basculas", "root", "cay0703081");
  34. } catch (SQLException ex) {
  35. JOptionPane.showMessageDialog(jPanel1, "Error B4", "ERROR", JOptionPane.ERROR_MESSAGE);
  36. }
  37.  
  38. int k = 0;
  39.  
  40. while (k == 0) {
  41. try {
  42. mySC = new Scanner(mySP.getInputStream());
  43. } catch (IOException ex) {
  44. JOptionPane.showMessageDialog(jPanel1, "Error B5", "ERROR", JOptionPane.ERROR_MESSAGE);
  45. }
  46.  
  47. if (mySC.nextLine() != null) {
  48. Statement st = null;
  49. try {
  50. st = conexion.createStatement();
  51. } catch (SQLException ex) {
  52. JOptionPane.showMessageDialog(jPanel1, "Error B6", "ERROR", JOptionPane.ERROR_MESSAGE);
  53. }
  54. try {
  55. st.executeUpdate("UPDATE peso_bascula SET peso='" + mySC.nextLine() + "' WHERE id='1'");
  56. } catch (SQLException ex) {
  57. JOptionPane.showMessageDialog(jPanel1, "Error B7", "ERROR", JOptionPane.ERROR_MESSAGE);
  58. }
  59. }
  60. //System.out.println (""+mySC.nextLine());
  61. // Thread.sleep(1000);
  62.  
  63. }
  64.  
  65. }
  66. // sleep(1000);
  67.  
  68. } // Fin del while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement