Jump to content

Recommended Posts

Posted

Olá a todos,

Eu tenho um leitor de cartões magnéticos com porta série. Eu quero o seguinte: quero fazer um programa em Java que, ao passar um cartão pelo leitor, leia a informação contida no cartão. Por exemplo: ao passar o cartão, escrever o nome da pessoa.

Eu já tenho um programa que lista as portas série disponíveis, mas, neste momento. quando se passa um cartão pelo leitor, não faz nada.

Se alguém me puder/conseguir ajudar, agradecia,

Obrigado.

P.S: Este projeto é para a minha escola, portanto, o cartão a ser passado pelo leitor será o da minha escola, obviamente.

Posted
Em 07/04/2017 às 19:42, Bruno Tafarelo disse:

Olá

Não tenho um equipamento para testes, mas nesse link há um código que parece correto.

http://www.java-samples.com/showtutorial.php?tutorialid=11

Eu tenho um programa parecido com esse no Eclipse. O código é este:

import java.io.*; 
import java.util.*;
import gnu.io.*;

public class SerialReading implements Runnable, SerialPortEventListener {
	static CommPortIdentifier portID;
	static Enumeration portList;
	InputStream inputStream;
	SerialPort serialPort;
	Thread readThread;
	byte[] readBuffer;
	
	public static void main(String[] args) {
		portList = CommPortIdentifier.getPortIdentifiers();
		System.out.println("portList..."+portList);
		while (portList.hasMoreElements()) {
			portID = (CommPortIdentifier)portList.nextElement();
			if (portID.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				System.out.println("Port identified is serial..."+portID.getPortType());
			if (portID.getName().equals("COM4")) {
				System.out.println("Port identified is COM4..."+portID.getName());
				SerialReading reader = new SerialReading();
			} else {
				System.out.println("Unable to open port");
			}
		}
	}
}
	public SerialReading() {
		try {
			System.out.println("In SerialReading() constructor");
			serialPort = (SerialPort)portID.open("SerialReadingApp",500);
			System.out.println("Serial Port..."+serialPort);
		} catch (PortInUseException e) {
			System.out.println("Port in use Exception");
		}
		try {
			inputStream = serialPort.getInputStream();
			System.out.println("Input Stream..."+inputStream);
		} catch (IOException e) {
			System.out.println("IO Exception");
		}
		try {
			serialPort.addEventListener(this);
		} catch (TooManyListenersException e) {
			System.out.println("Too many Listener Exception");
		}
		serialPort.notifyOnDataAvailable(true);
		try {
			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
			
			serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
			
			serialPort.enableReceiveTimeout(500);
		} catch (UnsupportedCommOperationException e) {
			System.out.println("Unsupported Comm operation");
		}
		readThread = new Thread(this);
		readThread.start();
	}
	
	public void run() {
		try {
		System.out.println("In run() function");
		Thread.sleep(20);
	} catch (InterruptedException e) {
		System.out.println("Interrupted Exception in run() method");
	}
}
	public void serialEvent(SerialPortEvent event) {
		System.out.println("In serialEvent() function"+event+event.getEventType());
		switch (event.getEventType()) {
		case SerialPortEvent.DATA_AVAILABLE:
			readBuffer = new byte[8];
			
			try {
				while (inputStream.available()>0) {
					int numBytes = inputStream.read(readBuffer);
					System.out.println("Number of bytes read"+numBytes);
				}
				System.out.println(new String(readBuffer));
			} catch (IOException e) {
				System.out.println("IO Exception in serialEvent()");
			}
			break;
		}
	}
}

 E o output que recebo, quando ligo o leitor de cartões ao PC, é este:

portList...gnu.io.CommPortEnumerator@60e53b93
Port identified is serial...1
Port identified is COM4...COM4
In SerialReading() constructor
Serial Port...//./COM4
Input Stream...gnu.io.RXTXPort$SerialInputStream@d716361
In run() function

Mas quando eu passo o cartão pelo leitor, não aparece nada.

P.S: Eu substitui a biblioteca javax.comm pela biblioteca RXTX (gnu.io) porque não tenho a javax.comm

O que faço para que o programa receba dados quando se passa o cartão?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.