夜光序言:
嗯唔:命 是用来保护人的 而不是用来伤害人的~~
正文:
opentcs,最为关键之处就是构建客户端,涉及PLC通讯协议
package china.opentcs.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
*
* author: Genius Team
*
*/
public class CapitalizeServer {
/**
*
*
* author: Genius Team
*
*
*/
public static void main(String[] args) throws Exception {
System.out.println("The capitalization server is running.");
int clientNumber = 0;
ServerSocket listener = new ServerSocket(1314);
try {
while (true) {
new Capitalizer(listener.accept(), clientNumber++).start();
}
} finally {
listener.close();
}
}
/**
* A private thread to handle capitalization requests on a particular
* socket. The client terminates the dialogue by sending a single line
* containing only a period.
*/
private static class Capitalizer extends Thread {
private Socket socket;
private int clientNumber;
private static boolean commandExcecuted;
public Capitalizer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
BufferedReader into = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner sc = new Scanner(System.in);
out.print("Point-0005<EOF>");
while (true) {
String input = into.readLine();
if (input.equals("<EOF>")) {
System.out.println("Please Provide The next Position?");
String point = sc.next();
out.print(point+"<EOF>");
out.flush();
}
System.out.println(input);
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
/**
*/
private void log(String message) {
System.out.println(message);
}
}
}
package china.opentcs.test;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* author:
*/
public class CapitalizeClient
extends Thread {
private boolean ct = false;
private String ipaddress;
private Socket sock;
// reading from keyboard (keyRead object)
private BufferedReader keyRead;
// sending to client (pwrite object)
private OutputStream ostream;
private PrintWriter pwrite;
// receiving from server ( receiveRead object)
private InputStream istream;
protected DataInputStream dataInputStream; // InputStream to receive data
private BufferedReader receiveRead;
private String currenposition;
public CapitalizeClient(String ip)
throws IOException {
this.ipaddress = ip;
this.sock = new Socket(ipaddress, 11000);
this.keyRead = new BufferedReader(new InputStreamReader(System.in));
this.ostream = this.sock.getOutputStream();
this.pwrite = new PrintWriter(ostream, true);
this.istream = this.sock.getInputStream();
this.dataInputStream = new DataInputStream(istream);
this.receiveRead = new BufferedReader(new InputStreamReader(istream));
}
@Override
public void run() {
while (!ct) {
}
}
public void disconnect()
throws IOException {
System.out.println("org.opentcs.virtualvehicle.CapitalizeClient.disconnect()");
try {
this.ct = true;
}
finally {
this.istream.close();
this.ostream.close();
this.pwrite.close();
this.sock.close();
}
}
public void sendMessage(String message) {
System.out.println("org.opentcs.virtualvehicle.CapitalizeClient.send");
this.pwrite.println(message);
}
public String getPosition()
throws IOException {
this.pwrite.println("<EOF>");
byte[] bytedata = new byte[1024];
int character;
StringBuilder data = new StringBuilder();
while ((character = istream.read()) != -1) {
if ((char) character == '<') {
if ((char) istream.read() == 'E') {
if ((char) istream.read() == 'O') {
if ((char) istream.read() == 'F') {
if ((char) istream.read() == '>') {
break;
}
}
}
}
}
data.append((char) character);
}
System.out.println("This is received : " + ":::::::::" + data.toString());
String position = data.toString();
this.pwrite.flush();
return position;
}
public Boolean readCommand()
throws IOException {
BufferedReader reader = this.receiveRead;
if (reader.readLine() != null) {
System.out.println(reader.readLine().toLowerCase());
if (reader.readLine().toLowerCase() == "yes") {
return true;
}
}
return false;
}
public static void main(String[] args)
throws Exception {
System.out.println("The capitalization Client is running.");
CapitalizeClient c = new CapitalizeClient("192.168.1.40");
c.start();
c.getPosition();
}
}