aspose file tools*
The moose likes Java in General and the fly likes unable to read the data from serial port in rfid  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "unable to read the data from serial port in rfid  " Watch "unable to read the data from serial port in rfid  " New topic
Author

unable to read the data from serial port in rfid

Achyut Behera
Greenhorn

Joined: Mar 09, 2009
Posts: 17
Hi All,

I am able to connect to RFID through COM4 in java (Windows platfom).
Not able to read and write the data.
The event function is not calling.

please go through the code in below.

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("COM4")) {
/* if (portId.getName().equals("/dev/term/a")) {*/
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleRead", 2000);

} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();

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;
}
}
}


Can you please help me how to call the event handler function to read the data.
Please reply ASAP.

Thanks,
Achyut
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

UseCodeTags & PatienceIsAVirtue

I'm not familiar with COM4 but I find it weird that your Runnable implementation only lets the thread sleep 20 seconds and the eventlistener
only does something when the input is of a certain type. I would make it at least output the event to the console so that you can track the
events.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Wouter Oet wrote:PatienceIsAVirtue

That one is for people who bump their threads within one day. You mean EaseUp


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Achyut Behera
Greenhorn

Joined: Mar 09, 2009
Posts: 17
i don't think so 'com4 ' is the problem.
Because i am able to connect it to that port.

Do you have any idea how to read the data from serial port.
if you have any related code can you please share with me.
it wil really helpful for me.

Thanks,
Achyut
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
My experience with the code above is it is expecting data to be either sent to the port or data received by the port. So if you are connecting what data are you sending or receiving? Are you expecting a constant data stream from the rfid device or to you need to initialize the device and get it to send data to you.

Mike
Achyut Behera
Greenhorn

Joined: Mar 09, 2009
Posts: 17
Some data is already there in the device.
I just want to read the data after that do some manipulation and update to the device.
Just need to know hon can i read??

Thanks.
Achyut
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
So do you have the manual for the device and determine the protocol to access it. Normally, there an initialize command or a download command along with some form of update.

We do not know what device you are using so at this point, it is back to the manual which should tell you all this information.

Mike
Achyut Behera
Greenhorn

Joined: Mar 09, 2009
Posts: 17
I am using Mifare card reader.
Do have any idea what are the set up needed for windows to read Mifare Card.

Thanks,
Achyut
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
I would suggest going to the manufacture. Every card is different.

Mike
 
Consider Paul's rocket mass heater.
 
subject: unable to read the data from serial port in rfid