首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从通过USB->RS232连接的终端接收数据

从通过USB->RS232连接的终端接收数据
EN

Stack Overflow用户
提问于 2013-11-13 23:25:36
回答 1查看 1.4K关注 0票数 1

我正在开发一个java应用程序,需要与连接到usb到RS232转换器的终端进行通信!!

现在我可以连接设备并发送数据!我可以肯定终端接收到了发送的数据,因为当终端接收到某些东西时,led会发光!

我正在使用JSSC (链接:https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples)...但由于某种原因,我从未从终端接收到任何数据。

我的代码(JSSC代码):

代码语言:javascript
运行
复制
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");
              }
         }
    }
}
}

有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2015-04-01 14:08:15

您是否打算将硬件流控制与USB-UART一起使用。如果是,请尝试设置DTR,然后设置RTS。这将告诉第一端,第二端已准备好通信。此外,没有接收到10个字节或者根本没有接收到数据。还要考虑另一个串行端口通信库,如单片机http://www.embeddedunveiled.com/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19957662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档