PDA Serial Port and PC
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tamer_Aidek
Posted On:   Wednesday, March 21, 2001 04:20 AM

Hi! I need urgent help. I want to connect a PDA (PalmV) with a PC by serial port. My application doesn't need Conduit API because I only want send data without I use HotSync. I have KVM in PDA and comm package in PC. Client (PDA) code is: import java.io.*; import javax.microedition.io.*; import com.sun.kjava.*; public class cliente extends Spotlet { private static Graphics sGraphics; private TextField mInputField; private PrintStream mOut; private static StreamConnection s; public static void main(String[] args) throws IOException { // Clear off the KVM splash screen. sGraphics = Graphics.getGraphics(); sGraphics.clearScreen(); //Conecto    More>>

Hi! I need urgent help. I want to connect a PDA (PalmV) with a PC by serial port. My application doesn't need Conduit API because I only want send data without I use HotSync. I have KVM in PDA and comm package in PC. Client (PDA) code is:

import java.io.*;

import javax.microedition.io.*;

import com.sun.kjava.*;

public class cliente extends Spotlet {
private static Graphics sGraphics;
private TextField mInputField;
private PrintStream mOut;
private static StreamConnection s;

public static void main(String[] args) throws IOException {
// Clear off the KVM splash screen.
sGraphics = Graphics.getGraphics();
sGraphics.clearScreen();
//Conecto
try {
s = (StreamConnection)Connector.open
("comm:0;baudrate=9600",Connector.READ_WRITE);
cliente c = new cliente();
}
catch (IOException ioe) {
System.out.println("Error al crear el conector: " + ioe);
System.exit(0);
}

}

public cliente()
throws IOException {
wireNetwork(s);
createUI();

}

public void shutDown() {
mOut.close();
}

protected void createUI() {
// Create the Input field and give it focus.
register(NO_EVENT_OPTIONS);
mInputField = new TextField("Send", 0, 0, 144, 12);
mInputField.setText("");
mInputField.setFocus();
}

protected void wireNetwork(StreamConnection s)
throws IOException {
try {
mOut = new PrintStream(s.openOutputStream());
}
catch (IOException ioe) {
System.out.println("Error en wirenetwork: " + ioe);
System.exit(0);
}
}

public void keyDown(int key) {
if (mInputField.hasFocus()) {
if (key == '
') sendLine();
else mInputField.handleKeyDown(key);
}
}

protected void sendLine() {
// Send the stuff to the server.

String line = mInputField.getText();
mInputField.setText("Mandando... datos: " + line);
mOut.print(line);
mInputField.setText("Mandado");
}
}

********************************
Server (PC) code is:

import java.io.*;
import java.net.*;
import java.util.*;

import javax.comm.*;

public class Server implements Runnable, SerialPortEventListener
{
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("Dentro del main");
while (portList.hasMoreElements()) {
System.out.println("Inicio del while");
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("Dentro del while. Portid = " +
portId.getName());
if (portId.getPortType() ==
CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
//if (portId.getName().equals("/dev/term/a")) {
System.out.println("Dentro del if. Portid
= " + portId.getName());
Server reader = new Server();
}
}
} //del while */
} //del main

public Server() {
try {
System.out.println("Antes de abrir el puerto");
serialPort = (SerialPort) portId.open("ServerApp", 9600);
System.out.println("Tras abrir el puerto");
} catch (PortInUseException e) {System.out.println (e.toString());}
try {
inputStream = serialPort.getInputStream();
System.out.println("Tras crear el stream de entrada");
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
System.out.println("Tras poner el listener del serial port");
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
System.out.println("Tras poner los parametros");
readThread = new Thread(this);
readThread.start();
System.out.println("Tras el start");
}

public void run() {
try {
Thread.sleep(8000);
System.out.println("Dentro del run");
} catch (InterruptedException e) {System.out.println (e.toString());}
}

public void serialEvent(SerialPortEvent event) {
System.out.println("Se produce un evento");
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:
System.out.println("Hay datos disponibles");
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println (e.toString());}
break;
}
}
}
********************
It works fine, but it doesn't response anything. Please, what's the problem????????

   <<Less
About | Sitemap | Contact