Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.41 KB | None | 0 0
  1. import gnu.io.CommPort;
  2. import gnu.io.CommPortIdentifier;
  3. import gnu.io.NoSuchPortException;
  4. import gnu.io.SerialPort;
  5.  
  6. import java.util.HashMap;
  7. import java.awt.EventQueue;
  8. import java.awt.event.ItemEvent;
  9. import java.awt.event.ItemListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.io.PrintStream;
  14. import java.util.ArrayList;
  15. import java.util.Enumeration;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import javax.swing.JFrame;
  19. import javax.swing.JPanel;
  20. import javax.swing.border.EmptyBorder;
  21. import javax.swing.JButton;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.GridBagLayout;
  25. import java.awt.GridBagConstraints;
  26. import java.awt.Insets;
  27. import javax.swing.JTextArea;
  28. import javax.swing.JLabel;
  29. import javax.swing.JComboBox;
  30. import javax.swing.SwingUtilities;
  31. import java.awt.Window.Type;
  32. import java.awt.Frame;
  33. import java.awt.Rectangle;
  34. import java.io.IOException;
  35. import javax.swing.JTextField;
  36. import javax.swing.JScrollPane;
  37.  
  38.  
  39.  
  40.  
  41. public class EBIAlarmUI extends JFrame {
  42.  
  43. private static final long serialVersionUID = 16022014;
  44.  
  45. public TwoWaySerialComm twoWaySerCom;
  46.  
  47.  
  48. /**
  49. * Start the GUI
  50. */
  51.  
  52. public static void main(String[] args) {
  53. EventQueue.invokeLater(new Runnable() {
  54. public void run() {
  55.  
  56.  
  57. try {
  58. EBIAlarmUI frame = new EBIAlarmUI();
  59. frame.setVisible(true);
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. });
  65.  
  66. }
  67.  
  68. /**
  69. * Create the frame.
  70. */
  71. public EBIAlarmUI() {
  72. initComponents();
  73. twoWaySerCom = new TwoWaySerialComm();
  74. redirectSystemStreams();
  75.  
  76. }
  77.  
  78.  
  79. private void initComponents(){
  80.  
  81.  
  82. setBounds(new Rectangle(0, 0, 1000, 600));
  83. setExtendedState(Frame.MAXIMIZED_HORIZ);
  84. setType(Type.POPUP);
  85. setAlwaysOnTop(true);
  86. setTitle("Alarm Generator");
  87. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88. GridBagLayout gridBagLayout = new GridBagLayout();
  89. gridBagLayout.columnWidths = new int[]{0, 118, 0, 0, 0, 0, 0, 17, 23, 0, 0};
  90. gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
  91. gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
  92. gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
  93. getContentPane().setLayout(gridBagLayout);
  94.  
  95. JLabel setComPortLabel = new JLabel("Com Port");
  96. GridBagConstraints gbc_setComPortLabel = new GridBagConstraints();
  97. gbc_setComPortLabel.anchor = GridBagConstraints.EAST;
  98. gbc_setComPortLabel.insets = new Insets(0, 0, 5, 5);
  99. gbc_setComPortLabel.gridx = 0;
  100. gbc_setComPortLabel.gridy = 2;
  101. getContentPane().add(setComPortLabel, gbc_setComPortLabel);
  102.  
  103. comPortcomboBox = new JComboBox();
  104. comPortcomboBox.setEnabled(true);
  105. setComPortLabel.setLabelFor(comPortcomboBox);
  106. GridBagConstraints gbc_comPortcomboBox = new GridBagConstraints();
  107. gbc_comPortcomboBox.fill = GridBagConstraints.HORIZONTAL;
  108. gbc_comPortcomboBox.insets = new Insets(0, 0, 5, 5);
  109. gbc_comPortcomboBox.gridx = 1;
  110. gbc_comPortcomboBox.gridy = 2;
  111. getContentPane().add(comPortcomboBox, gbc_comPortcomboBox);
  112. Enumeration portList = CommPortIdentifier.getPortIdentifiers();
  113.  
  114. while (portList.hasMoreElements()) {
  115. CommPortIdentifier cpi = (CommPortIdentifier) portList.nextElement();
  116.  
  117. if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  118. comPortcomboBox.addItem(cpi.getName());
  119.  
  120. }
  121.  
  122. }
  123.  
  124. comPortcomboBox.addActionListener(new java.awt.event.ActionListener() {
  125. public void actionPerformed(java.awt.event.ActionEvent evt) {
  126. comPortcomboBoxActionPerformed(evt);
  127. }
  128.  
  129. public void comPortcomboBoxActionPerformed(java.awt.event.ActionEvent evt) {
  130. comPortcomboBox.getSelectedItem();
  131.  
  132.  
  133.  
  134. }
  135.  
  136. });
  137.  
  138. alarm1Button = new JButton();
  139. alarm1Button.setText("Alarm 1");
  140. GridBagConstraints gbc_alarm1Button = new GridBagConstraints();
  141. gbc_alarm1Button.insets = new Insets(0, 0, 5, 0);
  142. gbc_alarm1Button.gridx = 9;
  143. gbc_alarm1Button.gridy = 2;
  144. getContentPane().add(alarm1Button, gbc_alarm1Button);
  145.  
  146. alarm1Button.addActionListener(new java.awt.event.ActionListener() {
  147. public void actionPerformed(java.awt.event.ActionEvent evt) {
  148. alarm1ButtonActionPerformed(evt);
  149. }
  150. public synchronized void alarm1ButtonActionPerformed(
  151. java.awt.event.ActionEvent evt) {
  152. resultsTextArea.setText("Alarm 1 Activated, String: " + alarm1);
  153.  
  154. try {
  155.  
  156. byte[] b = alarm1.getBytes("ASCII");
  157.  
  158. TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
  159. twoWaySerCom.serialPort.getOutputStream());
  160.  
  161. sw.out.write(b);
  162. } catch (IOException e) {
  163. // Do something to handle the exception
  164. e.printStackTrace();
  165. }
  166. }
  167. });
  168.  
  169. JButton alarm2Button = new JButton("Alarm 2");
  170. alarm2Button.addActionListener(new java.awt.event.ActionListener() {
  171. public void actionPerformed(java.awt.event.ActionEvent evt) {
  172. alarm2ButtonActionPerformed(evt);
  173. }
  174. public synchronized void alarm2ButtonActionPerformed(
  175. java.awt.event.ActionEvent evt) {
  176. resultsTextArea.setText("Alarm 2 Activated, String: " + alarm2);
  177.  
  178. try {
  179.  
  180. byte[] b = alarm2.getBytes("ASCII");
  181.  
  182. TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
  183. twoWaySerCom.serialPort.getOutputStream());
  184.  
  185. sw.out.write(b);
  186. } catch (IOException e) {
  187. // Do something to handle the exception
  188. e.printStackTrace();
  189. }
  190. }
  191. });
  192.  
  193. JLabel setBaudRateLabel = new JLabel("Baud Rate");
  194. GridBagConstraints gbc_setBaudRateLabel = new GridBagConstraints();
  195. gbc_setBaudRateLabel.anchor = GridBagConstraints.EAST;
  196. gbc_setBaudRateLabel.insets = new Insets(0, 0, 5, 5);
  197. gbc_setBaudRateLabel.gridx = 0;
  198. gbc_setBaudRateLabel.gridy = 3;
  199. getContentPane().add(setBaudRateLabel, gbc_setBaudRateLabel);
  200.  
  201. baudRatecomboBox = new JComboBox(baudRates);
  202. baudRatecomboBox.setSelectedIndex(2);
  203. setBaudRateLabel.setLabelFor(baudRatecomboBox);
  204. GridBagConstraints gbc_baudRateComboBox = new GridBagConstraints();
  205. gbc_baudRateComboBox.fill = GridBagConstraints.HORIZONTAL;
  206. gbc_baudRateComboBox.insets = new Insets(0, 0, 5, 5);
  207. gbc_baudRateComboBox.gridx = 1;
  208. gbc_baudRateComboBox.gridy = 3;
  209. getContentPane().add(baudRatecomboBox, gbc_baudRateComboBox);
  210. GridBagConstraints gbc_alarm2Button = new GridBagConstraints();
  211. gbc_alarm2Button.insets = new Insets(0, 0, 5, 0);
  212. gbc_alarm2Button.gridx = 9;
  213. gbc_alarm2Button.gridy = 3;
  214. getContentPane().add(alarm2Button, gbc_alarm2Button);
  215.  
  216. JButton alarm3Button = new JButton("Alarm 3");
  217. alarm3Button.addActionListener(new java.awt.event.ActionListener() {
  218. public void actionPerformed(java.awt.event.ActionEvent evt) {
  219. alarm3ButtonActionPerformed(evt);
  220. }
  221. public synchronized void alarm3ButtonActionPerformed(
  222. java.awt.event.ActionEvent evt) {
  223. resultsTextArea.setText("Alarm 3 Activated, String: " + alarm3);
  224.  
  225. try {
  226.  
  227. byte[] b = alarm3.getBytes("ASCII");
  228.  
  229. TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
  230. twoWaySerCom.serialPort.getOutputStream());
  231.  
  232. sw.out.write(b);
  233. } catch (IOException e) {
  234. // Do something to handle the exception
  235. e.printStackTrace();
  236. }
  237. }
  238. });
  239.  
  240. JLabel lblDataBits = new JLabel("Data Bits");
  241. GridBagConstraints gbc_lblDataBits = new GridBagConstraints();
  242. gbc_lblDataBits.insets = new Insets(0, 0, 5, 5);
  243. gbc_lblDataBits.anchor = GridBagConstraints.EAST;
  244. gbc_lblDataBits.gridx = 0;
  245. gbc_lblDataBits.gridy = 4;
  246. getContentPane().add(lblDataBits, gbc_lblDataBits);
  247.  
  248. dataBitscomboBox = new JComboBox(dataBits);
  249. dataBitscomboBox.setSelectedIndex(1);
  250. GridBagConstraints gbc_dataBitscomboBox = new GridBagConstraints();
  251. gbc_dataBitscomboBox.insets = new Insets(0, 0, 5, 5);
  252. gbc_dataBitscomboBox.fill = GridBagConstraints.HORIZONTAL;
  253. gbc_dataBitscomboBox.gridx = 1;
  254. gbc_dataBitscomboBox.gridy = 4;
  255. getContentPane().add(dataBitscomboBox, gbc_dataBitscomboBox);
  256.  
  257. GridBagConstraints gbc_alarm3Button = new GridBagConstraints();
  258. gbc_alarm3Button.insets = new Insets(0, 0, 5, 0);
  259. gbc_alarm3Button.gridx = 9;
  260. gbc_alarm3Button.gridy = 4;
  261. getContentPane().add(alarm3Button, gbc_alarm3Button);
  262.  
  263. JLabel lblStopBits = new JLabel("Stop Bits");
  264. GridBagConstraints gbc_lblStopBits = new GridBagConstraints();
  265. gbc_lblStopBits.insets = new Insets(0, 0, 5, 5);
  266. gbc_lblStopBits.anchor = GridBagConstraints.EAST;
  267. gbc_lblStopBits.gridx = 0;
  268. gbc_lblStopBits.gridy = 5;
  269. getContentPane().add(lblStopBits, gbc_lblStopBits);
  270.  
  271. stopBitscomboBox = new JComboBox(stopBits);
  272. stopBitscomboBox.setSelectedIndex(1);
  273. GridBagConstraints gbc_stopBitscomboBox = new GridBagConstraints();
  274. gbc_stopBitscomboBox.insets = new Insets(0, 0, 5, 5);
  275. gbc_stopBitscomboBox.fill = GridBagConstraints.HORIZONTAL;
  276. gbc_stopBitscomboBox.gridx = 1;
  277. gbc_stopBitscomboBox.gridy = 5;
  278. getContentPane().add(stopBitscomboBox, gbc_stopBitscomboBox);
  279.  
  280. JButton customAlarmButton = new JButton("Custom Alarm");
  281. GridBagConstraints gbc_customAlarmButton = new GridBagConstraints();
  282. gbc_customAlarmButton.insets = new Insets(0, 0, 5, 0);
  283. gbc_customAlarmButton.gridx = 9;
  284. gbc_customAlarmButton.gridy = 5;
  285. getContentPane().add(customAlarmButton, gbc_customAlarmButton);
  286.  
  287. customAlarmButton.addActionListener(new java.awt.event.ActionListener() {
  288. public void actionPerformed(java.awt.event.ActionEvent evt) {
  289. customAlarmButtonActionPerformed(evt);
  290. }
  291. public void customAlarmButtonActionPerformed(java.awt.event.ActionEvent evt) {
  292. resultsTextArea.setText("Custom alarm, string sent: "+ customStringTextArea.getText());
  293.  
  294. try {
  295. byte[] b = customStringTextArea.getText().toString().getBytes("ASCII");
  296.  
  297. TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
  298. twoWaySerCom.serialPort.getOutputStream());
  299.  
  300. sw.out.write(b);
  301. } catch (IOException e) {
  302. // Do something to handle the exception
  303. e.printStackTrace();
  304. }
  305. }
  306. });
  307.  
  308. JLabel lblParity = new JLabel("Parity");
  309. GridBagConstraints gbc_lblParity = new GridBagConstraints();
  310. gbc_lblParity.insets = new Insets(0, 0, 5, 5);
  311. gbc_lblParity.anchor = GridBagConstraints.EAST;
  312. gbc_lblParity.gridx = 0;
  313. gbc_lblParity.gridy = 6;
  314. getContentPane().add(lblParity, gbc_lblParity);
  315.  
  316. paritycomboBox = new JComboBox(parity);
  317. paritycomboBox.setSelectedIndex(0);
  318. GridBagConstraints gbc_paritycomboBox = new GridBagConstraints();
  319. gbc_paritycomboBox.insets = new Insets(0, 0, 5, 5);
  320. gbc_paritycomboBox.fill = GridBagConstraints.HORIZONTAL;
  321. gbc_paritycomboBox.gridx = 1;
  322. gbc_paritycomboBox.gridy = 6;
  323. getContentPane().add(paritycomboBox, gbc_paritycomboBox);
  324.  
  325. JLabel customStringAreaLabel = new JLabel("Enter Custom String:");
  326. GridBagConstraints gbc_customStringAreaLabel = new GridBagConstraints();
  327. gbc_customStringAreaLabel.insets = new Insets(0, 0, 5, 5);
  328. gbc_customStringAreaLabel.gridx = 6;
  329. gbc_customStringAreaLabel.gridy = 6;
  330. getContentPane().add(customStringAreaLabel, gbc_customStringAreaLabel);
  331.  
  332. customStringTextArea = new JTextArea();
  333. GridBagConstraints gbc_customStringTextArea = new GridBagConstraints();
  334. gbc_customStringTextArea.gridwidth = 3;
  335. gbc_customStringTextArea.insets = new Insets(0, 0, 5, 0);
  336. gbc_customStringTextArea.fill = GridBagConstraints.BOTH;
  337. gbc_customStringTextArea.gridx = 7;
  338. gbc_customStringTextArea.gridy = 6;
  339. getContentPane().add(customStringTextArea, gbc_customStringTextArea);
  340. customStringTextArea.setText("Enter Custom String Here");
  341. customStringTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
  342.  
  343. public void mousePressed(java.awt.event.MouseEvent evt) {
  344. customStringTextAreaMousePressed(evt);
  345. }
  346.  
  347. private void customStringTextAreaMousePressed(java.awt.event.MouseEvent evt) {
  348. customStringTextArea.setText("");
  349. }
  350.  
  351. });
  352.  
  353. openComportButton = new JButton("Open");
  354. GridBagConstraints gbc_openComportButton = new GridBagConstraints();
  355. gbc_openComportButton.anchor = GridBagConstraints.NORTH;
  356. gbc_openComportButton.insets = new Insets(0, 0, 0, 5);
  357. gbc_openComportButton.gridx = 0;
  358. gbc_openComportButton.gridy = 7;
  359. getContentPane().add(openComportButton, gbc_openComportButton);
  360.  
  361. openComportButton.addActionListener(new java.awt.event.ActionListener() {
  362. public void actionPerformed(java.awt.event.ActionEvent evt) {
  363. openComportButtonActionPerformed(evt);
  364. }
  365. public synchronized void openComportButtonActionPerformed(
  366. java.awt.event.ActionEvent evt) {
  367. resultsTextArea.setText("Com Port Opened");
  368. String port = (String)comPortcomboBox.getSelectedItem();
  369. String rate = (String)baudRatecomboBox.getSelectedItem();
  370. String data = (String)dataBitscomboBox.getSelectedItem();
  371. String stop = (String)stopBitscomboBox.getSelectedItem();
  372. int parity = (int)paritycomboBox.getSelectedIndex();
  373. int rateInt = Integer.parseInt(rate);
  374. int dataInt = Integer.parseInt(data);
  375. int stopInt = Integer.parseInt(stop);
  376.  
  377. if (port != null && rate != null && data != null && stop != null) {
  378.  
  379.  
  380. try {
  381. twoWaySerCom.connect(port, rateInt, dataInt, stopInt, parity);
  382. } catch (Exception e) {
  383.  
  384. e.printStackTrace();
  385. }
  386. }
  387. }
  388. });
  389.  
  390.  
  391. closeComportButton = new JButton("Close");
  392. GridBagConstraints gbc_closeComportButton = new GridBagConstraints();
  393. gbc_closeComportButton.anchor = GridBagConstraints.NORTHWEST;
  394. gbc_closeComportButton.insets = new Insets(0, 0, 0, 5);
  395. gbc_closeComportButton.gridx = 1;
  396. gbc_closeComportButton.gridy = 7;
  397. getContentPane().add(closeComportButton, gbc_closeComportButton);
  398.  
  399. closeComportButton.addActionListener(new java.awt.event.ActionListener() {
  400. public void actionPerformed(java.awt.event.ActionEvent evt) {
  401. closeComportButtonActionPerformed(evt);
  402. }
  403. public synchronized void closeComportButtonActionPerformed(
  404. java.awt.event.ActionEvent evt) {
  405.  
  406. resultsTextArea.setText("Com Port Closed");
  407. try {
  408. twoWaySerCom.disconnect();
  409. } catch (Exception e) {
  410.  
  411. e.printStackTrace();
  412. }
  413. }
  414.  
  415.  
  416. });
  417.  
  418. JLabel resultAreaLabel = new JLabel("Result:");
  419. GridBagConstraints gbc_resultAreaLabel = new GridBagConstraints();
  420. gbc_resultAreaLabel.anchor = GridBagConstraints.NORTHEAST;
  421. gbc_resultAreaLabel.insets = new Insets(0, 0, 0, 5);
  422. gbc_resultAreaLabel.gridx = 6;
  423. gbc_resultAreaLabel.gridy = 7;
  424. getContentPane().add(resultAreaLabel, gbc_resultAreaLabel);
  425.  
  426. JScrollPane scrollPane = new JScrollPane();
  427. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  428. gbc_scrollPane.gridwidth = 8;
  429. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  430. gbc_scrollPane.gridx = 7;
  431. gbc_scrollPane.gridy = 7;
  432.  
  433. getContentPane().add(scrollPane, gbc_scrollPane);
  434.  
  435.  
  436. resultsTextArea = new JTextArea();
  437. scrollPane.setViewportView(resultsTextArea);
  438. resultsTextArea.setLineWrap(true);
  439.  
  440.  
  441.  
  442.  
  443. }
  444. public void updateTextArea(final String text) {
  445. SwingUtilities.invokeLater(new Runnable() {
  446. public void run() {
  447. resultsTextArea.append(text);
  448. }
  449. });
  450. }
  451.  
  452. public void redirectSystemStreams() {
  453. OutputStream out = new OutputStream() {
  454. @Override
  455. public void write(int b) throws IOException {
  456. updateTextArea(String.valueOf((char) b));
  457. }
  458.  
  459. @Override
  460. public void write(byte[] b, int off, int len) throws IOException {
  461. updateTextArea(new String(b, off, len));
  462. }
  463.  
  464. @Override
  465. public void write(byte[] b) throws IOException {
  466. write(b, 0, b.length);
  467. }
  468. };
  469.  
  470. System.setOut(new PrintStream(out, true));
  471. System.setErr(new PrintStream(out, true));
  472. }
  473. public javax.swing.JTextArea resultsTextArea;
  474. public javax.swing.JTextArea customStringTextArea;
  475. public javax.swing.JButton alarm1Button;
  476. public javax.swing.JButton alarm2Button;
  477. public javax.swing.JButton alarm3Button;
  478. public javax.swing.JButton customAlarmButton;
  479. public javax.swing.JButton openComportButton;
  480. public javax.swing.JButton closeComportButton;
  481.  
  482. protected HashMap map = new HashMap();
  483. public javax.swing.JComboBox comPortcomboBox;
  484. public javax.swing.JComboBox baudRatecomboBox;
  485. public javax.swing.JComboBox dataBitscomboBox;
  486. public javax.swing.JComboBox stopBitscomboBox;
  487. public javax.swing.JComboBox paritycomboBox;
  488.  
  489.  
  490.  
  491. String alarm1 = "81 01 06 01 00 10 03 01 FF";
  492. String alarm2 = "*993R03,67,6#";
  493. String alarm3 = "*994R14,67,1#";
  494. String[] parity = { "none", "odd", "even", "mark" };
  495. String[] baudRates = { "2400", "4800", "9600", "14400", "19200", "38400", "56000", "115200" };
  496. String[] stopBits = { "0", "1" };
  497. String[] dataBits = { "7", "8" };
  498.  
  499.  
  500. }
  501.  
  502. import gnu.io.CommPort;
  503. import gnu.io.CommPortIdentifier;
  504. import gnu.io.SerialPort;
  505.  
  506. import java.io.IOException;
  507. import java.io.InputStream;
  508. import java.io.OutputStream;
  509.  
  510. public class TwoWaySerialComm {
  511.  
  512. SerialPort serialPort;
  513.  
  514. public TwoWaySerialComm() {
  515. super();
  516. }
  517.  
  518. public synchronized void connect(String port, int rateInt, int dataInt, int stopInt, int parity) throws Exception {
  519.  
  520. CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
  521. if (portIdentifier.isCurrentlyOwned()) {
  522.  
  523. System.out.println("Error: Port is currently in use");
  524. } else {
  525. CommPort commPort = portIdentifier.open(this.getClass().getName(),
  526. 2000);
  527.  
  528. if (commPort instanceof SerialPort) {
  529. serialPort = (SerialPort) commPort;
  530. serialPort.setSerialPortParams(rateInt, dataInt, stopInt, parity);
  531.  
  532. InputStream in = serialPort.getInputStream();
  533. OutputStream out = serialPort.getOutputStream();
  534.  
  535. (new Thread(new SerialReader(in))).start();
  536. (new Thread(new SerialWriter(out))).start();
  537.  
  538. } else {
  539. System.out
  540. .println("Error: Only serial ports are handled by this.");
  541. }
  542. }
  543. }
  544. public synchronized void disconnect() throws Exception {
  545. if (serialPort != null) {
  546. InputStream in = serialPort.getInputStream();
  547. OutputStream out = serialPort.getOutputStream();
  548. try {
  549.  
  550. serialPort.removeEventListener();
  551.  
  552. in.close();
  553. out.close();
  554. } catch (IOException ex) {
  555.  
  556. }
  557.  
  558. serialPort.close();
  559. }
  560.  
  561. }
  562. /** */
  563. public class SerialReader implements Runnable {
  564. InputStream in;
  565.  
  566. public SerialReader(InputStream in) {
  567. this.in = in;
  568. }
  569.  
  570. public void run() {
  571. byte[] buffer = new byte[1024];
  572. int len = -1;
  573. try {
  574. while ((len = this.in.read(buffer)) > -1) {
  575. System.out.print(new String(buffer, 0, len));
  576.  
  577. }
  578. } catch (IOException e) {
  579. e.printStackTrace();
  580. }
  581. }
  582. }
  583.  
  584. /** */
  585. public static class SerialWriter implements Runnable {
  586. OutputStream out;
  587.  
  588. public SerialWriter(OutputStream out) {
  589. this.out = out;
  590. }
  591.  
  592. public void run() {
  593. try {
  594. int c = 0;
  595. while ((c = System.in.read()) > -1) {
  596. this.out.write(c);
  597. System.out.println(c);
  598. }
  599. } catch (IOException e) {
  600. e.printStackTrace();
  601. }
  602. }
  603. }
  604.  
  605. }
  606.  
  607. if (commPort instanceof SerialPort) {
  608. serialPort = (SerialPort) commPort;
  609. serialPort.setSerialPortParams(rateInt, dataInt, stopInt, parity);
  610.  
  611. DTR Out (Data Terminal Ready)
  612. DSR In (Data Set Ready)
  613. RTS Out (Request to Send)
  614. CTS In (Clear to Send)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement