Add to Favorites    Make Home Page 28026 Online  
 Language Categories  
 Our Services  

Home » Java Home » Utility Tools / Softwares Home » Com Port Reader - RS232

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Com Port Reader - RS232
Author Marimuthu
Author Email pmarimuthu [at] gmail.com
Description RS232, COM Port reader.Reads data from COM Device connected with a PC through COM Port. This tool is very simillar to Windows HyperTerminal, but not exactly.
Category Java » Utility Tools / Softwares
Hits 15080
Code Select and Copy the Code
Code : import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.Enumeration; import java.util.Vector; import javax.comm.CommPortIdentifier; import javax.comm.SerialPort; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.StyleSheet; /* * Created on Jul 18, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author marimuthup - pmarimuthu@gmail.com * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class ComConsole extends JFrame implements ActionListener { Container g = null; JMenuBar mb = null; JMenu file, settings, help = null; JMenuItem save, filter, exit = null; JTabbedPane tab = null; JTextArea debug = null; JEditorPane myDebug = null; JLabel status = null; HTMLDocument doc = null; StyleSheet styleSheet = null; JButton clearDebug, saveMyDebug = null; JPanel debugPanel, myDebugPanel = null; JComboBox combo = null; Vector filterVector = new Vector(); String filters[] = {""}; String filterText = "MMP"; FileOutputStream fout = null; FileChannel fc = null; public ComConsole() throws Exception { super("Com Console"); g = this.getContentPane(); g.setLayout(new BorderLayout(5,5)); mb = new JMenuBar(); file = new JMenu("File"); save = new JMenuItem("Save"); exit = new JMenuItem("Exit"); file.add(save); file.addSeparator(); file.add(exit); mb.add(file); settings = new JMenu("Settings"); filter = new JMenuItem("Filter"); filter.addActionListener(this); settings.add(filter); mb.add(settings); help = new JMenu("Help"); mb.add(help); this.setJMenuBar(mb); tab = new JTabbedPane(); debugPanel = new JPanel(new BorderLayout(3,3)); debug = new JTextArea(""); debugPanel.add("Center", new JScrollPane(debug)); tab.addTab("Debug", debugPanel); clearDebug = (JButton) debugPanel.add("South", new JButton("Clear")); System.out.println("middle"); //doc = new HTMLDocument(); myDebugPanel = new JPanel(new BorderLayout(3,3)); myDebug = new JEditorPane("text/html", "<body bgcolor=#ccffcc><h3>My Debug</h3><hr><Br><B> </html>"); doc = (HTMLDocument) myDebug.getDocument(); myDebugPanel.add("Center", new JScrollPane(myDebug)); saveMyDebug = (JButton) myDebugPanel.add("South", new JButton("Save")); tab.addTab("My Output", myDebugPanel); g.add(tab, "Center"); status = new JLabel("Welcome"); g.add(status, "South"); clearDebug.addActionListener(this); saveMyDebug.addActionListener(this); System.out.println("GUI Constructed."); } public void readAndWrite() throws Exception { System.out.println("Read And Write ..."); Enumeration en = CommPortIdentifier.getPortIdentifiers(); while(en.hasMoreElements()) { CommPortIdentifier com = (CommPortIdentifier) en.nextElement(); System.out.println("com - nextElement."); if(com.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println("SERIAL_PORT Found ..."); SerialPort sPort = (SerialPort) com.open("COM1", 115200); System.out.println("Serial Port Opened."); JOptionPane.showMessageDialog(this, "COM1 Opened Successfully!"); sPort.setSerialPortParams(115200, 8, 1, SerialPort.PARITY_NONE); sPort.notifyOnDataAvailable(true); //sPort.addEventListener(this); SimpleAttributeSet attb = new SimpleAttributeSet(); StyleConstants.setForeground(attb, Color.BLUE); BufferedReader br = new BufferedReader(new InputStreamReader(sPort.getInputStream())); fout = new FileOutputStream("C:/Documents and Settings/marimuthup/Desktop/out.html"); fc = fout.getChannel(); while(true) { String str = br.readLine().trim(); if(str.equals("")) { continue; } if(str.indexOf(filterText) != -1) { //myDebug.setText("<br><font color=red>"+myDebug.getText()+str+"</font>"); String line = " "+str; doc.insertString(doc.getLength(), line, attb); // myDebug.setCaretPosition(myDebug.getText().length()); fc.write(ByteBuffer.wrap(line.getBytes())); } if(!str.startsWith("Test1") && !str.startsWith("Test2") && !str.startsWith("Test3")) { debug.append(" "+str); } // debug.setCaretPosition(debug.getText().length()); } } System.out.println("Done."); } } public void actionPerformed(ActionEvent e) { if(e.getSource() == filter) { doFilter(); } else if(e.getSource() == clearDebug) { debug.setText(""); } else if(e.getSource() == saveMyDebug) { String text = myDebug.getText(); try { } catch(Exception ex) { ex.printStackTrace(); } } } private void doFilter() { String ftext = (String) JOptionPane.showInputDialog(this, "Enter Filter-By text : ", "Input", JOptionPane.QUESTION_MESSAGE, null, filters, filters[0]); if(!filterVector.contains(ftext)) { filterVector.add(ftext); filters = (String[]) filterVector.toArray(); } filterText = (ftext == null) ? filterText : ftext; } public static void main(String[] args) { ComConsole z = null; try { z = new ComConsole(); System.out.println("ComConsole ..."); z.setLocation(100, 50); z.setSize(630, 630); z.setVisible(true); z.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); z.readAndWrite(); } catch(Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "COM1 Opened Successfully!", JOptionPane.ERROR_MESSAGE); if(z != null) { z.status.setText("Ex: "+ex.getMessage()); } try { if(z.fc != null) z.fc.close(); if(z.fout != null) z.fout.close(); System.out.println("Saved."); } catch(Exception ex2) { } } } }

Related Source Codes

Script Name Author
Sending mail Using JavaMail to Yahoo and Gmail accounts sai prasad
Simple Program in Java to Implement Multithreading Satish.K
Simple Calculator in Java Using Remote Method Invocation Satish.K
Guest Book Application Using Servlets Satish.K
String Manipulation Using Stringification Satish.K
String Manipulation Using Stringification Satish.K
Moving Ball Application Using Java Beans Satish.K
Rapid Roll game subrahmanyeswararao
student mgm arpan
Sourav Datta
Download Manager Sagar
Address Book in Java Rahul Chouhan
address book using java database connectivity(jdbc-msaccess) shekhar bansal
sun Steganography B.Rajavel
Connecting Java with MS-Access - Inserting data in Aseem

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2019 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1004


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review