02. december 2005 - 10:42
Der er
1 kommentar og
1 løsning
Skrive og modtage på samme tid
Hej Experter
Jeg skal lave en lille java-applikation, som skal skrive de ting ud som der kommer ind på porten. Samtidigt med dette skal det være muligt på et tilfældigt tidspunkt at kalde en funktion der sender noget ud på porten. Jeg går udfra at der skal køre to processor..en til receive og en til send. Jeg har dog problemet med at disse processer skal deles om den samme comport.
Jeg håber nogle har noget sourcecode til at ligge som gør ovenstående
03. december 2005 - 13:44
#2
Beklager formuleringen...Jeg har lavet ovenstående kodeeksempel, som passer til min krav.
package Network;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class CommunicationThread implements SerialPortEventListener, Runnable{
static BufferedReader reader;
static String tempString;
private CommPortIdentifier portId;
private Enumeration portList;
OutputStream outputStream;
InputStream inputStream;
SerialPort serialPort;
static String gpgga;
static String gprmc;
static String gpgsa;
static String gpgll;
static String gpgsv;
static String gpvtg;
public void initCommunication() {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("writereadobjectApp", 2000);
System.out.println("Opened the port for writing: " + portId.getName());
} catch (PortInUseException e) {
System.out.println("hej2");
}
try {
serialPort.setSerialPortParams(38400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
System.out.println("UnsupportedCommOperationException, Could not set the port: " + e);
}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
}
catch (IOException e) {
System.out.println("hej4");
}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {
System.out.println(e);
}
serialPort.notifyOnDataAvailable(true);
}
}
}
String command1 = "open\r";
String command2 = "close\r";
boolean EndCharacterQMark = false;
Write(command1);
try {
EndCharacterQMark = waitUntilDataComes();
System.out.println("EndCharacterQMark is: " + EndCharacterQMark);
}
catch (IOException e) {
System.out.println(e);
}
if(EndCharacterQMark) {
EndCharacterQMark = false;
Write(command2);
try {
EndCharacterQMark = waitUntilDataComes();
System.out.println("EndCharacterQMark is: " + EndCharacterQMark);
}
catch (IOException e) {
System.out.println(e);
}
}
closePort();
}
synchronized boolean waitUntilDataComes() throws IOException {
String total="";
boolean EndCharQMark = false;
StringBuffer inputBuffer = new StringBuffer();
int newData = 0;
while(true) {
if(!dataAvailable()) {
try {
wait();
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
try {
while (newData != -1) {
newData = inputStream.read();
inputBuffer.append((char)newData);
}
}
catch (IOException ex) {
System.err.println(ex);
}
String dataAtPort = inputBuffer.toString();
for(int i = 0; i < dataAtPort.length(); i++) {
if(dataAtPort.indexOf("?")>1 || dataAtPort.indexOf(">")>1 ) {
EndCharQMark = true;
return EndCharQMark;
}
}
}
}
boolean dataAvailable() throws IOException {
return inputStream.available() > 0;
}
public void Write(String commandToSend) {
String command = commandToSend;
try {
outputStream.write(command.getBytes());
System.out.println("\nSuccessfully Written: " + command);
}
catch (IOException e) {
System.out.println("IO Exception Occured: " + e);
}
}
void closePort() {
serialPort.close();
System.out.println("Port closed successfully.");
}
public CommunicationThread() {
Thread receive = new Thread(this);
receive.start();
}
synchronized public void serialEvent(SerialPortEvent event) {
System.out.println("Event registret");
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:
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while (reader.ready()){
String currLine = reader.readLine();
try{
//Change this code to use the Split() function
StringTokenizer st = new StringTokenizer(currLine, ",");
tempString = st.nextToken();
System.out.println(currLine);
//check value of first token in currLine assign to variable
if (tempString.equals("$GPGGA"))
gpgga = new String(currLine.toString());
else if(tempString.equals("$GPRMC"))
gprmc = new String(currLine.toString());
else if(tempString.equals("$GPGSA"))
gpgsa = new String(currLine.toString());
else if(tempString.equals("$GPGLL"))
gpgll = new String(currLine.toString());
else if(tempString.equals("$GPGSV"))
gpgsv = new String(currLine.toString());
else if(tempString.equals("$GPVTG"))
gpvtg = new String(currLine.toString());
else
System.out.println("invalid line: " +currLine.toString());
}catch (Exception e) {}//System.out.println("error 6\n" + e);}
}
} catch (IOException e) {
System.out.println("error 7");}
break;
}
}
public String getGPGGA(){
return gpgga;
}
public String getGPRMC(){
return gprmc;
}
public String getGPGSA(){
return gpgsa;
}
public String getGPGLL(){
return gpgll;
}
public String getGPGSV(){
return gpgsv;
}
public String getGPVTG(){
return gpvtg;
}
public void run() {
initCommunication();
}
}