Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. public class Buttons {
  2.  
  3. private static SerialPort serialPort = new SerialPort("COM1") ;
  4.  
  5. public static void frame(){
  6. ButtonFrame frame= new ButtonFrame();//frame
  7. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//end of the program
  8. frame.setVisible(true);
  9.  
  10. }
  11. public static final int DEFAULT_WIDTH=700;
  12. public static final int DEFAULT_HEIGHT=250;
  13.  
  14. JButton RunButton = new JButton("Пуск"); //create buttons
  15. JButton StopButton = new JButton("Стоп");
  16. JButton SaveButton = new JButton("Сохранить");
  17. JButton CheckPorts = new JButton("Получить список доступных портов");
  18.  
  19. add(RunButton); //adding button on a panel
  20. add(StopButton);
  21. add(SaveButton);
  22. add(CheckPorts);
  23.  
  24. ButtonActionRun RunAction = new ButtonActionRun();
  25. ButtonActionStop StopAction = new ButtonActionStop();
  26. ButtonActionSave SaveAction = new ButtonActionSave();
  27. ButtonActionCheckPorts CheckAction = new ButtonActionCheckPorts();
  28.  
  29. RunButton.addActionListener(RunAction); //set action for buttons
  30. StopButton.addActionListener(StopAction);
  31. SaveButton.addActionListener(SaveAction);
  32. CheckPorts.addActionListener(CheckAction);
  33.  
  34. add(textArea);
  35. textArea.setLineWrap(true);
  36.  
  37. }
  38.  
  39. private class ButtonActionRun implements ActionListener{
  40. private SerialPort serialPort = new SerialPort("COM1");
  41. private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  42. private Date date = new Date();
  43. private String s = dateFormat.format(date);
  44. protected String portOpenFail;
  45. private String fromPort;
  46. @Override
  47. public void actionPerformed(ActionEvent event){ //Button reaction
  48. try {
  49. serialPort.openPort();
  50. serialPort.setParams(
  51. SerialPort.BAUDRATE_9600,
  52. SerialPort.DATABITS_8,
  53. SerialPort.STOPBITS_1,
  54. SerialPort.PARITY_NONE);
  55.  
  56. serialPort.setFlowControlMode(
  57. SerialPort.FLOWCONTROL_RTSCTS_IN |
  58. SerialPort.FLOWCONTROL_RTSCTS_OUT);
  59.  
  60. serialPort.addEventListener(new EventListener(), serialPort.MASK_RXCHAR);
  61. //serialPort.addEventListener(new EventListener());
  62. serialPort.writeString("R");
  63.  
  64. } catch (SerialPortException ex) {
  65. portOpenFail = "Port"+ " " + serialPort.getPortName()+ " " + "not found" + " " + ex;
  66. }
  67. try {
  68. textField.setText(fromPort = serialPort.readString());
  69. textField.setText(fromPort + s);
  70. textField.setText(fromPort);
  71. textArea.append(fromPort + s + "n");
  72. } catch (SerialPortException e) {
  73. textField.setText("No Data from " + serialPort.getPortName());
  74. textArea.setText("No Data from " + serialPort.getPortName() + "n");
  75. }
  76.  
  77.  
  78. }
  79. }
  80.  
  81. private class ButtonActionStop implements ActionListener{
  82. private String actionStop;
  83. private String actionStopFail;
  84.  
  85. @Override
  86. public void actionPerformed(ActionEvent event) {
  87. try {
  88. serialPort.closePort();
  89. actionStop = "Port Closed!" + serialPort.getPortName();
  90. textField.setText(actionStop);
  91. textArea.append(actionStop + "n");
  92. } catch (SerialPortException e) {
  93. actionStopFail = "Port not closed" + " " + serialPort.getPortName();
  94. textField.setText(actionStopFail);
  95. textArea.append(actionStopFail + "n");
  96. }
  97.  
  98. }
  99.  
  100. }
  101.  
  102. private class ButtonActionSave implements ActionListener{
  103. private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  104. private Date date = new Date();
  105. private String dateDB = dateFormat.format(date);
  106. private String actionSave;
  107. private String dataSavedToDB;
  108. private String dataNotSavedToDB;
  109. private String dataReceived;
  110.  
  111.  
  112. @Override
  113. public void actionPerformed(ActionEvent event) {
  114. try {
  115. serialPort.addEventListener(new EventListener());
  116. dataReceived = serialPort.readString();
  117.  
  118. } catch (SerialPortException e) {
  119. textField.setText("Check Serial Port! " + e);
  120. textArea.setText("Check Serial Port! " + e + "n");
  121. }
  122. try {
  123. initDB();
  124. addData(dataReceived, dateDB);
  125.  
  126. dataSavedToDB = "Data successfully saved to Data Base!";
  127.  
  128. textField.setText(dataSavedToDB + " " + dataReceived + " " + " date:" + dateDB);
  129. textArea.setText(dataSavedToDB + " " + dataReceived + " " + " date:" + dateDB + "n");
  130. } catch (SQLException e) {
  131. dataNotSavedToDB = "Fail to save data!";
  132. textField.setText(dataNotSavedToDB);
  133. textArea.setText(dataNotSavedToDB + "n");
  134. }
  135.  
  136. }
  137.  
  138. }
  139.  
  140. private class ButtonActionCheckPorts implements ActionListener{
  141.  
  142. private String actionCheck;
  143. private String portsList;
  144. private String portName = "COM1";
  145.  
  146. @Override
  147. public void actionPerformed(ActionEvent event) {
  148. String[] portNames = SerialPortList.getPortNames();
  149.  
  150. if (portNames.length == 0) {
  151. actionCheck = "There are no serial-ports :(";
  152. textField.setText(actionCheck);
  153. textArea.setText(actionCheck + "n");
  154. return;
  155. }
  156.  
  157. // Available ports
  158. actionCheck = "Available com-ports:";
  159. for (int i = 0; i < portNames.length; i++){
  160. portsList = portNames[i];
  161. }
  162.  
  163. actionCheck = "Type port name, which you want to use, and press Enter...";
  164. textField.setText(actionCheck);
  165. textArea.setText(actionCheck + "n");
  166.  
  167. Scanner in = new Scanner(System.in);
  168. portName = in.next();
  169.  
  170. textField.setText(actionCheck);
  171. textArea.append(actionCheck + "n");
  172.  
  173. textField.setText(portsList);
  174. textArea.append(portsList);
  175.  
  176. }
  177.  
  178. }
  179.  
  180. private class EventListener implements SerialPortEventListener {
  181. public String receivedData;
  182. public String noDataFromPort;
  183.  
  184. public void serialEvent(SerialPortEvent event) {
  185. if(event.isRXCHAR() && event.getEventValue() > 0){
  186. try {
  187. receivedData = serialPort.readString(event.getEventValue());
  188. textField.setText(receivedData);
  189. textArea.setText(receivedData + "n");
  190. serialPort.closePort();
  191. }
  192. catch (SerialPortException ex) {
  193. noDataFromPort = "No data received";
  194. textField.setText(noDataFromPort + " " + ex);
  195. textArea.setText(noDataFromPort + " " + ex + "n");
  196. }
  197. }
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement