samedi 9 mai 2015

sending a char to usb port by java coded in eclipse IDE

basically I have a aurdino usb interfacing boarding which is already coded to glow an led when it receives a "L" from usb port... I just want a java code. rxtx is already correctly setup.. test file run successfully. please help thanks in advance

here is my code:(I took help from internet)

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

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

 public class SimpleWrite {
@SuppressWarnings("rawtypes")
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "L";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) throws IOException{
    portList = CommPortIdentifier.getPortIdentifiers();
    int i;
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
             if (portId.getName().equals("COM12")) {
           // if (portId.getName().equals("/dev/term/a")) {
        while(( i=System.in.read()) != 13)              
        messageString += (char)i;                  
    try {
                    serialPort = (SerialPort)
                        portId.open("SimpleWriteApp", 2000);
                } catch (PortInUseException e) {System.out.println(e);}
                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {System.out.println(e);}
                try {
                    serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {System.out.println(e);}
                try {               System.out.println(messageString);
                    outputStream.write(messageString.getBytes());
                } catch (IOException e) {System.out.println(e);}            
            }
        }
    }
}
}

there is no warning no error shown in eclipse console. But LED remain in off stage. COM-PORT , BAUDRATE PARITY everything were checked by XCTU.. where I m going wrong please help

Aucun commentaire:

Enregistrer un commentaire