简体   繁体   English

从连接了USB-> RS232的终端接收数据

[英]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!! 我正在开发一个Java应用程序,该应用程序需要与通过usb-to-rs232转换器连接的终端进行通信!!

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! 我可以确定终端接收到了发送的数据,因为当终端接收到某些东西时,LED会发光!

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. 我正在使用JSSC (链接: https : //code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples )...但是由于某些原因,我从来没有从终端接收过任何数据。

My code (JSSC code): 我的代码(JSSC代码):

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. 您打算将硬件流控制与USB-UART一起使用吗? If yes, try setting DTR followed by RTS. 如果是,请尝试先设置DTR,再设置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. 此外,没有接收到10个字节或根本没有接收到数据。 Also consider another serial port communication library like scm http://www.embeddedunveiled.com/ 还要考虑另一个串行端口通信库,例如scm http://www.embeddedunveiled.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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