Beispiel #1
0
 /// <summary>
 /// 这个方法在执行开始前打开串口,并负责在执行后关闭
 /// </summary>
 /// <param name="info"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public string WriteRegister(FXRegisterBasicInfo info, byte[] value)
 {
     string result = null;
     try
     {
         byte[] cmd = info.GetWriteRegisterCmd(mFXModel, value);
         byte[] cmd2 = info.GetWriteRegisterCmd(mFXModel, -111);
         if (cmd != null)
         {
             if (!this.mSerialPort.IsOpen)
                 this.mSerialPort.Open();
             byte[] replyBytes = this.mSerialPort.Read(cmd);
             this.mSerialPort.Close();
             if (replyBytes[0] != (byte)FXSeriesControl.ACK)
                 result = "PLC Reply Fail";
         }
         else
             result = "Can Not Write " + info.Register + info.NO;
     }
     catch (System.Exception ex)
     {
         result = ex.Message;
     }
     return result;
 }
Beispiel #2
0
 /// <summary>
 /// 这个方法在执行开始前打开串口,并负责在执行后关闭
 /// </summary>
 /// <returns></returns>
 public string ForceOn(FXRegisterBasicInfo info)
 {
     string result = null;
     try
     {
         byte[] cmd = info.GetForceOnOFFCmd(mFXModel, true);
         if (cmd != null)
         {
             if (!this.mSerialPort.IsOpen)
                 this.mSerialPort.Open();
             byte[] replyBytes = this.mSerialPort.Read(cmd);
             this.mSerialPort.Close();
             if (replyBytes[0] != (byte)FXSeriesControl.ACK)
                 result = "PLC Reply Fail";
         }
         else
             result = "Can Not Force " + info.Register + info.NO + " ON";
     }
     catch (System.Exception ex)
     {
         result = ex.Message;
     }
     return result;
 }
Beispiel #3
0
 /// <summary>获得三字指令的操作数信息
 /// 
 /// </summary>
 /// <param name="codes">机器码(至少3个长)</param>
 /// <param name="character"></param>
 /// <param name="registerDict"></param>
 /// <returns></returns>
 private static FXRegisterBasicInfo[] GetRegisterByCharacterTriple(string[] codeArray, ref int index, FXBasicCmdCharacter character, Dictionary<string, FXRegister> registerDict)
 {
     FXRegisterBasicInfo[] infos = null;
     int addr0 = Convert.ToInt32(codeArray[index].Substring(1, 3), 16);              //获取第一个操作数地址
     int addr1 = Convert.ToInt32(codeArray[index + 2].Substring(2, 2) + codeArray[index + 1].Substring(2, 2), 16);       //获取第二个操作数地址
     FXRegisterBasicInfo info0 = GetRegisterInfo(addr0, character.Adapter, 0, FXBasicCmdAdapterType.With, registerDict);
     if (info0 != null)
     {
         FXRegisterBasicInfo info1 = GetRegisterInfo(addr1, character.Adapter, 1, FXBasicCmdAdapterType.With, registerDict);
         if (info1 != null)
         {
             infos = new FXRegisterBasicInfo[] { info0, info1 };
             index = index + 3;
         }
     }
     return infos;
 }
Beispiel #4
0
 /// <summary>从给定的地址中获得Register信息
 /// 当adapterType=With时,需要指定validAdapterIndex来指定需要具体哪一个适配器
 /// </summary>
 /// <param name="address">地址</param>
 /// <param name="adapterValue">适配器的值</param>
 /// <param name="validAdapterIndex">有效的适配器索引(仅当adapterType=With时有效,其余值忽略本参数)</param>
 /// <param name="adapterType">适配器类型</param>
 /// <param name="registerDict">Register字典</param>
 /// <returns></returns>
 private static FXRegisterBasicInfo GetRegisterInfo(int address, string adapterValue, int validAdapterIndex, FXBasicCmdAdapterType adapterType, Dictionary<string, FXRegister> registerDict)
 {
     FXRegisterBasicInfo info = null;
     if (adapterType == FXBasicCmdAdapterType.N)              //适配器类型是N,直接返回常数
     {
         info = new FXRegisterBasicInfo();
         info.Register = FXConfigReader.KCONSTANT;
         info.NO = address;
         return info;
     }
     string[] adapterItems = adapterValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);       //分出adapterValue中的所有Register
     if (adapterType == FXBasicCmdAdapterType.Exclude)     //计算除了adapterValue中的所有元素
     {
         foreach (FXRegister register in registerDict.Values)            //遍历所有Register
         {
             if (!Utils.IsItemInAnotherArray(register.Name, adapterItems))        //只计算不相同的元素
             {
                 string registerNOStr = register.GetRegisterNOStr(address);
                 if (registerNOStr != null)
                 {
                     info = new FXRegisterBasicInfo();
                     info.Register = register.Name;
                     info.NO = Convert.ToInt32(registerNOStr);
                     break;
                 }
             }
         }
     }
     if (adapterType == FXBasicCmdAdapterType.Only)         //只计算adapterValue中指定的元素
     {
         foreach (string adapter in adapterItems)
         {
             FXRegister register = registerDict[adapter];
             string registerNOStr = register.GetRegisterNOStr(address);
             if (registerNOStr != null)
             {
                 info = new FXRegisterBasicInfo();
                 info.Register = register.Name;
                 info.NO = Convert.ToInt32(registerNOStr);
                 break;
             }
         }
     }
     if (adapterType == FXBasicCmdAdapterType.With)           //只计算adapterValue中由validAdapterIndex指定的元素
     {
         string adapter = adapterItems[validAdapterIndex];
         if (adapter.Equals(FXConfigReader.KCONSTANT))        //如果是K的话说明是常数,不用算了
         {
             info = new FXRegisterBasicInfo();
             info.Register = FXConfigReader.KCONSTANT;
             info.NO = address;
         }
         else
         {
             FXRegister register = registerDict[adapter];
             string registerNOStr = register.GetRegisterNOStr(address);
             if (registerNOStr != null)
             {
                 info = new FXRegisterBasicInfo();
                 info.Register = register.Name;
                 info.NO = Convert.ToInt32(registerNOStr);
             }
         }
     }
     return info;
 }
Beispiel #5
0
 /// <summary>获得单字或多字指令的操作数信息
 /// 
 /// </summary>
 /// <param name="code">如果是Single的,那么就是4位的机器码;如果是Double的,那么就是8位机器码中的后四位</param>
 /// <param name="character"></param>
 /// <param name="registerDict"></param>
 /// <returns></returns>
 private static FXRegisterBasicInfo[] GetRegisterByCharacterSingleAndDouble(string[] codeArray, ref int index, FXBasicCmdCharacter character, Dictionary<string, FXRegister> registerDict)
 {
     FXRegisterBasicInfo[] infos = null;
     if (character.BasicCmdType == FXBasicCmdType.Double)
         index++;
     int specifiedAddr = Convert.ToInt32(codeArray[index].Substring(1, 3), 16);      //单字和双字命令的操作数地址为给定4位机器码的后三位
     index++;
     FXRegisterBasicInfo info = GetRegisterInfo(specifiedAddr, character.Adapter, -1, character.AdapterType, registerDict);
     infos = new FXRegisterBasicInfo[] { info };
     return infos;
 }