[prev in list] [next in list] [prev in thread] [next in thread]
List: rxtx
Subject: [Rxtx] SerialPortEvent.DATA_AVAILABLE not listening !!
From: Raman Agarwal <raman_agarwal82 () yahoo ! com>
Date: 2007-04-01 17:25:23
Message-ID: 506615.93205.qm () web34307 ! mail ! mud ! yahoo ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Hi,
I used the following Java code to connect two computers through a PSTN modem. It \
works perfectly fine with Java.comm library but somehow doesnt with RXTXcomm.
My diagnosis indicate that the SerialPortEvent.DATA_AVAILABLE doesnot listen even if \
the remote PC has transferred some data to the listening PC. I have made sure that my \
machine isnt busy in any other task and no new constructors are getting initialized, \
etc.
Can anyone suggest what is the problem and where do i need to modify the code.
In my experiment, I executed my java code on a PC and ran Hyperterminal on the other. \
Once the modems got "connected", i typed various strings in the Hyperterminal and \
expected them to appear on the first PC (running the java code). Like mentioned \
before, this works fine with "import java.comm.*" and not with "import gnu.io.*"
JRE = 1.5.0_06
JDK = 1.5
RXTX version = 2.1
Thanks in advance,
Raman Agarwal
---------------
Here is the relevant code snippet :-
==========================================
public void run(){
try{
try{
portId1 = CommPortIdentifier.getPortIdentifier(ComPort);
} catch (Exception e) {
TimeStamp = new java.util.Date().toString();
text.append(TimeStamp + ": ComPort " + portId1 + "\n");
text.append(TimeStamp + ": msg1 - " + e + "\n");
text.append("\n+++++++++ Restart Application ++++++++\n\n"); \
ComError = true;
}
if (ComError == false)
{
try {
TimeStamp = new java.util.Date().toString();
serialPort1 = (SerialPort) portId1.open("Modem_Diagnostics", 2000);
text.append(TimeStamp + ": " + portId1.getName() + " opened for i/o" + \
"\n"); } catch (PortInUseException e) {}
try {
serialPort1.setSerialPortParams(BaudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort1.setDTR(true);
//serialPort1.setRTS(true);
serialPort1.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN & \
SerialPort.FLOWCONTROL_RTSCTS_OUT); \
//serialPort1.enableReceiveTimeout(3000);
} catch (UnsupportedCommOperationException e) {}
// initializing the serial port
try
{
serialPort1.addEventListener(this);
}
catch (TooManyListenersException e){}
serialPort1.notifyOnDataAvailable(true);
serialPort1.notifyOnCarrierDetect(true);
serialPort1.notifyOnBreakInterrupt(true);
serialPort1.notifyOnCTS(true);
serialPort1.notifyOnDSR(true);
serialPort1.notifyOnFramingError(true);
serialPort1.notifyOnOutputEmpty(true);
serialPort1.notifyOnOverrunError(true);
serialPort1.notifyOnParityError(true);
// *************** protocol to initialize modem, starts here... \
************** char received_data;
// send "at"
writeData('a'); writeData('t'); writeData('\r');
// wait for 1 sec for the modem to accept this command
// update the textbox in the meantime
myThread.sleep(1000);
text.append("\nDialing. Please Wait..\n");
// dial a number
String Number = "ATDP2569404\r";
char [] Dial = Number.toCharArray();
for(int i=0;i<Dial.length;i++){
writeData(Dial[i]);
}
// wait for a sec for the modem to accept this command
// update the textbox in the meantime
myThread.sleep(1000);
// Now, wait for 1 minute for dialing and establishing connection \
myThread.sleep(60000);
if(serialPort1.isCD() == false){
// disconnect the modem
text.append("\n\nNo Carrier !!");
text.append("\nDisconnecting now...\n\n");
writeData('+'); writeData('+'); writeData('+'); writeData('\r');
}
}
}
catch(Exception e){}
} // end of run
public void serialEvent(SerialPortEvent event){
char receivedData;
switch(event.getEventType()){
case SerialPortEvent.OE: text.append("\nOE\n");break;
case SerialPortEvent.FE: text.append("\nFE\n");break;
case SerialPortEvent.PE: text.append("\nPE\n");break;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: ;break;
case SerialPortEvent.DSR: text.append("\nData Set Ready\n");break;
case SerialPortEvent.BI: text.append("\nBreak Interrupt\n"); break;
case SerialPortEvent.CTS: text.append("\nClear to send\n"); break;
case SerialPortEvent.CD:
if( event.getNewValue() ){
text.append("\nConnected\n");
connected = true;
//write whatever you want to
//WriteData();
}
else{
text.append("\nDisconnected\n");
connected = false;
// disconnect the modem
writeData('+'); writeData('+'); writeData('+'); \
writeData('\r'); }
break;
case SerialPortEvent.DATA_AVAILABLE: HandleData(); break;
}// end of switch-case
}// end of serial event function
public void HandleData(){
char receivedData = ' ';
if(connected){
System.out.println("\nHandling data now...\n");
try{
do{
inputStream = serialPort1.getInputStream();
text.append(new String().valueOf((char)inputStream.read()));
}while(inputStream.available()>0);
}catch(Exception e){}
}
}
==========================================
Project Manager
Envirotech Instruments Pvt. Ltd.
Online Division
A-271 Okhla Phase 1
New Delhi 110020
India
Website: www.envirotechindia.com
Phone: +91(0)9872469994
---------------------------------
Don't be flakey. Get Yahoo! Mail for Mobile and
always stay connected to friends.
[Attachment #5 (text/html)]
<div>Hi,<br><br>I used the following Java code to connect two computers through a \
PSTN modem. It works perfectly fine with Java.comm library but somehow doesnt with \
RXTXcomm.<br><br>My diagnosis indicate that the SerialPortEvent.DATA_AVAILABLE \
doesnot listen even if the remote PC has transferred some data to the listening PC. I \
have made sure that my machine isnt busy in any other task and no new constructors \
are getting initialized, etc. <br><br>Can anyone suggest what is the problem and \
where do i need to modify the code. <br><br>In my experiment, I executed my java code \
on a PC and ran Hyperterminal on the other. Once the modems got "connected", i typed \
various strings in the Hyperterminal and expected them to appear on the first PC \
(running the java code). Like mentioned before, this works fine with "import \
java.comm.*" and not with "import gnu.io.*"<br><br>JRE = 1.5.0_06<br>JDK = \
1.5<br>RXTX version = 2.1<br><br></div><div>Thanks in advance, </div> \
<div> </div> <div>Raman Agarwal<br></div> <div> </div> \
<div>---------------</div> <div>Here is the relevant code snippet :-</div> \
<div> </div> <div>========================================== </div> \
<div>public void run(){<br> \
<br> \
try{<br> \
<br> \
try{<br> portId1 = \
CommPortIdentifier.getPortIdentifier(ComPort); \
<br> } catch (Exception e) \
{ \
<br> \
TimeStamp = new java.util.Date().toString();<br>
text.append(TimeStamp + ": ComPort " + portId1 + \
"\n");<br> \
text.append(TimeStamp + ": msg1 - " + e + \
"\n");<br> \
text.append("\n+++++++++ Restart Application \
++++++++\n\n"); \
<br> \
ComError = true; \
<br> }<br> if \
(ComError == false)<br> \
{<br> try \
{<br> TimeStamp = \
new java.util.Date().toString();<br>
serialPort1 = (SerialPort) portId1.open("Modem_Diagnostics", \
2000);<br> \
text.append(TimeStamp + ": " + portId1.getName() + " opened for i/o" + \
"\n");<br> } catch (PortInUseException e) \
{}<br> \
<br> try \
{<br> \
serialPort1.setSerialPortParams(BaudRate,<br> \
SerialPort.DATABITS_8,<br> \
SerialPort.STOPBITS_1,<br> \
SerialPort.PARITY_NONE);</div> \
<div> \
serialPort1.setDTR(true);<br> \
//serialPort1.setRTS(true);<br> \
serialPort1.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN & \
SerialPort.FLOWCONTROL_RTSCTS_OUT); \
<br> \
//serialPort1.enableReceiveTimeout(3000); \
<br> \
<br> } catch \
(UnsupportedCommOperationException e) {} \
<br> \
<br> // initializing the serial \
port<br> try \
<br> \
{ \
<br> \
serialPort1.addEventListener(this);<br> } \
<br> catch (TooManyListenersException e){} \
<br> \
serialPort1.notifyOnDataAvailable(true);<br> \
serialPort1.notifyOnCarrierDetect(true); \
<br> \
serialPort1.notifyOnBreakInterrupt(true);<br> \
serialPort1.notifyOnCTS(true);<br> \
serialPort1.notifyOnDSR(true);<br> \
serialPort1.notifyOnFramingError(true);<br> \
serialPort1.notifyOnOutputEmpty(true);<br> \
serialPort1.notifyOnOverrunError(true);<br> \
serialPort1.notifyOnParityError(true);<br> \
<br> // *************** protocol to \
initialize modem, starts here... \
**************<br> \
char received_data; \
<br> // send \
"at"<br> \
writeData('a'); writeData('t'); \
writeData('\r');<br> \
// wait for 1 sec for the modem to accept this \
command<br> // \
update the textbox in the \
meantime<br> \
myThread.sleep(1000); \
<br> \
text.append("\nDialing. Please \
Wait..\n");<br> // \
dial a number<br> \
String Number = "ATDP2569404\r";<br> \
char [] Dial = Number.toCharArray(); \
<br> for(int \
i=0;i<Dial.length;i++){<br> \
writeData(Dial[i]);<br>
}<br> // wait for \
a sec for the modem to accept this \
command<br> // \
update the textbox in the \
meantime<br> \
myThread.sleep(1000);<br> \
<br> // Now, wait \
for 1 minute for dialing and establishing \
connection \
<br> \
myThread.sleep(60000);<br> \
if(serialPort1.isCD() == \
false){<br> \
// disconnect the modem<br> \
text.append("\n\nNo Carrier \
!!");<br> \
text.append("\nDisconnecting \
now...\n\n");<br> \
writeData('+'); writeData('+'); writeData('+'); \
writeData('\r');<br> \
} \
<br> }<br> } \
<br> catch(Exception e){}<br> } // end of \
run<br> <br> public void \
serialEvent(SerialPortEvent event){ \
<br> char \
receivedData;<br> switch(event.getEventType()){<br> case \
SerialPortEvent.OE: text.append("\nOE\n");break;<br> case \
SerialPortEvent.FE: text.append("\nFE\n");break;<br> case \
SerialPortEvent.PE: text.append("\nPE\n");break;<br> \
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: ;break;<br> case \
SerialPortEvent.DSR: text.append("\nData Set Ready\n");break;<br> case \
SerialPortEvent.BI: text.append("\nBreak Interrupt\n"); break;<br> case \
SerialPortEvent.CTS: text.append("\nClear to send\n"); \
break;<br> case SerialPortEvent.CD: <br> if( \
event.getNewValue() ){<br> text.append("\nConnected\n");<br>&nb \
sp;   \
; \
connected = true;<br> &nbs \
p; \
//write whatever you want to \
<br> &nbs \
p; \
//WriteData();<br> \
}<br> else{<br> text.append("\nDisconnected\n" \
);<br> &n \
bsp; \
connected = false;<br> &nb \
sp; \
// disconnect the modem<br>   \
; \
writeData('+'); writeData('+'); writeData('+'); \
writeData('\r');<br> }<br> break;<br> case \
SerialPortEvent.DATA_AVAILABLE: HandleData(); break; <br> }// end of \
switch-case<br> }// end of serial event \
function<br> <br> public void \
HandleData(){<br> char receivedData = ' \
';<br> \
if(connected){<br> \
System.out.println("\nHandling data \
now...\n");<br> \
try{ <br> \
do{   \
; & \
nbsp; \
<br> \
inputStream = serialPort1.getInputStream(); \
<br> \
text.append(new String().valueOf((char)inputStream.read())); \
<br> \
}while(inputStream.available()>0); \
<br> }catch(Exception \
e){}<br> \
} \
<br> }</div> \
<div>==========================================</div><BR><BR><div> <div> <div> <div> \
<div> <div> <div><strong><span style="text-decoration: \
underline;"><br></span></strong><span style="font-weight: bold;">Project \
Manager</span><br> Envirotech Instruments Pvt. Ltd.<br> Online Division<br> A-271 \
Okhla Phase 1<br> New Delhi 110020<br> India<br> Website: www.envirotechindia.com<br> \
Phone: +91(0)9872469994<br> </div></div></div></div></div></div></div><p> 
<hr size=1>Don't be flakey. <a \
href="http://us.rd.yahoo.com/evt=43909/*http://mobile.yahoo.com/mail">Get Yahoo! Mail \
for Mobile</a> and <br><a \
href="http://us.rd.yahoo.com/evt=43909/*http://mobile.yahoo.com/mail">always stay \
connected</a> to friends.
_______________________________________________
Rxtx mailing list
Rxtx@qbang.org
http://mailman.qbang.org/mailman/listinfo/rxtx
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic