niagara框架中串口的打開方式

一直好奇niagara框架中串口是如何打開的,最近通過看源碼,大致明白了,現記錄如下

首先從BModbusAsyncNetwork這個類看起,在niagara help中查找沒有源碼,不過沒關係,利用反編譯看,重點代碼如下


//BModbusAsyncNetwork是一個BIService
public void serviceStarted() throws Exception {
//必須看父類的實現
        super.serviceStarted();
        getNameSubscriber().subscribe(getSerialPortConfig());
        getSerialPortConfig().setSerialHelperParent(this);
        ...
    }

    public void startComm() throws Exception {
        if (getSerialPortConfig().getPortName().equals("none")) {
            configFail("No port selected for Modbus communication.");
        } else {
            try {
//還是要看父類的實現
                super.startComm();
                configOk();
            } catch (Exception e) {
                configFail(new StringBuffer("Could not enable Modbus serial communication (").append(e).append(')').toString());
                throw e;
            }
        }
        ...
    }

//對父類抽象方法的重寫  
protected Comm makeComm() {
        return new ModbusAsyncSerialComm(this);
    }

回溯父類,其中BBasicNetwork比較重要(有niagara源碼)

/**
   * Builds the communication handler (Comm).
   */
  public void serviceStarted()
    throws Exception
  {
    buildComm();
  }

 /**
   * Initializes the basic network with a new communication handler (Comm)
   * for both the transmit and receive drivers.  After creating
   * a new Comm, this method calls initComm(Comm comm)
   * with the new Comm to allow subclasses to perform any initialization
   * (i.e. adding custom UnsolicitedMessageListeners for handling unsolicited received 
   * messages).  
   */
  private void buildComm()
    throws Exception
  {
    comm = makeComm();
    initComm(comm);
  }
 /**
   * This method starts the Communication handler
   * (Comm) if the network is not down/fault/out-of-service
   * and the current Comm is not null.
   */
  public void startComm()
    throws Exception
  {
    if((!isDisabled()) && (!isFatalFault()) && (comm != null))
    {
      if (getLog().isTraceOn()) getLog().trace(getName() + " *** Starting Communication Handler ***");
      comm.start();
      if (getLog().isTraceOn()) getLog().trace(getName() + " *** Started Communication Handler ***");
    }
  }
  /**
   * Start the basic network.  This starts the Communication handler
   * (Comm) if the network is not down/fault/out-of-service.
   */
  public void started()
    throws Exception
  {                 
    super.started();
    try
    {
      startComm();
    }
    catch (Exception e)
    {
      getLog().error("Could not start communication handler", e);
    }
  }
接下來要看comm.start(),comm是Comm對象(有niagara源碼)其start()裏主要調用了started(),對於BModbusAsyncNetwork,comm是ModbusAsyncSerialComm ,
    protected boolean started() throws Exception {
        try {
            BISerialService bISerialService = (BISerialService) Sys.getService(BISerialService.TYPE);
            this.serialPort = ((BModbusAsyncNetwork) getNetwork()).getSerialPortConfig().open(getNetwork().getName());
            this.serialPort.enableReceiveTimeout(bISerialService.getMinTimeout());
            this.in = this.serialPort.getInputStream();
            this.out = this.serialPort.getOutputStream();
            ...
getSerialPortConfig()返回的是BSerialHelper對象(有niagara源碼),其open()方法爲
  /**
   * Opens the serial port and sets the port parameters.
   * Returns the serial port as a BISerialPort.
   * 
   * @param String owner - The name of the owner to set for the serial port.
   */
  public BISerialPort open(String owner)
    throws Exception
  {        
    BISerialService platSvc = (BISerialService) Sys.getService(BISerialService.TYPE);
    ((BComponent)platSvc).lease();  // create a subscription to force the platform service to lazy-init

    try
    {
      port = platSvc.openPort(getPortName(), owner);
    }
可以看到是通過BISerialService來打開串口的,遇到接口就只能猜測一下了。。。還好,最終發現在我的電腦上是BSerialPortPlatformServiceWin64,它的父類BSerialPortPlatformService實現了該接口,openPort()的核心代碼如下
  public BISerialPort openPort(String paramString1, String paramString2)
    throws PortNotFoundException, PortDeniedException
  {
    ...
    Object localObject = (BSerialPort)get(paramString1);
    ...
    try
    {
      ((BSerialPort)localObject).openPort();
      return new BSerialPortHandle((BSerialPort)localObject);
    }
    ...
  }
get(paramString1)這個看上去比較奇怪,其實它是BComplex的方法(獲得slot),這些BSerialPort是如何添加的能,這就要看它的抽象方法loadPortIdentifiers()了,這裏看BSerialPortPlatformServiceWin64的實現
  protected String[] loadPortIdentifiers()
    throws Exception
{   
    ... 
    Enumeration localEnumeration = CommPortIdentifier.getPortIdentifiers();
    while (localEnumeration.hasMoreElements())
    {
      CommPortIdentifier localCommPortIdentifier1 = (CommPortIdentifier)localEnumeration.nextElement();
      if (localCommPortIdentifier1.getPortType() == 1)
      {
        localVector.add(localCommPortIdentifier1);
      }
    }
    ...
        BSerialPortWin64 localBSerialPortWin64 = new BSerialPortWin64(arrayOfCommPortIdentifier[j], j);

        add(SlotPath.escape(arrayOfString[j]), localBSerialPortWin64, 3);
}
是不是很熟悉了,RxTx庫哈哈,add()實現slot的動態添加
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章