Advertisement
Guest User

coding dr khamim

a guest
Nov 20th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.00 KB | None | 0 0
  1. package ComInt1;
  2.  
  3. import ComInt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.io.*;
  7. /**
  8. *
  9. * @author Abd Khamim Ismail (Department of Physics, Faculty of Science, UTM)
  10. * Last updated 17 Sept 2018
  11. */
  12.  
  13. public class ComIntUI1 extends javax.swing.JFrame implements ActionListener{
  14. static Parameters param;
  15. static Com com;
  16. static PrintStream temps,plTemps;
  17. Timer timer;
  18. int onADC,mSec;
  19. double t;
  20.  
  21. /** Creates new form CoolingCurveUI */
  22. public ComIntUI1() {
  23. initComponents();
  24. initGnuplot();
  25. mSec=100; // sampling interval in milisecond
  26. timer = new Timer(mSec, this);
  27. //timer.start();
  28. onADC=0;
  29. t = 0.0;
  30. try{
  31. param = new Parameters(); //create object param
  32. param.setPort("COM3"); //set com port in Windows
  33. param.setBaudRate("115200"); //set Baud rate
  34. com = new Com(param); //assign com port
  35. System.out.println(com.receiveToString('\n', 0)); //acknowledge com port connection
  36. }catch (Exception e){
  37. }
  38. OffTemp.setEnabled(false);
  39. PlotTemp.setEnabled(false);
  40. }
  41. private void initGnuplot() {
  42. try {
  43. //plotting temperature(oC)
  44. plTemps = new PrintStream(new FileOutputStream("plottemp.plt"));
  45. plTemps.printf("set xlabel \"Time (sec)\n");
  46. plTemps.printf("set ylabel \"Temperature (oC)\n");
  47. plTemps.printf("plot \"temp.dat\" using 1:2 title \"Temperature (oC)\" with line\n");
  48. plTemps.printf("pause 0.2\n");
  49. plTemps.printf("reread");
  50. plTemps.close();
  51.  
  52. } catch (IOException e) {
  53. System.err.println ("Error writing to file");
  54. }
  55. }
  56. //Called when the Timer fires.
  57. @SuppressWarnings("empty-statement")
  58. public void actionPerformed(ActionEvent ee) {
  59.  
  60. try{
  61. if (onADC==1) {
  62. int datPoint = 4;// number of data point
  63. double ADCSum = 0.0;
  64. for (int i=1; i<=datPoint; i++)
  65. {
  66. com.sendString("rd1\r",1);// read ADC A1
  67. String ADC = com.receiveToString('\n', 0);//receive ADC value in string
  68. double ADCR = Double.parseDouble(ADC);//convert ADC value from String to double
  69. ADCSum = ADCSum + ADCR;//summation of ADC data points
  70. }
  71.  
  72. t = t + mSec/1000.0;//calculate elapsed time
  73.  
  74. double ADCAve = ADCSum/datPoint; //averaging
  75. double volVal = (ADCAve/1023.0)*5.0;//convert ADC value into analogue voltage
  76. double tempVal = volVal*100.0; //convert volVal to deg Celsius, from LM35 datasheet(10mV/degC or 100degC/V)
  77. String tempVolValF = String.format("%.4f", volVal); // formatting voltage value to decimal places
  78. String tempValF = String.format("%.2f", tempVal); //formatting temperature value to decimal places
  79. TempVolValue.setText(tempVolValF);//display analogue voltage value
  80. TempValue.setText(tempValF);//display temperature value
  81.  
  82. //------------------------------ Optional bar graph------------------------------------
  83. double barTemp = tempVal*(100.0/100.0);//scaling to 0-100 deg Celcius
  84. long tempGraph = Math.round(barTemp); //rounding voltage value into integer (0-100)
  85. TempScale.setValue((int) tempGraph); //Display temperature value to progress bar (0-100)
  86. //------------------------------------------------------------------------------------
  87.  
  88. temps.printf("%.2f %.2f\n",t,tempVal);//record data into file
  89. System.out.printf("%.2f %.2f\n",t,tempVal);
  90.  
  91. Thread.sleep(5);//delay
  92. }
  93.  
  94. }catch (Exception e){System.out.println("Device not found");}
  95. }
  96.  
  97. /** This method is called from within the constructor to
  98. * initialise the form.
  99. * WARNING: Do NOT modify this code. The content of this method is
  100. * always regenerated by the Form Editor.
  101. */
  102. @SuppressWarnings("unchecked")
  103. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  104. private void initComponents() {
  105.  
  106. jScrollPane1 = new javax.swing.JScrollPane();
  107. jTable1 = new javax.swing.JTable();
  108. buttonGroup1 = new javax.swing.ButtonGroup();
  109. buttonGroup2 = new javax.swing.ButtonGroup();
  110. buttonGroup3 = new javax.swing.ButtonGroup();
  111. buttonGroup4 = new javax.swing.ButtonGroup();
  112. buttonGroup5 = new javax.swing.ButtonGroup();
  113. buttonGroup6 = new javax.swing.ButtonGroup();
  114. buttonGroup7 = new javax.swing.ButtonGroup();
  115. buttonGroup8 = new javax.swing.ButtonGroup();
  116. canvas1 = new java.awt.Canvas();
  117. jPanel3 = new javax.swing.JPanel();
  118. jLabel9 = new javax.swing.JLabel();
  119. OffTemp = new javax.swing.JButton();
  120. OnTemp = new javax.swing.JButton();
  121. TempValue = new javax.swing.JTextField();
  122. jLabel19 = new javax.swing.JLabel();
  123. TempVolValue = new javax.swing.JTextField();
  124. jLabel20 = new javax.swing.JLabel();
  125. PlotTemp = new javax.swing.JButton();
  126. TempScale = new javax.swing.JSlider();
  127. ExitButton = new javax.swing.JButton();
  128. jLabel16 = new javax.swing.JLabel();
  129.  
  130. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  131. new Object [][] {
  132. {null, null, null, null},
  133. {null, null, null, null},
  134. {null, null, null, null},
  135. {null, null, null, null}
  136. },
  137. new String [] {
  138. "Title 1", "Title 2", "Title 3", "Title 4"
  139. }
  140. ));
  141. jScrollPane1.setViewportView(jTable1);
  142.  
  143. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  144. setForeground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
  145.  
  146. jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
  147.  
  148. jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  149. jLabel9.setText("<html><sup>o</sup> C");
  150.  
  151. OffTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  152. OffTemp.setText("OFF/HOLD");
  153. OffTemp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
  154. OffTemp.addActionListener(new java.awt.event.ActionListener() {
  155. public void actionPerformed(java.awt.event.ActionEvent evt) {
  156. OffTempActionPerformed(evt);
  157. }
  158. });
  159.  
  160. OnTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  161. OnTemp.setText("ON");
  162. OnTemp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
  163. OnTemp.addActionListener(new java.awt.event.ActionListener() {
  164. public void actionPerformed(java.awt.event.ActionEvent evt) {
  165. OnTempActionPerformed(evt);
  166. }
  167. });
  168.  
  169. TempValue.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  170. TempValue.addActionListener(new java.awt.event.ActionListener() {
  171. public void actionPerformed(java.awt.event.ActionEvent evt) {
  172. TempValueActionPerformed(evt);
  173. }
  174. });
  175.  
  176. jLabel19.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
  177. jLabel19.setText("<html><sup>o</sup>C");
  178.  
  179. TempVolValue.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  180. TempVolValue.addActionListener(new java.awt.event.ActionListener() {
  181. public void actionPerformed(java.awt.event.ActionEvent evt) {
  182. TempVolValueActionPerformed(evt);
  183. }
  184. });
  185.  
  186. jLabel20.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
  187. jLabel20.setText("V");
  188.  
  189. PlotTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  190. PlotTemp.setText("PLOT/SAVE");
  191. PlotTemp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
  192. PlotTemp.addActionListener(new java.awt.event.ActionListener() {
  193. public void actionPerformed(java.awt.event.ActionEvent evt) {
  194. PlotTempActionPerformed(evt);
  195. }
  196. });
  197.  
  198. TempScale.setForeground(new java.awt.Color(0, 0, 255));
  199. TempScale.setMajorTickSpacing(10);
  200. TempScale.setMinorTickSpacing(5);
  201. TempScale.setPaintLabels(true);
  202. TempScale.setPaintTicks(true);
  203. TempScale.setValue(0);
  204.  
  205. javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
  206. jPanel3.setLayout(jPanel3Layout);
  207. jPanel3Layout.setHorizontalGroup(
  208. jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  209. .addGroup(jPanel3Layout.createSequentialGroup()
  210. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  211. .addGroup(jPanel3Layout.createSequentialGroup()
  212. .addContainerGap()
  213. .addComponent(TempScale, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE)
  214. .addGap(13, 13, 13)
  215. .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  216. .addGroup(jPanel3Layout.createSequentialGroup()
  217. .addGap(19, 19, 19)
  218. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  219. .addGroup(jPanel3Layout.createSequentialGroup()
  220. .addComponent(TempVolValue, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
  221. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  222. .addComponent(jLabel20))
  223. .addGroup(jPanel3Layout.createSequentialGroup()
  224. .addComponent(TempValue, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
  225. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  226. .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))))
  227. .addGap(25, 39, Short.MAX_VALUE)
  228. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  229. .addComponent(PlotTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  230. .addComponent(OffTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  231. .addComponent(OnTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
  232. .addContainerGap())
  233. );
  234. jPanel3Layout.setVerticalGroup(
  235. jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  236. .addGroup(jPanel3Layout.createSequentialGroup()
  237. .addContainerGap()
  238. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  239. .addComponent(OnTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
  240. .addComponent(TempVolValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  241. .addComponent(jLabel20))
  242. .addGap(8, 8, 8)
  243. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  244. .addComponent(OffTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
  245. .addComponent(TempValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  246. .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  247. .addGap(18, 18, 18)
  248. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  249. .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  250. .addComponent(PlotTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  251. .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  252. .addComponent(TempScale, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
  253. .addContainerGap(34, Short.MAX_VALUE))
  254. );
  255.  
  256. ExitButton.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
  257. ExitButton.setForeground(new java.awt.Color(255, 0, 51));
  258. ExitButton.setText("EXIT");
  259. ExitButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
  260. ExitButton.addActionListener(new java.awt.event.ActionListener() {
  261. public void actionPerformed(java.awt.event.ActionEvent evt) {
  262. ExitButtonActionPerformed(evt);
  263. }
  264. });
  265.  
  266. jLabel16.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
  267. jLabel16.setForeground(new java.awt.Color(0, 0, 204));
  268. jLabel16.setText("<html>SENSOR-TEMPERATURE (<sup>o</sup>C)");
  269.  
  270. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  271. getContentPane().setLayout(layout);
  272. layout.setHorizontalGroup(
  273. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  274. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  275. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  276. .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  277. .addGap(134, 134, 134))
  278. .addGroup(layout.createSequentialGroup()
  279. .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  280. .addGap(0, 0, Short.MAX_VALUE))
  281. .addGroup(layout.createSequentialGroup()
  282. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  283. .addGroup(layout.createSequentialGroup()
  284. .addGap(593, 593, 593)
  285. .addComponent(ExitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
  286. .addGroup(layout.createSequentialGroup()
  287. .addGap(218, 218, 218)
  288. .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)))
  289. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  290. );
  291. layout.setVerticalGroup(
  292. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  293. .addGroup(layout.createSequentialGroup()
  294. .addGap(9, 9, 9)
  295. .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  296. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  297. .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  298. .addGap(35, 35, 35)
  299. .addComponent(ExitButton)
  300. .addGap(0, 0, 0)
  301. .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  302. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  303. );
  304.  
  305. pack();
  306. }// </editor-fold>
  307.  
  308. private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
  309. // TODO add your handling code here:
  310. System.exit(0);
  311. }
  312.  
  313. private void OffTempActionPerformed(java.awt.event.ActionEvent evt) {
  314. // TODO add your handling code here:
  315. onADC=0;
  316. OffTemp.setEnabled(false);
  317. OnTemp.setEnabled(true);
  318. temps.close();
  319. }
  320.  
  321. private void OnTempActionPerformed(java.awt.event.ActionEvent evt) {
  322. // TODO add your handling code here:
  323. timer.start();
  324. onADC=1;
  325. try{
  326. temps = new PrintStream(new FileOutputStream("temp.dat"));//open the file
  327. } catch(Exception e){System.out.println("ERROR FILE WRITTING");}
  328. OnTemp.setEnabled(false);
  329. OffTemp.setEnabled(true);
  330. PlotTemp.setEnabled(true);
  331. }
  332.  
  333. private void TempValueActionPerformed(java.awt.event.ActionEvent evt) {
  334. // TODO add your handling code here:
  335. }
  336.  
  337. private void TempVolValueActionPerformed(java.awt.event.ActionEvent evt) {
  338. // TODO add your handling code here:
  339. }
  340.  
  341. private void PlotTempActionPerformed(java.awt.event.ActionEvent evt) {
  342. // TODO add your handling code here:
  343. Runtime cgp = Runtime.getRuntime();
  344.  
  345. try {
  346. Process prcs = cgp.exec("wgnuplot plottemp.plt");
  347. } catch (IOException e) {
  348. System.err.println ("Error calling gnuplot");
  349. }
  350. }
  351.  
  352. /**
  353. * @param args the command line arguments
  354. */
  355. public static void main(String args[]) {
  356. java.awt.EventQueue.invokeLater(new Runnable() {
  357. public void run() {
  358. new ComIntUI1().setVisible(true);
  359. }
  360. });
  361. }
  362.  
  363. // Variables declaration - do not modify
  364. private javax.swing.JButton ExitButton;
  365. private javax.swing.JButton OffTemp;
  366. private javax.swing.JButton OnTemp;
  367. private javax.swing.JButton PlotTemp;
  368. private javax.swing.JSlider TempScale;
  369. private javax.swing.JTextField TempValue;
  370. private javax.swing.JTextField TempVolValue;
  371. private javax.swing.ButtonGroup buttonGroup1;
  372. private javax.swing.ButtonGroup buttonGroup2;
  373. private javax.swing.ButtonGroup buttonGroup3;
  374. private javax.swing.ButtonGroup buttonGroup4;
  375. private javax.swing.ButtonGroup buttonGroup5;
  376. private javax.swing.ButtonGroup buttonGroup6;
  377. private javax.swing.ButtonGroup buttonGroup7;
  378. private javax.swing.ButtonGroup buttonGroup8;
  379. private java.awt.Canvas canvas1;
  380. private javax.swing.JLabel jLabel16;
  381. private javax.swing.JLabel jLabel19;
  382. private javax.swing.JLabel jLabel20;
  383. private javax.swing.JLabel jLabel9;
  384. private javax.swing.JPanel jPanel3;
  385. private javax.swing.JScrollPane jScrollPane1;
  386. private javax.swing.JTable jTable1;
  387. // End of variables declaration
  388.  
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement