コプロセッサを初期化する(1)

まずはプロパティファイルを読み込みこれを使う準備をすることと、コプロセッサを付けるアダプターを使えるように設定します。
クラス名がinitcoprですから initcopr.java というファイル名で保存してください。
import com.dalsemi.onewire.utils.IOHelper;
import com.dalsemi.onewire.*;
import com.dalsemi.onewire.adapter.*;
import com.dalsemi.onewire.container.*;
import com.dalsemi.onewire.application.sha.*;
import java.util.Vector;
import java.io.*;
import com.dalsemi.onewire.utils.*;
import java.util.*;


public class initcopr
{
   static boolean EMULATE_COPR = false;
   static String SHA_COPR_FILENAME = "sha_copr";

   final static byte[] DEFAULT_COPR_ROMID
      = new byte[]{(byte)0x18,(byte)0xFB,(byte)0x13,0,0,0,0,(byte)0xB2};
ここからmainメソッドが始まる。

   /**
    * Method main
    *
    *
    * 入力: args
    *
    * OneWireExceptionをthrowする
    * OneWireIOExceptionをthrowsする
    *
    */
    public static void main (String[] args)
       throws OneWireIOException, OneWireException, IOException
    {
// プロパティファイルthe sha.properties を開きプロパティを読み込む
        try
        {
           FileInputStream prop_file
              = new FileInputStream("sha.properties");
           sha_properties = new Properties();
           sha_properties.load(prop_file);
        }
        catch(Exception e)
        {
           sha_properties = null;
        }
プロパティファイルの構成の簡単な説明
  1. #あるいは!で始まる行はコメントである
  2. 行の始めのスペースを除いて英文字で始まって数字も含まれるものが変数
  3. 区切りはスペースか=か:である。
  4. 次の英文字で始まって数字も含まれるものが値となる
コプロセッサのコンテナを初期化する
      // ------------------------------------------------------------
      // コプロセッサのコンテナを初期化する
      // ------------------------------------------------------------
      SHAiButtonCopr copr = null;
      OneWireContainer18 copr18 = new OneWireContainer18();
      copr18.setSpeed(DSPortAdapter.SPEED_OVERDRIVE, false);
      copr18.setSpeedCheck(false);
OneWireContainer18クラスはこの実験で用いるSHAのiButton1963Sに対応する クラスである。このクラスは1963Sをコントロールするメソッドなどを持っている。

コプロセッサに対するアダプタを設定する。

      // ------------------------------------------------------------
      // コプロセッサに対するアダプタを設定する
      // ------------------------------------------------------------
      DSPortAdapter coprAdapter = null;
      String coprAdapterName = null, coprPort = null;
      try
      {
         coprAdapterName = getProperty("copr.adapter");
         coprPort = getProperty("copr.port");

         if(coprPort==null || coprAdapterName==null)
         {
            coprAdapter = OneWireAccessProvider.getDefaultAdapter();
         }
         else
         {
            coprAdapter
               = OneWireAccessProvider.getAdapter(coprAdapterName, coprPort);
         }

         IOHelper.writeLine("Coprocessor adapter loaded, adapter: " +
                            coprAdapter.getAdapterName() +
                            " port: " + coprAdapter.getPortName());

         coprAdapter.adapterDetected();
         coprAdapter.targetFamily(0x18);    // 1963Sのみを探すようにする
         coprAdapter.beginExclusive(true);  // 排他制御ON
         coprAdapter.reset();
         coprAdapter.setSearchAllDevices();// Alarm状態も解除して探す
         coprAdapter.reset();
         coprAdapter.putByte(0x3c);
         coprAdapter.setSpeed(coprAdapter.SPEED_OVERDRIVE);//高速度に設定
      }
      catch(Exception e)
      {
         IOHelper.writeLine("Error initializing coprocessor adapter");
         e.printStackTrace();
         System.exit(1);
      }
   }

mainメソッド終了

  1. プロパティリストからcopr.adapterという変数を探し、その値を得る。
  2. 同様にcopr.portを探し、その値を得る。
  3. 実際にはTINにはここに書かれているadapterはportはTINIExternalAdapterで portはserial1でなければいけない。
   static Properties sha_properties = null;
   /**
    * 示されたonewireのプロパティを得る
    * 次の場所からプロパティを探す:
    *   System.properties(環境変数)の中
    *   カレントディレクトリの中のonewire.propertiesファイル
    *       あるいは < java.home >/lib/ (Desktop) or /etc/ (TINI)
    *   プロパティが 'onewire.adapter.default'あるいは'onewire.port.default'
    *     だとスマートである
    *
    * 入力: propName 読み込むプロパティの名前のstring
    *
    * 戻り値:  プロパティ値を表すstringかみつからなかった場合はnullを返す
    *          (onewire.adapter.defaultとonewire.port.defaultの
    *          プロパティが存在していなくとも次のオーバーロードで
    *           default値を返すこともできる)
    */
   public static String getProperty (String propName)
   {
      // first, try system properties
      try
      {
         String ret_str = System.getProperty(propName, null);
         if(ret_str!=null)
            return ret_str;
      }
      catch (Exception e)
      { ; }

      // if defaults not found then try sha.properties file
      if(sha_properties==null)
      {
         //try to load sha_propreties file
         FileInputStream prop_file = null;

         // loop to attempt to open the sha.properties file in two locations
         // .\sha.properties or<java.home>\lib\sha.properties
         String path = "";

         for (int i = 0; i <= 1; i++)
         {

            // attempt to open the sha.properties file
            try
            {
               prop_file = new FileInputStream(path + "sha.properties");
               // attempt to read the onewire.properties
               try
               {
                  sha_properties = new Properties();
                  sha_properties.load(prop_file);
               }
               catch (Exception e)
               {
                  //so we remember that it failed
                  sha_properties = null;
               }
            }
            catch (IOException e)
            {
               prop_file = null;
            }

            // check to see if we now have the properties loaded
            if (sha_properties != null)
               break;

            // try the second path
            path = System.getProperty("java.home") + File.separator + "lib"
                   + File.separator;
         }
      }

      if(sha_properties==null)
      {
         IOHelper.writeLine("Can't find sha.properties file");
         return null;
      }
      else
      {
         Object ret = sha_properties.get(propName);
         if(ret==null)
            return null;
         else
            return ret.toString();
      }
   }
プロパティの値が設定されていなかった場合、第二引数をデフォールトとして使います。
   public static String getProperty (String propName, String defValue)
   {
      String ret = getProperty(propName);
      return (ret==null) ? defValue : ret;
   }
} コプロセッサのエミュレータデータのsha_copr.datと ここに説明してある修正したsha.propertiesをTINIに転送し、 以上をコンパイルしてTINIで実行します。
これをsha.propertiesというファイル名で保存し、実際に合うように修正してください。
Coprocessor adapter loaded, adapter: TINIExternalAdapter port: serial1
と表示して終了します。これが表示されれば、ハードウェア的に接続可能であることが分ります。