Created
June 28, 2016 23:39
-
-
Save forabi/563044a89182593ca9c05073ece0fd0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import javax.comm.*; | |
/* الكلاس بتعتمد واجهة Runnable مشان نقدر نشغلها بـThread لحالها> | |
/ SerialPortEventListener على ما يبدو جاية من حزمة 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("COM1")) { // هاد اسم المنفذ بويندوز | |
// if (portId.getName().equals("/dev/term/a")) { // هاد اسم المنفذ بلينكس | |
SimpleRead reader = new SimpleRead(); // هاد المنفذ المطلوب، أنشئ قارئ | |
} | |
} | |
} | |
} | |
public SimpleRead() { | |
try { | |
// portId هو المنفذ يلي مرينا عليه وطلع تسلسلي بالتابع فوق | |
// افتح هذا المنفذ، ما بعرف شو يعني البقية | |
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); | |
} catch (PortInUseException e) { | |
System.out.println(e); | |
} | |
try { | |
// جيب الستريم تبع المنفذ، مشان نقرا منها تحت بتابع serialEvent | |
inputStream = serialPort.getInputStream(); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
try { | |
// بما أنو هي الكلاس بتعتمد واجهة مستمع لأحداث المنفذ التسلسي SerialPortEventListener | |
// ففينا نخلي المنفذ يبلّغنا متى ما صار شي جديد عليه | |
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); | |
} | |
} | |
// هاد التابع لازم تطبقو كل كلاس بتعتمد واجهة SerialPortEventListener | |
// ولما بيصير أي حدث على المنفذ، رح يستدعى هاد التابع | |
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) { | |
// اقرا 20 بايت وحطن بالمتغير readBuffer | |
int numBytes = inputStream.read(readBuffer); | |
} | |
// بس تخلص، اطبع كل شي جبنا | |
System.out.print(new String(readBuffer)); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment