ผมเคยทำแค่ใน .Net ซึ่งมันสามารถเรียกAdd Handle ในคลาสที่เราต้องการเรียกขึ้นมาได้
แต่พอมาใช้Javaแล้วไม่ค่อยเข้าใจ เรื่องListener
ผมต้องการสร้างโปรแกรมสำหรับรับค่ามาจากSerial Portเพื่อแสดงผลทางหน้าจอUI
โดยประกอบไปด้วย3คลาส
1.คลาสForm ใช้สำหรับแสดงผลค่าที่รับมาจากSerialPort
2.คลาสMultiConnect จะเป็นคลาสที่ใช้ดูว่ามีPortไหนที่มีการเชื่อมต่อบ้าง จากนั้นจะไปเปิดPortนั้นด้วยการสร้างClassที่ชื่อว่าPort
3. คลาส Port ก็จะมีEventที่ชื่อSerialPortEvent สำหรับอ่านค่าที่ได้มาจากPortทีละบรรทัด
ซึ่งตอนนี้ทำได้แค่ แสดงผลออกมาทางConsole
คำถามคือ ผมจะให้ TextField บนคลาสForm มันเปลี่ยนตามที่จังหวะที่มันรับค่าจากSerialPort
ซึ่งก็คือ ทุกครั้งที่Event SerialPortEventในคลาสPortทำงาน ตัวTextFieldบนคลาสFormจะต้องถูกSetText
จะใช้อะไรในการดัก ถ้าเป็น .Net มันจะสามารถ Add Handleไว้ที่หน้าForm โดยที่ในคลาสPortจะประกาศตัวแปรชนิดEventได้
ถ้าเป็นJavaต้องทำอย่างไร ขอคำแนะนำหน่อยครับ
ตัวอย่างโค้ด
1. คลาส Form สั่งให้เปิดPortตั้งแต่ครั้งแรกที่มีการโหลดForm ด้วยการสั่ง New คลาส SerialMultiplePort
public class MeasurementForm extends JFrame {
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
SerialMultiplePort sp = new SerialMultiplePort();
}
}
2. จากนั้นคลาสSerialMultiplePort มันจะไปหาว่ามีPortไหนที่ Avaliableอยู่บ้าง
ถ้าเจอมันก็จะทำการเปิดPortนั้นๆ ขึ้นมาใช้ด้วยการ สร้างคลาสPortมาใช้
public class SerialMultiplePort {
public SerialMultiplePort() throws IOException, NoSuchPortException,PortInUseException,UnsupportedCommOperationException{
Enumeration pList = CommPortIdentifier.getPortIdentifiers();
while (pList.hasMoreElements()){
CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();
String pName = cpi.getName();
System.out.println("Port " + pName + " ");
if (cpi.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println("is a Serial Port: " + cpi);
SerialPort sPort;
try{
sPort = (SerialPort) cpi.open("Port",1000);
}catch (PortInUseException e) {
// TODO: handle exception
System.err.println("Port in use: " + pName);
continue;
}
sPort.notifyOnDataAvailable(true);
try {
sPort.addEventListener(new ConnectedPort(cpi.getName(),sPort));
} catch (TooManyListenersException e) {
// TODO Auto-generated catch block
System.err.println("Too many listeners(!) " + e);
System.exit(0);
}
}
}
}
}
3. คลาสPort (ในที่นี้มันตั้งชื่อว่าConnectPort) ซึ่งภายในคลาสนี้จะประกอบไปด้วยEventสำหรับอ่านค่าที่รับมาจากSerial Port
ซึ่งในอนาคตคาดว่าจะทำเป็นAttibuteเพื่อส่งค่าไปให้คลาสForm แต่ตอนนี้ทำเป็นแบบ System.out.println(portName + ": " + line);ไปก่อน
public class ConnectedPort implements SerialPortEventListener {
String portName;
SerialPort thePort;
BufferedReader br;
public ConnectedPort(String name, SerialPort port) throws IOException {
portName = name;
thePort = port;
br = new BufferedReader(new InputStreamReader(thePort
.getInputStream()));
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent) {
String line;
try {
line = br.readLine();
if (line == null) {
System.out.println("EOF on serial port.");
System.exit(0);
}
System.out.println(portName + ": " + line);
} catch (IOException ex) {
System.err.println("IO Error " + ex);
}
}
}
JavaดักEventที่อยู่ในClassอื่นได้ด้วยวิธีใด?
แต่พอมาใช้Javaแล้วไม่ค่อยเข้าใจ เรื่องListener
ผมต้องการสร้างโปรแกรมสำหรับรับค่ามาจากSerial Portเพื่อแสดงผลทางหน้าจอUI
โดยประกอบไปด้วย3คลาส
1.คลาสForm ใช้สำหรับแสดงผลค่าที่รับมาจากSerialPort
2.คลาสMultiConnect จะเป็นคลาสที่ใช้ดูว่ามีPortไหนที่มีการเชื่อมต่อบ้าง จากนั้นจะไปเปิดPortนั้นด้วยการสร้างClassที่ชื่อว่าPort
3. คลาส Port ก็จะมีEventที่ชื่อSerialPortEvent สำหรับอ่านค่าที่ได้มาจากPortทีละบรรทัด
ซึ่งตอนนี้ทำได้แค่ แสดงผลออกมาทางConsole
คำถามคือ ผมจะให้ TextField บนคลาสForm มันเปลี่ยนตามที่จังหวะที่มันรับค่าจากSerialPort
ซึ่งก็คือ ทุกครั้งที่Event SerialPortEventในคลาสPortทำงาน ตัวTextFieldบนคลาสFormจะต้องถูกSetText
จะใช้อะไรในการดัก ถ้าเป็น .Net มันจะสามารถ Add Handleไว้ที่หน้าForm โดยที่ในคลาสPortจะประกาศตัวแปรชนิดEventได้
ถ้าเป็นJavaต้องทำอย่างไร ขอคำแนะนำหน่อยครับ
ตัวอย่างโค้ด
1. คลาส Form สั่งให้เปิดPortตั้งแต่ครั้งแรกที่มีการโหลดForm ด้วยการสั่ง New คลาส SerialMultiplePort
public class MeasurementForm extends JFrame {
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
SerialMultiplePort sp = new SerialMultiplePort();
}
}
2. จากนั้นคลาสSerialMultiplePort มันจะไปหาว่ามีPortไหนที่ Avaliableอยู่บ้าง
ถ้าเจอมันก็จะทำการเปิดPortนั้นๆ ขึ้นมาใช้ด้วยการ สร้างคลาสPortมาใช้
public class SerialMultiplePort {
public SerialMultiplePort() throws IOException, NoSuchPortException,PortInUseException,UnsupportedCommOperationException{
Enumeration pList = CommPortIdentifier.getPortIdentifiers();
while (pList.hasMoreElements()){
CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();
String pName = cpi.getName();
System.out.println("Port " + pName + " ");
if (cpi.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println("is a Serial Port: " + cpi);
SerialPort sPort;
try{
sPort = (SerialPort) cpi.open("Port",1000);
}catch (PortInUseException e) {
// TODO: handle exception
System.err.println("Port in use: " + pName);
continue;
}
sPort.notifyOnDataAvailable(true);
try {
sPort.addEventListener(new ConnectedPort(cpi.getName(),sPort));
} catch (TooManyListenersException e) {
// TODO Auto-generated catch block
System.err.println("Too many listeners(!) " + e);
System.exit(0);
}
}
}
}
}
3. คลาสPort (ในที่นี้มันตั้งชื่อว่าConnectPort) ซึ่งภายในคลาสนี้จะประกอบไปด้วยEventสำหรับอ่านค่าที่รับมาจากSerial Port
ซึ่งในอนาคตคาดว่าจะทำเป็นAttibuteเพื่อส่งค่าไปให้คลาสForm แต่ตอนนี้ทำเป็นแบบ System.out.println(portName + ": " + line);ไปก่อน
public class ConnectedPort implements SerialPortEventListener {
String portName;
SerialPort thePort;
BufferedReader br;
public ConnectedPort(String name, SerialPort port) throws IOException {
portName = name;
thePort = port;
br = new BufferedReader(new InputStreamReader(thePort
.getInputStream()));
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent) {
String line;
try {
line = br.readLine();
if (line == null) {
System.out.println("EOF on serial port.");
System.exit(0);
}
System.out.println(portName + ": " + line);
} catch (IOException ex) {
System.err.println("IO Error " + ex);
}
}
}