The Artima Developer Community
Sponsored Link

Java Answers Forum
About javx.comm.... reading the serial port

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
chanda

Posts: 11
Nickname: rcss
Registered: Sep, 2004

About javx.comm.... reading the serial port Posted: Jan 28, 2005 12:38 AM
Reply to this message Reply
Advertisement
My phone is connected to modem through a serial port…

I want to read the modem after the phone ring has indicated,

I am using javax.comm. package…

If phone rings then it shows that phone is ringing…

But my problem is, It doesn’t reads the data after I pick the phone…

Please suggest me what should I do?….

wether i have to go for event listning or use a Thread which continously monitor the serial port ...

Thanks in advance…




My code…>



import javax.comm.*;
import java.io.IOException;
import java.util.TooManyListenersException;
import java.io.*;

public class PhoneListener1 implements SerialPortEventListener
{
SerialPort modem;
private InputStream inputStream;

public PhoneListener1()
{
}

public void setup()
{
String portName = "COM3";

try
{
CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(portName);

if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
modem = (SerialPort)cpi.open("Phone Listener", 1000);
inputStream = modem.getInputStream();

modem.addEventListener(this);

modem.notifyOnRingIndicator(true);
modem.notifyOnDataAvailable(true);
}
}
catch (NoSuchPortException e)
{
System.err.println("Usage: java PhoneListener port_name");
}
catch (PortInUseException e)
{
System.err.println(e);
}
catch (IOException ioe)
{
System.out.println("in catch3");
}
catch (TooManyListenersException e){ }
}

public void close()
{
try
{
modem.removeEventListener();
modem.close();
}
catch(Exception e)
{
System.out.println(e);
}
}

public static void main(String[] args) throws InterruptedException, IOException
{
System.out.println("in main");

PhoneListener1 pl = new PhoneListener1();

while(true)
{
pl.setup();

System.out.println("Press ENTER to stop");


System.in.read(new byte[10]);

pl.close();

}
}


public void serialEvent(SerialPortEvent evt)
{

if (evt.getEventType() == SerialPortEvent.RI)
{
System.out.println("Ringing...");
}

if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
System.out.println("Data available...");
byte[] readBuffer = new byte[8];

try
{
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
System.out.println("The read value in integer::" + numBytes);
}
}

catch (IOException ioe)
{
System.out.println("ReaderClient: Caught IOException: " + ioe.getMessage());
}
}
}
}

Topic: Code for privious---serial Port event Previous Topic   Next Topic Topic: about SerialPortEvent Listener..

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use