Skip to content

Instantly share code, notes, and snippets.

@forabi
Last active June 29, 2016 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forabi/2700baf0cde4c1d0f50adc756aac45ac to your computer and use it in GitHub Desktop.
Save forabi/2700baf0cde4c1d0f50adc756aac45ac to your computer and use it in GitHub Desktop.
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