Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to read the data from serial port in rfid

 
Achyut Behera
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:PatienceIsAVirtue

That one is for people who bump their threads within one day. You mean EaseUp
 
Achyut Behera
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest going to the manufacture. Every card is different.

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic