리눅스와 보드의 시리얼 통신에 관해..
먼저..
pc 두대로.. ttyS0 끼리 통신이 되도록 구현을 했습니다.
간단하게 하나는 데이타를 주고, 하나는 데이타를 받는..
그리고나서.. 성공후에.. 다시..
PC 하나는.. 미니컴으로.. 보드를 컨트롤하구요..
PC 하나는.. 보드와 통신을 할껍니다.
원래는 pc - pc 통신였죠..(시리얼을 이용한..)
그 다음은 pc - board 통신을 할려고 합니다. 이때 pc 하나는 보드를 minicom 으로 컨트롤하죠..
그럼.. 일단 소스를 먼저..
==========================================
SimpleWrite.java
==========================================
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;
static byte[] test;
public static void main(String[] args) throws Exception{
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//if (portId.getName().equals("/dev/ttyS0")) {
System.out.println("\n" + portId.getName()+ "\n");
if (portId.getName().equals("/dev/ttyS2")) {
System.out.println("come in.. ^^");
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 {
for(int j=0;j<100;j++){
int i = (int)(Math.random()*99+1);
String reply = Integer.toString(i);
outputStream.write(reply.getBytes());
System.out.println("write = " + reply);
Thread.sleep(500);
}
outputStream.write("END".getBytes());
System.out.println("END");
outputStream.close();
} catch (IOException e) {}
serialPort.close();
}
}
}
}
}
==========================================
SimpleRead.java
==========================================
import java.io.*;
import java.util.*;
import 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) {
//boolean a = portId.getName().equals("/dev/ttyS0");
//System.out.println(a);
if (portId.getName().equals("/dev/ttyS0")) {
//if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
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];
//int numBytes;
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
byte[] result = trim(readBuffer, numBytes);
System.out.print(new String(result));
if(new String(result).equals("END")) {
serialPort.close();
break;
}
}
//byte[] result = trim(readBuffer, numBytes);
//System.out.print(new String(result));
//serialPort.close();
} catch (IOException e) {}
break;
}
}
public byte[] trim(byte[] data, int len){
byte[] data_t;
if(data.length>len){
data_t = new byte[len];
for(int i=0; i
data_t[i]=data[i];
}
return data_t;
}
else{
return data;
}
}/******************** trim END ***********************************/
}
==========================================
내용이 좀 길어 졌는데..
보드에선.. write 를.. PC 에선 read 를 합니다.
근데..
이렇게 돌려보면..
[root@TynuxBox_Ie open-wonka]$./wonka SimpleWrite
loading ficl O-O extensions
loading ficl utility classes
finished loading ficl softcore
Launched system thread, returning control to kernel
ok> ok> ok> ok> ok> ok>
Reading device configuration from file 'system/device.config'
ok> ok> Calling JNI_CreateJavaVM() ...
Init: looking up System property com.acunia.wonka.Init.linux.start
Init: start_class = class SimpleWrite
Init: will invoke public static void SimpleWrite.main(java.lang.String[]) throws java.lang.Exception of class SimpleWrite
Init: starting Garbage Collector
Init: starting Finalizer
Init: starting Heartbeat
Init: starting public static void SimpleWrite.main(java.lang.String[]) throws java.lang.Exception ...
stdin
stderr
stdout
java.lang.Runtime/exit called with status 0, running shutdown hooks.
Finished running shutdown hooks, goodnight.
보드에서 이런 메세지가 나옵니다.
그래서 system/device.config 의 내용을 보니깐..
( This file uses Forth syntax, so comments look like this. )
( For each device of type sio that you use, you need to specify the path )
( to the device on your platform. )
( attach-serial-device sio 0 /dev/ttyS0 )
( attach-serial-device sio 1 /dev/ttyS1 )
( attach-serial-device sio 2 /dev/ttyS2 )
( attach-serial-device sio 3 /dev/ttyS3 )
( Each register-serial-device command takes three parameters: the name of )
( the device, the "family" name of its driver, and a member number. )
( Register serial devices - remove the brackets round the ones you need. )
( register-serial-device sio0 sio 0 )
( register-serial-device sio1 sio 1 )
( register-serial-device sio2 sio 2 )
( register-serial-device sio3 sio 3 )
( Register standard Unix file descriptors )
register-serial-device stdin wfd 0
register-serial-device stdout wfd 1
register-serial-device stderr wfd 2
( Register standard internal devices. These are cloned on demand, )
( replacing the trailing underscore with the member number. )
( You need unzip_ to be able to read wre.jar ! )
register-serial-device unzip_ zip 0
register-serial-device zip_ zip 20
( register-serial-device loop_ loop 0 )
( The file pseudo-devices, mainly used for test purposes. There are )
( four of them, numbered 0 to 3. Set the name and then register. )
( set-file-name 0 temp/foo )
( set-file-name 1 temp/bar )
( set-file-name 2 temp/baz )
( set-file-name 3 temp/quux )
( register-serial-device file0 file 0 )
( register-serial-device file1 file 1 )
( register-serial-device file2 file 2 )
( register-serial-device file3 file 3 )
( Specify the mouse driver and path. Currently the first two parameters )
( are ignored. Only effective for framebuffer devices, not for X. )
attach-mouse-device touchscreen 0 /dev/ts
이렇더군요..
ㅠ.ㅠ 넘 길어져서 죄송합니다.. 자세히 쓸려고 하니..
암튼.. 먼가를 수정해야 할것 같은데..
몰라서요.. 이것 저것 고쳐 봤는데..
멀 고쳐야 하는지..
답변 부탁드립니다~~ ^^
댓글 달기