| Programming Examples &
Sample Code |
| G-5. Sample Code for control PCCOM PCI RS232/422/485 Serial Card |
| Date: 2003, Feb. 13 Program idea: 1. Hardware: PCCOM PCI RS232 2/4/8 Port Card PCCOM PCI RS422 2/4/8 Port Card PCCOM PCI RS485 2/4/8/Port Card PCCOM PCI RS232 Photo Isolator 2/4/8 Port Card PCCOM PCI RS422 Photo Isolator 2/4/8 Port Card PCCOM PCI RS485 Photo Isolator 2/4/8/Port Card |
| a. COM 1 and COM 2 Mother Board -- COM1 = /dev/ttyS0 Mother Board -- COM2 = /dev/ttyS1 |
| b. Decision PCCOM PCI Card: PCCOM PCI 2 Port Card Card Number Device Name First Card /dev/ttyS4-/dev/ttyS5 Second Card /dev/ttyS12-/dev/ttyS13 Third Card /dev/ttyS20-/dev/ttyS21 Fourth Card /dev/ttyS28-/dev/ttyS29 |
| PCCOM PCI 4 Port Card Card Number Device Name First Card /dev/ttyS4-/dev/ttyS7 Second Card /dev/ttyS12-/dev/ttyS15 Third Card /dev/ttyS20-/dev/ttyS23 Fourth Card /dev/ttyS28-/dev/ttyS31 |
| PCCOM PCI 8 Port Card Card Number Device Name First Card /dev/ttyS4-/dev/ttyS11 Second Card /dev/ttyS12-/dev/ttyS19 Third Card /dev/ttyS20-/dev/ttyS27 Fourth Card /dev/ttyS28-/dev/ttyS35 |
| If COM 1 receive data "Tranzview JAVA Sample
Program" The PCI Industrial Port0 Line14 will be ON, Other wise the PCI Industrial Port0 Line14 will be OFF |
| ****** Sample Code for control
PCI Industrial Card ***** import java.io.*; import com.decision.rp.user.Adaptlet; public class MySerial extends Adaptlet implements Runnable { private SerialPort serial; // serial port object public void start() { try { // init the serial port this.serial = new SerialPort("/dev/ttyS0",9600,8,SerialPort.NO_PARITY,SerialPort.NO_FLOWCONTROL,SerialPort.STOP_BIT_1); Thread thr = new Thread(this); // create the thread // start the thread, that will invoke the run() method thr.start(); } // if it has some problem, we throw the exception and dump in server side catch (IOException ioe) { ioe.printStackTrace(); // dump the error message } } public void stop() { this.serial.close(); // close the serail port } public void valueChanged(int card, int port, int line, int state) { } public void run() { try { byte[] buf; // read line from serail port input stream while ((buf = this.readLine(this.serial)) != null) { String ln = new String(buf); // create the string object // if the input string is included 'decision' if (ln.indexOf("Tranzview JAVA Sample Program ") >= 0) { // set the [Industrial card][port 0][line 14] as on this.set(INDUSTRIAL_CARD,0,14,1); } else { this.set(INDUSTRIAL_CARD,0,14,0); } } } catch (IOException ioe) // if the I/O has problem { ioe.printStackTrace(); } } private byte[] readLine(InputStream is) throws IOException { int c = is.read(); // read the first character if (c != -1) // if the first character is not EOF (end of file { // create the temp byte array buffer ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(c); // insert the first character into buffer // if the next character is not EOF while ((c = is.read()) != -1) { // if the character is '\r', we got end of line if (c == 0x0A) break; else if (c == 0x0D) continue; // ignore the '\n' else baos.write(c); // insert this character into buffer } baos.flush(); // flush the buffer return (baos.toByteArray()); // return the data of buffer } else // EOF { return (null); } } } ****** End Of Program ***** |