Clase Ejecutable
import processing.serial.*;
Serial myPort;
int linefeed = 10;
int sensor = 0;
HServidor Servidor;
void setup() {
size(200, 200);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(linefeed);
Servidor = new HServidor(this);
Servidor.start();
}
void draw() {
println (sensor);
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil(linefeed);
if (myString != null) {
myString = trim(myString);
sensor = int(split(myString, ',')) [0];
}
}
void keyPressed () {
sensor = key-48;
}
Clase HCliente
public class HCliente extends Thread{
DataInputStream dis;
DataOutputStream dos;
Socket aceptado;
String mensaje;
int posX;
public HCliente(Socket aceptado){
this.aceptado = aceptado;
try{
dis = new DataInputStream(new BufferedInputStream(aceptado.getInputStream()));
dos = new DataOutputStream(new BufferedOutputStream(aceptado.getOutputStream()));
}
catch(SocketTimeoutException e){
}
catch(IOException io){
}
}
public void run(){
while(true){
try{
enviar();
sleep(40);
}
catch(InterruptedException ie){
}
catch(Exception e){
}
}
}
public void enviar(){
try{
dos.writeInt(sensor*2);
dos.flush();
}
catch(IOException io){
}
}
}
Clase HServidor
public class HServidor extends Thread{
ServerSocket socket;
Socket aceptado;
ArrayList misClientes;
HCliente miCliente;
PApplet app;
public HServidor(PApplet app){
this.app = app;
try{
socket = new ServerSocket(4500);
misClientes = new ArrayList(6000);
}
catch(IOException io){
}
}
public void run(){
while(true){
try{
aceptado = socket.accept();
miCliente = new HCliente(aceptado);
miCliente.start();
misClientes.add(miCliente);
sleep(40);
}
catch(InterruptedException e){
}
catch(IOException io){
}
}
}
}
No hay comentarios:
Publicar un comentario