Exemplo n.º 1
0
 /// <summary>
 /// 将制定PLC的地址改为新的地址
 /// </summary>
 /// <param name="fOldPLCAddress">原PLC地址</param>
 /// <param name="fNewPLCAddress">新的PLC地址</param>
 public void SetPLCAddress(int fNewPLCAddress)
 {
     if (PLCActive() && (fSerialPortBus != null))
     {
         if (!string.IsNullOrEmpty(fPLCModbusNode.PLCSNStr))
         {
             int[] fDataBuf = new int[] { CRC16_GB19582.CRC16GB(fPLCModbusNode.PLCSNByteLF, TCRC16Endian.Big),
                                          fNewPLCAddress }; // 写入2个字节寄存器
             TModbusRTUCommandV2 fCommand = new TModbusRTUCommandV2(ModbusCommand.WriteMemoryReg_0x10);
             fCommand.PLCAddress      = (byte)PLCAddress;
             fCommand.RegWriteAddress = 0;       // 写入本机地址
             fCommand.RegWriteCount   = (ushort)fDataBuf.Length;
             for (int i = 0; i < fDataBuf.Length; i++)
             {
                 fCommand.RegValue[i] = (ushort)fDataBuf[i];
             }
             int fTimeOut = 100;
             fSerialPortBus.SendModbusCommand(fCommand, ref fTimeOut);
         }
     }
     else
     {
         throw new Exception("目标PLC无法操作!");
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 写入特殊寄存器值
 /// </summary>
 /// <param name="fRegIdx">寄存器地址序号</param>
 /// <param name="fADCSet">要设置的值</param>
 public void WriteSetReg(ushort fRegIdx, ushort fADCSet)
 {
     if (PLCActive() && (fSerialPortBus != null))
     {
         int[] fDataBuf = new int[] { CRC16_GB19582.CRC16GB(fPLCModbusNode.PLCSNByteLF, TCRC16Endian.Big),
                                      fADCSet }; // 写入2个字节寄存器
         TModbusRTUCommandV2 fCommand = new TModbusRTUCommandV2(ModbusCommand.WriteMemoryReg_0x10);
         fCommand.PLCAddress      = (byte)PLCAddress;
         fCommand.RegWriteAddress = fRegIdx;       // 写入本机地址
         fCommand.RegWriteCount   = (ushort)fDataBuf.Length;
         for (int i = 0; i < fDataBuf.Length; i++)
         {
             fCommand.RegValue[i] = (ushort)fDataBuf[i];
         }
         int fTimeOut = 100;
         fSerialPortBus.SendModbusCommand(fCommand, ref fTimeOut);
     }
 }
Exemplo n.º 3
0
        public int SetPLCRunTimeCFG(ushort[] RTCFGDatas)
        {
            int fResult = 0;
            TModbusRTUCommandV2 fCommand = new TModbusRTUCommandV2(ModbusCommand.HBHCCommand_0x42);

            fCommand.PLCAddress    = (byte)PLCAddress;
            fCommand.MainCode      = 0x0A;
            fCommand.SubCode       = ADP1701VX.SubCode_SetCFG;
            fCommand.CryptKey      = (ushort)CRC16_GB19582.CRC16GB(PLCSNByteLF, TCRC16Endian.Big);
            fCommand.RegWriteCount = (ushort)RTCFGDatas.Length;
            for (int i = 0; i < fCommand.RegWriteCount; i++)
            {
                fCommand.RegValue[i] = (ushort)RTCFGDatas[i];
            }
            int fTimeOut = 100;

            fResult = base.fSerialPortBus.SendModbusCommand(fCommand, ref fTimeOut);
            return(fResult);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 写入PLC的序列号;
 /// </summary>
 /// <param name="fPLCSN"></param>
 /// <param name="fPLCAddress"></param>
 public void SetPLCSN(byte[] fPLCSN)
 {
     if (PLCActive() && (fSerialPortBus != null))
     {
         if ((fPLCSN != null) && (fPLCSN.Length == 12))
         {
             List <ushort> fDataBuf = new List <ushort>();
             fDataBuf.Add((ushort)CRC16_GB19582.CRC16GB(PLCSNByteLF, TCRC16Endian.Big));
             // 将要写入的PLCSN放入,转换为ushort,高位在前,写入缓冲区
             ushort fVTemp = 0;
             for (int i = 0; i < fPLCSN.Length; i++)
             {
                 if (i % 2 == 0)     //寄存器低位: 0,2,4,6,8,10,12
                 {
                     fVTemp = fPLCSN[i];
                 }
                 else
                 {       //寄存器高位: 1,3,5,7,9,11
                     ushort fVT1 = fPLCSN[i];
                     fVTemp <<= 8;
                     fVTemp  |= fVT1;
                     fDataBuf.Add(fVTemp);
                     fVTemp = 0;
                 }
             }
             TModbusRTUCommandV2 fCommand = new TModbusRTUCommandV2(ModbusCommand.WriteMemoryReg_0x10);
             fCommand.PLCAddress      = (byte)this.PLCAddress;
             fCommand.RegWriteAddress = 2;       // 写入PLCSN,不是所有PLC都能够写入PLCSN
             fCommand.RegWriteCount   = (ushort)fDataBuf.Count;
             for (int i = 0; i < fDataBuf.Count; i++)
             {
                 fCommand.RegValue[i] = fDataBuf[i];
             }
             int fTimeOut = 100;
             fSerialPortBus.SendModbusCommand(fCommand, ref fTimeOut);
         }
     }
     else
     {
         throw new Exception("目标PLC无法操作!");
     }
 }