J'ai réussi à installer et tester l'API Java Comm. Je pourrais envoyer et recevoir des données via le port série. Mais le problème est qu'il y a 19 espaces supplémentaires avant chaque personnage. Lorsque j'ai essayé d'utiliser hyper terminal, les espaces supplémentaires n'étaient pas là.Java Comm API Issue
Par exemple:
+ CENG: 0, "0021,37,99,404,46,36,0000,05,05,77,255"
est attendue. Mais le programme Java, il génère des espaces avant chaque charector
+ E N
Le code est:
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) throws Exception
{
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("SimpleRead Started.");
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println ("Found " + portId.getName());
if (portId.getName().equals("COM5"))
{
OutputStream outputStream;
SerialPort writePort = (SerialPort) portId.open("SimpleWriteApp", 2000);
writePort.setSerialPortParams(2400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream = writePort.getOutputStream();
outputStream.write("AT+CENG=2".getBytes());
writePort.close();
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead()
{
try
{
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
}
catch (PortInUseException e)
{
e.printStackTrace();
}
try
{
inputStream = serialPort.getInputStream();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
serialPort.addEventListener(this);
}
catch (TooManyListenersException e)
{
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try
{
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
}
public void run()
{
try
{
Thread.sleep(20000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
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];
try
{
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
}
catch (IOException e)
{
e.printStackTrace();
}
break;
}
}
}
Êtes-vous sûr votre vitesse de transmission/contrôle de flux/bits d'arrêt sont correctement réglés? Pouvez-vous montrer votre code? Pour tout ce que nous savons, vous imprimez juste 19 espaces avant chaque personnage. –
J'ai mis les bits correctement car dans hyper terminal j'ai utilisé la même configuration. Mon code peut être trouvé dans http://www.wepaste.com/harikk09 (désolé pour les problèmes d'alignement) – Harikrishnan
À l'avenir, s'il vous plaît inclure votre code avec votre message. –