Results 1 to 9 of 9
- 04-19-2011, 10:17 AM #1
Serial port Communication Problem
private void portopenbuttonActionPerformed(java.awt.event.Actio nEvent evt) {
try
{
portconnect("COM7");
} catch (Exception e)
{ e.printStackTrace(); }
}
void portconnect(String portname)throws NoSuchPortException,PortInUseException
{
try
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portname);
if(portIdentifier.isCurrentlyOwned())
{ JOptionPane.showMessageDialog(null,"Port in Use!.."); }
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000 );
if (commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort)portIdentifier.open("ListPortclass", 300);
serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
JOptionPane.showMessageDialog(null,"Port Opened!..");
}
else
{ JOptionPane.showMessageDialog(null,"Error"); }
}
}catch(Exception e) { System.out.println(e);}
}
Hi friends, this is my code..
am using netbeans and rxtxcomm.jar for communicate with serial port..
am not able to access the serial port, the above portparams are given correctly..
Please check this code friends
if any mistake help me..
-
are you given any error message?
- 04-20-2011, 10:34 AM #3
it doesn't give any errors...
is this step correct or not.. to communicate serial port
-
Found this from a google search, apparently it works (you need to install the javax.comm package):
Java Code:import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM1")) { SimpleRead reader = new SimpleRead(); } } } } public void SimpleRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {System.out.println(e);} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {System.out.println(e);} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { System.out.println(e); } readThread = new Thread(this); readThread.start(); } public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) { System.out.println(e); } } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); } catch (IOException e) { System.out.println(e); } break; } } }
- 04-20-2011, 11:37 AM #5
thanks for your searching for me friend..
am use rxtxcomm.jar package for communication.. am searched on net some one posted, javacomm utility doesnt support for windows.. so i jumped to rxtxpackage..
can i use the rxtx package otherwise javacomm package..
which one is suitable to communicate serial port..
my requirements are :
access serial port (usb to serial converter)...
and send some threshold to that port...
-
Apparently version 2.0 of javax.comm was compatible with windows...
Communicating with ports using javax.comm package for Windows « ! .. % .. $..*.. / .. @ .. ++++ .. :) … ^
Version 3.0 has incompatibility issues.
According to the link above, you can use rxtx to replace javax.comm:
Not so good news is that Sun no more provides
support for Java communications API for Windows.Why? Here is the answer.
But not to worry..
You can still get the package from www.rxtx.org.
Changes to be then made include in import statements.
So instead of import javax.comm it should now be import gnu.io.*
- 04-20-2011, 12:04 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-20-2011, 12:29 PM #8
thanks jos..
here after i will continue my project with rxtx package..
the above code using rxtx package only...
is this code is correct or not to communicate
- 04-20-2011, 12:36 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Could be, I didn't look at it; the basic idea is the same always: pick a port you want/need, check if it's available and of the right type; if so set the baud rate, start bits, data bits, and handshake protocol you want and start using it. There are numerous examples available on the internet; it isn't rocket science ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Serial port communication via jSSC
By scream3r in forum New To JavaReplies: 5Last Post: 02-15-2011, 08:12 PM -
serial communication is very slow while reading from serial port
By elsanthosh in forum AWT / SwingReplies: 1Last Post: 07-30-2010, 08:29 AM -
serial port communication in java
By elsanthosh in forum New To JavaReplies: 2Last Post: 04-06-2010, 06:33 PM -
Usage of Serial Com Port communication
By calvinlyp in forum New To JavaReplies: 0Last Post: 02-19-2010, 06:25 AM -
Serial Port
By radhika in forum New To JavaReplies: 5Last Post: 11-06-2009, 10:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks