| Author |
How to synchronize Serial communication?
|
chen leon
Greenhorn
Joined: Aug 11, 2004
Posts: 6
|
|
My function is checking on work attendance with IC Card : 1. Read Serial, get IC cardno 2. check cardno, query cardno from database 3. if cardno is valid, send "S98" to serial, otherwise send "S97" 4. Save cardno and current time to database My program is often running corret, but sometimes do absurdness things. For example, my ic cardno is 10 chars, but sometimes only get first 8 chars. [ August 11, 2004: Message edited by: chen leon ]
|
 |
chen leon
Greenhorn
Joined: Aug 11, 2004
Posts: 6
|
|
my program is : SerailValidator.validBytes() is XOR validation the data CardManager.isCardValid() is query the cardno from database CardManager.saveCardRecord() is execute an insert SQL. package com.ditian.serial; import javax.servlet.ServletContextListener; import javax.servlet.ServletContextEvent; import java.io.*; import java.util.*; import javax.comm.*; public class SerialTerminal implements SerialPortEventListener, Runnable { private InputStream in = null; private OutputStream out = null; private SerialPort serialPort = null; public static final String ValidStr = "S98"; public static final String InvalidStr = "S97"; public static void main(String[] args) throws IOException { SerialTerminal term = new SerialTerminal(args.length > 0 ? args[0] : null); } public SerialTerminal(String port) { System.out.println("serail termina init..............."); if (port == null) { port = "COM2"; } CommPortIdentifier portId = null; System.out.println("Searching for port: " + port); Enumeration portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { CommPortIdentifier cpi = (CommPortIdentifier) portList .nextElement(); if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (cpi.getName().equals(port)) { System.out.println("Found port: " + cpi.getName()); portId = cpi; break; } } } if (portId == null) { System.out.println("port " + port + " not found."); } try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //serialPort.enableReceiveTimeout(2000); serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); serialPort.notifyOnCTS(true); serialPort.notifyOnDSR(true); serialPort.notifyOnRingIndicator(true); serialPort.notifyOnCarrierDetect(true); serialPort.notifyOnOverrunError(true); serialPort.notifyOnParityError(true); serialPort.notifyOnFramingError(true); serialPort.notifyOnBreakInterrupt(true); //serialPort.notifyOnDataAvailable(true); serialPort.notifyOnOutputEmpty(true); in = serialPort.getInputStream(); out = serialPort.getOutputStream(); OutputStream out = serialPort.getOutputStream(); } catch (PortInUseException e) { System.out.println("Port " + port + " is in use."); } catch (Exception e) { e.printStackTrace(System.out); } } public void close() { try { in.close(); out.close(); serialPort.close(); } catch (Exception e) { e.printStackTrace(); } } //implement Runnable public void run() { try { synchronized (this) { wait(10000); } } catch (InterruptedException e) { } if (this.DA) { readData(); notifyAll(); } } 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: this.DA = true; readData(); try { wait(5000); } catch (Exception e){ } notifyAll(); break; } } private boolean DA; synchronized private void readData() { if (!DA) return; byte[] buf = new byte[12]; int r = 0; try { while ((r = in.read(buf)) != -1) { System.out.println("IN: " + new String(buf, 0, r)); //validation received data boolean bb = false; bb = SerailValidator.validBytes(buf); String cardNo = ""; if (bb) { cardNo = new String(buf, 0, r - 1); // validation card bb = CardManager.isCardValid(cardNo); } //try {Thread.sleep(50);} catch (Exception e) {} if (bb) { System.out.println("valid"); for (int k = 0; k < ValidStr.length(); k++) { out.write(ValidStr.charAt(k)); } try {Thread.sleep(100);} catch (Exception e) {} } else { System.out.println("invalid"); for (int k = 0; k < InvalidStr.length(); k++) { out.write(InvalidStr.charAt(k)); } } //save card record if (bb) CardManager.saveCardRecord(cardNo); } } catch (IOException e) { System.err.println("ERR: " + e.getMessage()); } this.DA = false; notifyAll(); } } [ August 11, 2004: Message edited by: chen leon ]
|
 |
Dmitry Melnik
Ranch Hand
Joined: Dec 18, 2003
Posts: 328
|
|
My programm is often corret, but sometimes do absurdness things. Life is full of surprises and absurd things, isn't it? Sometimes even a card number may not be read correctly. How about adding #5 to your spec: 5. if attempt to read a card number failed, then do something to address the issue
|
 |
chen leon
Greenhorn
Joined: Aug 11, 2004
Posts: 6
|
|
I have run some sun javacomm samples. "BlackBox" is good. "Simple" is the same with my program, sometimes occur error. Why? Is it a synchronization problem?
|
 |
chen leon
Greenhorn
Joined: Aug 11, 2004
Posts: 6
|
|
I follow James Bacon's topic, modify my program, but the error is exits. public void run() { try { synchronized (this) { wait(10000); } } catch (InterruptedException e) { } if (this.DA) { //readData(); } } 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: while (this.DA) { try { wait(); } catch (InterruptedException e) { } } //synchronized (lock) { this.DA = true; readData(); //} break; } } private boolean DA; synchronized private void readData() { while (!this.DA) { try { wait(); } catch (InterruptedException e) { } } //synchronized (lock) { byte[] buf = new byte[12]; int r = 0; try { while ((r = in.read(buf)) != -1) { System.out.println("IN: " + new String(buf, 0, r)); //validation received data boolean bb = false; bb = SerailValidator.validBytes(buf); String cardNo = ""; if (bb) { cardNo = new String(buf, 0, r - 1); // validation card bb = CardManager.isCardValid(cardNo); } if (bb) { System.out.println("valid"); for (int k = 0; k < ValidStr.length(); k++) { out.write(ValidStr.charAt(k)); } } else { System.out.println("invalid"); for (int k = 0; k < InvalidStr.length(); k++) { out.write(InvalidStr.charAt(k)); } } //save card record if (bb) CardManager.saveCardRecord(cardNo); } } catch (IOException e) { System.err.println("ERR: " + e.getMessage()); } this.DA = false; notifyAll(); //} }
|
 |
 |
|
|
subject: How to synchronize Serial communication?
|
|
|