Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

serial communication in java

Status
Not open for further replies.

visva

Member level 3
Member level 3
Joined
Jul 26, 2012
Messages
55
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Visit site
Activity points
1,633
i have tried serial communication in java. But it is not working. i don't know what is going on. its reading ports and checking ports and doing everything. but the serialEvent is not working when i send data from micro_controller.
note: i checked in hyper terminal the data is receiving. but i don't know what problem in the following program. please any one help me to solve this problem.


or otherwise show me any example program and explain. i need it fast to submit my project.




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();
//Boolean i=portList.hasMoreElements();
while (portList.hasMoreElements())
{	
//System.out.println("check");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
{
if (portId.getName().equals("COM13")) 
{
System.out.println("port detection");

SimpleRead reader = new SimpleRead();
}
}
}
}

public 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);
System.out.println("welcome");
readThread.start();
}

public void run() 
{
try 
{
Thread.sleep(20000);
} 
catch (InterruptedException e) {System.out.println(e);}
}

public void serialEvent(SerialPortEvent event) 
{
System.out.println("serial 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;
default:
System.out.println("There is no data");
break;
}
}
}
 
Last edited:

yes everything is ok.the following status(in picture) will show when i run this program finaly the cursor waiting for something. In here the port is detected. in that program there is no warning. the only problem is when i send data through serial port, the serialEvent is not working. Capture.PNG

send me if there is any serial port reading program in java
and explain me the procedure for run this serial communication program.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top