Last active
June 29, 2016 02:07
-
-
Save forabi/2700baf0cde4c1d0f50adc756aac45ac 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.*; | |
public class SimpleWrite { | |
static Enumeration portList; | |
static CommPortIdentifier portId; | |
static String messageString = "Hello, world!\n"; // الجملة يلي بدنا نكتبها ع المنفذ | |
static SerialPort serialPort; | |
static OutputStream outputStream; | |
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")) { // عنوان المنفذ الأول بلينكس | |
try { | |
serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000); // هاد هو المنفذ يلي بدنا ياه، افتحو | |
} catch (PortInUseException e) { | |
} | |
try { | |
outputStream = serialPort.getOutputStream(); // جيب الستريم يلي بدنا نكتب عليها | |
} catch (IOException e) { | |
} | |
try { | |
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); | |
} catch (UnsupportedCommOperationException e) { | |
} | |
try { | |
// حول الجملة من نص لبايتات، وعطيها لستريم الكتابة | |
outputStream.write(messageString.getBytes()); | |
} catch (IOException e) { | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment