简体   繁体   中英

Receiving data from Terminal connected with USB->RS232

I'm developing a java application that need communicate with a terminal connected with a usb-to-rs232 converter!!

Right now I can connect with device and send data! I can be sure that the terminal receive the data sent because a led glow when the terminal receive something!

I'm using JSSC (Link: https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples )... but for some reason I never never never receive any data FROM the Terminal.

My code (JSSC code):

public class Main
{

    static SerialPort serialPort;

    public static void main(String[] args) throws InterruptedException
    {
        serialPort = new SerialPort("COM7"); 
        try
        {
             serialPort.openPort();//Open port
             serialPort.setParams(9600, 8, 1, 0);//Set params
             int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
             serialPort.setEventsMask(mask);//Set mask
             serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener

             serialPort.writeByte( (byte)0x02 );

             TimeUnit.SECONDS.sleep( 10 );

             byte[] b = serialPort.readBytes();
             System.out.println( "bytes " + b );
        }
        catch (SerialPortException ex)
        {
             System.out.println(ex);
        }
}

/*
 * In this class must implement the method serialEvent, through it we learn about 
 * events that happened to our port. But we will not report on all events but only 
 * those that we put in the mask. In this case the arrival of the data and change the 
 * status lines CTS and DSR
 */
static class SerialPortReader implements SerialPortEventListener
{
    public void serialEvent(SerialPortEvent event)
    {
         System.out.println( "Event raised!" );
         if(event.isRXCHAR())
         {//If data is available
              if(event.getEventValue() == 10)
              {//Check bytes count in the input buffer
              //Read data, if 10 bytes available 
                   try
                   {
                        byte buffer[] = serialPort.readBytes(10);
                   }
                   catch (SerialPortException ex)
                   {
                        System.out.println(ex);
                   }
              }
         }
         else if(event.isCTS())
         {//If CTS line has changed state
              if(event.getEventValue() == 1)
              {//If line is ON
                  System.out.println("CTS - ON");
              }
              else
              {
                   System.out.println("CTS - OFF");
              }
         }
         else if(event.isDSR())
         {///If DSR line has changed state
              if(event.getEventValue() == 1)
              {//If line is ON
                  System.out.println("DSR - ON");
              }
              else
              {
                   System.out.println("DSR - OFF");
              }
         }
    }
}
}

Can anyone help me with this issue?

Did you intend to use hardware flow control with USB-UART. If yes, try setting DTR followed by RTS. This tells 1st end that 2nd end is ready for communication. Further does 10 bytes are not received or no data is received at all. Also consider another serial port communication library like scm http://www.embeddedunveiled.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM