Exemple #1
0
 private void DoWriteTcInstructions(AdsBinaryWriter writer)
 {
     writer.Write(inst.Handle);
     writer.Write(inst.Index);
     writer.Write(inst.Step);
     writer.Write(inst.Active);
     writer.Write(inst.State);
 }
Exemple #2
0
        internal void WritePointerValue(string symbolPath, object value, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if ((byteSize < 0) || ((byteSize != 4) && (byteSize != 8)))
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type targetType = null;

            if (byteSize == 4)
            {
                targetType = typeof(uint);
            }
            else if (byteSize == 8)
            {
                targetType = typeof(ulong);
            }
            if (targetType != value.GetType())
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, targetType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{targetType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            if (byteSize == 4)
            {
                writer.Write((uint)value);
            }
            else if (byteSize == 8)
            {
                writer.Write((ulong)value);
            }
        }
Exemple #3
0
        private void adsSendData(double shoulderLevelShift, double elbow, double wrist, double hand)
        {
            adsReadStream  = new AdsStream(40);
            adsWriteStream = new AdsStream(sizeof(double) * 4);
            AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);

            adsWriteStream.Position = 0;

            binWriter.Write(shoulderLevelShift);
            binWriter.Write(elbow);
            binWriter.Write(wrist);
            binWriter.Write(hand);

            _tcClient.ReadWrite(0x1, 0x1, null, adsWriteStream);
        }
        public static tcFunctionResult tcWriteData(int index, object data)
        {
            if (!tcClient.IsConnected)
            {
                LogMessage(string.Format("Error\t: {0}", "Attempted to write to PLC when not connected"));
                return(tcFunctionResult.TC_NOT_CONNECTED);
            }
            if (index > tcVarList.Count || index < 0)
            {
                LogMessage(string.Format("Error\t: {0}", "Attempted to access a value outside the list"));
                return(tcFunctionResult.TC_VARLIST_OUTOFBOUND);
            }
            AdsStream       _DataStrem     = new AdsStream(tcVarList[index].DataSize);
            AdsBinaryWriter tcStreamWriter = new AdsBinaryWriter(_DataStrem);

            try
            {
                switch (tcVarList[index].VariableType.ToLower())
                {
                case "bool":
                    tcStreamWriter.Write((bool)data);
                    tcClient.Write(tcVarList[index].Handle, _DataStrem);
                    break;

                case "arbool":
                    break;

                case "int":
                    break;

                case "arint":
                    break;

                case "string":
                    break;

                case "real":
                    break;

                case "lreal":
                    break;
                }
                tcStreamWriter.Close();
            }
            catch (Exception ex)
            {
                LogMessage(string.Format("Error at writing data {0}\t: {1}", index.ToString(), ex.Message));
                return(tcFunctionResult.TC_FAIL_TO_WRITE_DATA);
            }
            finally
            {
                tcStreamWriter.Close();
            }
            return(tcFunctionResult.TC_SUCCESS);
        }
Exemple #5
0
        private void adsSendShoulder(double shoulderLevelShift)
        {
            //adsReadStream = new AdsStream(40);
            adsWriteStream = new AdsStream(sizeof(double));
            AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);

            adsWriteStream.Position = 0;

            binWriter.Write(shoulderLevelShift);

            _tcClient.ReadWrite(0x1, 0x2, null, adsWriteStream);
        }
Exemple #6
0
        private void DoWriteTcArgs(AdsBinaryWriter writer)
        {
            int i;

            for (i = 0; i < 8; i++)
            {
                writer.Write(inst.Args.Bools[i]);
            }
            for (i = 0; i < 8; i++)
            {
                writer.Write(inst.Args.Bytes[i]);
            }
            for (i = 0; i < 8; i++)
            {
                writer.Write(inst.Args.Int16s[i]);
            }
            for (i = 0; i < 8; i++)
            {
                writer.Write(inst.Args.Int32s[i]);
            }
            for (i = 0; i < 8; i++)
            {
                writer.Write(inst.Args.Floats[i]);
            }
            for (i = 0; i < 8; i++)
            {
                writer.Write(inst.Args.Doubles[i]);
            }
        }
Exemple #7
0
 private void SendIndexCtr(int indexCtr)                 //发送控制轴的索引
 {
     try
     {
         AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
         adsWriteStream.Position = 0;
         binWriter.Write(indexCtr);
         _tcClient.ReadWrite(0x02, 0x02, adsReadStream, adsWriteStream);
         byte[] dataBuffer2 = adsReadStream.ToArray();
     }
     catch
     {
         MessageBox.Show("索引发送错误!");
     }
 }
Exemple #8
0
 private void SendPos(double Pos)
 {
     try
     {
         AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
         adsWriteStream.Position = 0;
         binWriter.Write(Pos);
         _tcClient.ReadWrite(0x02, 0x03, adsReadStream, adsWriteStream);
         byte[] dataBuffer3 = adsReadStream.ToArray();
     }
     catch
     {
         MessageBox.Show("位置指令发送错误!");
     }
 }
Exemple #9
0
 private void SendVelY(double VelY)
 {
     try
     {
         AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
         adsWriteStream.Position = 0;
         binWriter.Write(VelY);
         _tcClient.ReadWrite(0x02, 0x08, adsReadStream, adsWriteStream);
         byte[] dataBuffer6 = adsReadStream.ToArray();
     }
     catch
     {
         MessageBox.Show("Y速度指令发送错误!");
     }
 }
Exemple #10
0
 private void SendAutoInstruct(int instruct)
 {
     try
     {
         AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
         adsWriteStream.Position = 0;
         binWriter.Write(instruct);
         _tcClient.ReadWrite(0x02, 0x10, adsReadStream, adsWriteStream);
         byte[] dataBuffer5 = adsReadStream.ToArray();
     }
     catch
     {
         MessageBox.Show("指令发送错误!");
     }
 }
Exemple #11
0
        public void WriteUInt64(string name, UInt64 value)
        {
            int handle = GetHandle(name);

            lock (criticalLock)
            {
                using (AdsStream stream = new AdsStream())
                {
                    using (AdsBinaryWriter writer = new AdsBinaryWriter(stream))
                    {
                        writer.Write(value);
                        client.Write(handle, stream);
                    }
                }
            }
        }
        private void Overwrite_Clicked(object sender, RoutedEventArgs e)
        {
            try
            {
                AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
                adsWriteStream.Position = 0;

                binWriter.Write(uint.Parse(Overwrite));
                _tcClient.ReadWrite(0x3, 0x1, adsReadStream, adsWriteStream);
                byte[] dataBuffer = adsReadStream.ToArray();
                Box.Text = "Counter overwritten with Value = " + BitConverter.ToUInt32(dataBuffer, 0);
            }

            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private void btOverwrite_Click(object sender, EventArgs e)
        {
            try
            {
                AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
                adsWriteStream.Position = 0;

                binWriter.Write(uint.Parse(tbValue.Text));
                _tcClient.ReadWrite(0x3, 0x1, adsReadStream, adsWriteStream);
                byte[] dataBuffer = adsReadStream.ToArray();
                lbOutput.Items.Add("Counter overwritten with Value = " + BitConverter.ToUInt32(dataBuffer, 0));
                lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
            }

            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Exemple #14
0
        public void WriteUInt64(string name, UInt64[] value)
        {
            int handle = GetHandle(name);

            lock (criticalLock)
            {
                using (AdsStream stream = new AdsStream())
                {
                    using (AdsBinaryWriter writer = new AdsBinaryWriter(stream))
                    {
                        for (int i = 0; i < value.Length; i++)
                        {
                            writer.Write(value[i]);
                        }

                        client.Write(handle, stream);
                    }
                }
            }
        }
        private void lblStop_Click(object sender, EventArgs e)
        {
            lblPause.BackColor = Color.Empty;
            lblStart.BackColor = Color.Empty;
            lblStop.BackColor  = Color.Green;
            // 用sample03, adsbinaryWriter and adsstream 方法
            AdsStream       stopStream = new AdsStream(1);
            AdsBinaryWriter stopWriter = new AdsBinaryWriter(stopStream);

            try
            {
                stopWriter.Write(true);
                client.Write(hStopUI, stopStream);
                client.WriteAny(hPauseUI, false);
            }
            catch (Exception err)
            {
                MessageBox.Show("write stop to PLC: " + err.Message);
            }
        }
        private void lblStart_Click(object sender, EventArgs e)
        {
            //
            AdsStream       btnStream = new AdsStream(1);
            AdsBinaryWriter btnWriter = new AdsBinaryWriter(btnStream);

            try
            {
                btnWriter.Write(true);
                client.Write(hStartUI, btnStream);

                //------adsClient的WriteAny方法
                client.WriteAny(hPauseUI, false);
                client.WriteAny(hStopUI, false);
                //------adsClient的WriteAny方法
            }
            catch (Exception err)
            {
                MessageBox.Show("write start value to PLC: " + err.Message);
            }
            lblPause.BackColor = Color.Empty;
            lblStart.BackColor = Color.Green;
            lblStop.BackColor  = Color.Empty;
        }
Exemple #17
0
        //连接ADS


        private void Cycle()
        {
            int indexStatus = 0;

            while (true)
            {
                //发送要读取的轴的索引
                try
                {
                    AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream);
                    adsWriteStream.Position = 0;
                    binWriter.Write(indexStatus);
                    _tcClient.ReadWrite(0x2, 0x9, adsReadStream, adsWriteStream);
                    byte[] dataBuffer = adsReadStream.ToArray();
                    lbOutput.Items.Add("要读取的轴的索引为: " + BitConverter.ToUInt32(dataBuffer, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                }
                catch
                {
                    MessageBox.Show("读写轴索引发生错误!");
                }
                //读取对应索引的轴的使能情况
                try
                {
                    _tcClient.ReadWrite(0x1, 0x1, adsReadStream, adsWriteStream);
                    byte[] dataBuffer3 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的使能情况为: " + BitConverter.ToInt32(dataBuffer3, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_Enable     = BitConverter.ToUInt32(dataBuffer3, 0);
                    //DrivingWheel1Enable.Text = GlobalVar.R_Enable.ToString();
                }
                catch
                {
                    MessageBox.Show("读取轴使能出现错误!");
                }
                //读取对应索引的轴的速度大小
                try
                {
                    _tcClient.ReadWrite(0x1, 0x2, adsReadStream, adsWriteStream);
                    byte[] dataBuffer4 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的速度为: " + BitConverter.ToDouble(dataBuffer4, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActVelo    = BitConverter.ToDouble(dataBuffer4, 0);
                    GlobalVar.R_ActVelo    = ((int)(GlobalVar.R_ActVelo * 100)) / 100.0;
                    //DrivingWheel1ActVel.Text = GlobalVar.R_ActVelo.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴速度出现错误!");
                }
                //读取对应索引轴的位置
                try
                {
                    _tcClient.ReadWrite(0x1, 0x3, adsReadStream, adsWriteStream);
                    byte[] dataBuffer5 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的位置为: " + BitConverter.ToDouble(dataBuffer5, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActPos     = BitConverter.ToDouble(dataBuffer5, 0);
                    GlobalVar.R_ActPos     = ((int)(GlobalVar.R_ActPos * 100)) / 100.0;
                    //DrivingWheel1ActPos.Text = GlobalVar.R_ActPos.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }


                //读取对应索引轴的电流
                try
                {
                    _tcClient.ReadWrite(0x1, 0x4, adsReadStream, adsWriteStream);
                    byte[] dataBuffer9 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的电流为: " + BitConverter.ToInt16(dataBuffer9, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActCur     = BitConverter.ToInt16(dataBuffer9, 0);
                    //DrivingWheel1ActPos.Text = GlobalVar.R_ActPos.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }
                //读取对应索引轴的位置
                try
                {
                    _tcClient.ReadWrite(0x1, 0x5, adsReadStream, adsWriteStream);
                    byte[] dataBuffer6 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的位置为: " + BitConverter.ToDouble(dataBuffer6, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActPosX    = BitConverter.ToDouble(dataBuffer6, 0);
                    GlobalVar.R_ActPosX    = ((int)(GlobalVar.R_ActPosX * 100)) / 100.0;
                    tx_handx.Text          = GlobalVar.R_ActPosX.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }
                //读取对应索引轴的位置
                try
                {
                    _tcClient.ReadWrite(0x1, 0x6, adsReadStream, adsWriteStream);
                    byte[] dataBuffer7 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的位置为: " + BitConverter.ToDouble(dataBuffer7, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActPosY    = BitConverter.ToDouble(dataBuffer7, 0);
                    GlobalVar.R_ActPosY    = ((int)(GlobalVar.R_ActPosY * 100)) / 100.0;
                    tx_handy.Text          = GlobalVar.R_ActPosY.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }
                try
                {
                    _tcClient.ReadWrite(0x1, 0x7, adsReadStream, adsWriteStream);
                    byte[] dataBuffer8 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的位置为: " + BitConverter.ToDouble(dataBuffer8, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActPosX    = BitConverter.ToDouble(dataBuffer8, 0);
                    GlobalVar.R_ActPosX    = ((int)(GlobalVar.R_ActPosX * 100)) / 100.0;
                    tx_bigarm.Text         = GlobalVar.R_ActPosX.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }
                try
                {
                    _tcClient.ReadWrite(0x1, 0x8, adsReadStream, adsWriteStream);
                    byte[] dataBuffer9 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的位置为: " + BitConverter.ToDouble(dataBuffer9, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActPosX    = BitConverter.ToDouble(dataBuffer9, 0);
                    GlobalVar.R_ActPosX    = ((int)(GlobalVar.R_ActPosX * 100)) / 100.0;
                    tx_middlearm.Text      = GlobalVar.R_ActPosX.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }
                try
                {
                    _tcClient.ReadWrite(0x1, 0x9, adsReadStream, adsWriteStream);
                    byte[] dataBuffer10 = adsReadStream.ToArray();
                    lbOutput.Items.Add("当前轴的位置为: " + BitConverter.ToDouble(dataBuffer10, 0));
                    lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
                    GlobalVar.R_ActPosX    = BitConverter.ToDouble(dataBuffer10, 0);
                    GlobalVar.R_ActPosX    = ((int)(GlobalVar.R_ActPosX * 100)) / 100.0;
                    tx_smallarm.Text       = GlobalVar.R_ActPosX.ToString();
                }
                catch
                {
                    MessageBox.Show("读取当前轴位置出现错误!");
                }
                try
                {
                    switch (indexStatus)
                    {
                    case 0:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            DrivingWheel1Enable.Text      = "Enable";
                            DrivingWheel1Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            DrivingWheel1Enable.Text      = "Disable";
                            DrivingWheel1Enable.BackColor = Color.Red;
                        }
                        DrivingWheel1ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        DrivingWheel1ActPos.Text = GlobalVar.R_ActPos.ToString();
                        DrivingWheel1ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 1:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            DrivingWheel2Enable.Text      = "Enable";
                            DrivingWheel2Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            DrivingWheel2Enable.Text      = "Disable";
                            DrivingWheel2Enable.BackColor = Color.Red;
                        }
                        DrivingWheel2ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        DrivingWheel2ActPos.Text = GlobalVar.R_ActPos.ToString();
                        DrivingWheel2ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 2:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            DrivingWheel3Enable.Text      = "Enable";
                            DrivingWheel3Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            DrivingWheel3Enable.Text      = "Disable";
                            DrivingWheel3Enable.BackColor = Color.Red;
                        }
                        DrivingWheel3ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        DrivingWheel3ActPos.Text = GlobalVar.R_ActPos.ToString();
                        DrivingWheel3ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 3:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            DrivingWheel4Enable.Text      = "Enable";
                            DrivingWheel4Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            DrivingWheel4Enable.Text      = "Disable";
                            DrivingWheel4Enable.BackColor = Color.Red;
                        }
                        DrivingWheel4ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        DrivingWheel4ActPos.Text = GlobalVar.R_ActPos.ToString();
                        DrivingWheel4ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 4:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            SwingArm1Enable.Text      = "Enable";
                            SwingArm1Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            SwingArm1Enable.Text      = "Disable";
                            SwingArm1Enable.BackColor = Color.Red;
                        }
                        SwingArm1ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        SwingArm1ActPos.Text = GlobalVar.R_ActPos.ToString();
                        SwingArm1ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 5:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            SwingArm2Enable.Text      = "Enable";
                            SwingArm2Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            SwingArm2Enable.Text      = "Disable";
                            SwingArm2Enable.BackColor = Color.Red;
                        }
                        SwingArm2ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        SwingArm2ActPos.Text = GlobalVar.R_ActPos.ToString();
                        SwingArm2ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 6:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            SwingArm3Enable.Text      = "Enable";
                            SwingArm3Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            SwingArm3Enable.Text      = "Disable";
                            SwingArm3Enable.BackColor = Color.Red;
                        }
                        SwingArm3ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        SwingArm3ActPos.Text = GlobalVar.R_ActPos.ToString();
                        SwingArm3ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 7:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            SwingArm4Enable.Text      = "Enable";
                            SwingArm4Enable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            SwingArm4Enable.Text      = "Disable";
                            SwingArm4Enable.BackColor = Color.Red;
                        }
                        SwingArm4ActVel.Text = GlobalVar.R_ActVelo.ToString();
                        SwingArm4ActPos.Text = GlobalVar.R_ActPos.ToString();
                        SwingArm4ActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 9:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            BigArmEnable.Text      = "Enable";
                            BigArmEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            BigArmEnable.Text      = "Disable";
                            BigArmEnable.BackColor = Color.Red;
                        }
                        BigArmActVel.Text = GlobalVar.R_ActVelo.ToString();
                        BigArmActPos.Text = GlobalVar.R_ActPos.ToString();
                        BigArmActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 10:
                        if (GlobalVar.R_Enable != 0)           //已经使能
                        {
                            FlexEnable.Text      = "Enable";
                            FlexEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            FlexEnable.Text      = "Disable";
                            FlexEnable.BackColor = Color.Red;
                        }
                        FlexActVel.Text = GlobalVar.R_ActVelo.ToString();
                        FlexActPos.Text = GlobalVar.R_ActPos.ToString();
                        FlexActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 11:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            MiddleArmEnable.Text      = "Enable";
                            MiddleArmEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            MiddleArmEnable.Text      = "Disable";
                            MiddleArmEnable.BackColor = Color.Red;
                        }
                        MiddleArmActVel.Text = GlobalVar.R_ActVelo.ToString();
                        MiddleArmActPos.Text = GlobalVar.R_ActPos.ToString();
                        MiddleArmActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 12:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            SmallArmEnable.Text      = "Enable";
                            SmallArmEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            SmallArmEnable.Text      = "Disable";
                            SmallArmEnable.BackColor = Color.Red;
                        }
                        SmallArmActVel.Text = GlobalVar.R_ActVelo.ToString();
                        SmallArmActPos.Text = GlobalVar.R_ActPos.ToString();
                        SmallArmActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 13:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            RotationEnable.Text      = "Enable";
                            RotationEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            RotationEnable.Text      = "Disable";
                            RotationEnable.BackColor = Color.Red;
                        }
                        RotationActVel.Text = GlobalVar.R_ActVelo.ToString();
                        RotationActPos.Text = GlobalVar.R_ActPos.ToString();
                        RotationActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 14:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            ClampEnable.Text      = "Enable";
                            ClampEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            ClampEnable.Text      = "Disable";
                            ClampEnable.BackColor = Color.Red;
                        }
                        ClampActVel.Text = GlobalVar.R_ActVelo.ToString();
                        ClampActPos.Text = GlobalVar.R_ActPos.ToString();
                        ClampActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;

                    case 8:
                        if (GlobalVar.R_Enable != 0)            //已经使能
                        {
                            WaistEnable.Text      = "Enable";
                            WaistEnable.BackColor = Color.Chartreuse;
                        }
                        else
                        {
                            WaistEnable.Text      = "Disable";
                            WaistEnable.BackColor = Color.Red;
                        }
                        WaistActVel.Text = GlobalVar.R_ActVelo.ToString();
                        WaistActPos.Text = GlobalVar.R_ActPos.ToString();
                        WaistActCur.Text = GlobalVar.R_ActCur.ToString();
                        break;
                    }
                    indexStatus++;
                    if (indexStatus == 15)
                    {
                        indexStatus = 0;
                    }
                }
                catch
                {
                }
            }
        }
        static public tcFunctionResult tcSetAxisCommand(int index, Axis_HmiToPlc COMMAND)
        {
            if (!tcClient.IsConnected)
            {
                return(tcFunctionResult.TC_NOT_CONNECTED);
            }
            if (Axis_MaxAxes <= 0)
            {
                return(tcFunctionResult.TC_NO_AXIS);
            }
            if (index < 0 || index >= Axis_MaxAxes)
            {
                return(tcFunctionResult.TC_AXIS_OUTOFBOUND);
            }

            tcAxCommand[index].CONTROLLER_OVERRIDE = COMMAND.CONTROLLER_OVERRIDE;
            tcAxCommand[index].SERVO_HOME          = COMMAND.SERVO_HOME;
            tcAxCommand[index].SERVO_JOG_BW_FAST   = COMMAND.SERVO_JOG_BW_FAST;
            tcAxCommand[index].SERVO_JOG_BW_SLOW   = COMMAND.SERVO_JOG_BW_SLOW;
            tcAxCommand[index].SERVO_JOG_FW_FAST   = COMMAND.SERVO_JOG_FW_FAST;
            tcAxCommand[index].SERVO_JOG_FW_SLOW   = COMMAND.SERVO_JOG_FW_SLOW;
            tcAxCommand[index].SERVO_JOG_MODE      = COMMAND.SERVO_JOG_MODE;
            tcAxCommand[index].SERVO_MOVE_ABS      = COMMAND.SERVO_MOVE_ABS;
            tcAxCommand[index].SERVO_MOVE_REL      = COMMAND.SERVO_MOVE_REL;
            tcAxCommand[index].SERVO_HALT          = COMMAND.SERVO_HALT;
            tcAxCommand[index].SERVO_OFF           = COMMAND.SERVO_OFF;
            tcAxCommand[index].SERVO_ON            = COMMAND.SERVO_ON;
            tcAxCommand[index].SERVO_ON_BW         = COMMAND.SERVO_ON_BW;
            tcAxCommand[index].SERVO_ON_FW         = COMMAND.SERVO_ON_FW;
            tcAxCommand[index].SERVO_RESET         = COMMAND.SERVO_RESET;
            tcAxCommand[index].TARGET_ACCELERATION = COMMAND.TARGET_ACCELERATION;
            tcAxCommand[index].TARGET_DECELERATION = COMMAND.TARGET_DECELERATION;
            tcAxCommand[index].TARGET_JERK         = COMMAND.TARGET_JERK;
            tcAxCommand[index].TARGET_POSITION     = COMMAND.TARGET_POSITION;
            tcAxCommand[index].TARGET_VELOCITY     = COMMAND.TARGET_VELOCITY;

            AdsStream       _dataStream = new AdsStream(tcAxCommand[index].size);
            AdsBinaryWriter _dataWriter = new AdsBinaryWriter(_dataStream);

            _dataWriter.Write(tcAxCommand[index].TARGET_POSITION);
            _dataWriter.Write(tcAxCommand[index].TARGET_VELOCITY);
            _dataWriter.Write(tcAxCommand[index].TARGET_ACCELERATION);
            _dataWriter.Write(tcAxCommand[index].TARGET_DECELERATION);
            _dataWriter.Write(tcAxCommand[index].TARGET_JERK);
            _dataWriter.Write(tcAxCommand[index].CONTROLLER_OVERRIDE);
            _dataWriter.Write(tcAxCommand[index].SERVO_ON);
            _dataWriter.Write(tcAxCommand[index].SERVO_ON_BW);
            _dataWriter.Write(tcAxCommand[index].SERVO_ON_FW);
            _dataWriter.Write(tcAxCommand[index].SERVO_OFF);
            _dataWriter.Write(tcAxCommand[index].SERVO_MOVE_ABS);
            _dataWriter.Write(tcAxCommand[index].SERVO_MOVE_REL);
            _dataWriter.Write(tcAxCommand[index].SERVO_HALT);
            _dataWriter.Write(tcAxCommand[index].SERVO_HOME);
            _dataWriter.Write(tcAxCommand[index].SERVO_RESET);
            _dataWriter.Write(tcAxCommand[index].SERVO_JOG_MODE);
            _dataWriter.Write(tcAxCommand[index].SERVO_JOG_FW_FAST);
            _dataWriter.Write(tcAxCommand[index].SERVO_JOG_BW_FAST);
            _dataWriter.Write(tcAxCommand[index].SERVO_JOG_FW_SLOW);
            _dataWriter.Write(tcAxCommand[index].SERVO_JOG_BW_SLOW);
            try
            {
                tcClient.Write(tcAxCommand[index].handle, _dataStream);
            }
            catch (Exception ex)
            {
                LogMessage(string.Format("{0}\t: {1}", "Error", "Failed to send Axis Command " + ex.Message));
                _dataWriter.Close();
                return(tcFunctionResult.TC_GENERAL_FAILURE_1);
            }
            _dataWriter.Close();
            return(tcFunctionResult.TC_SUCCESS);
        }
        private bool WriteToStream(AdsStream dataStream, Tag tag, object value)
        {
            var binaryWriter = new AdsBinaryWriter(dataStream);

            switch (tag.DataType)
            {
            case IEC61131_3_DataTypes.Boolean:
                if (value is string)
                {
                    value = Boolean.Parse((string)value);
                }

                binaryWriter.Write((bool)value);
                break;

            case IEC61131_3_DataTypes.Byte:
                binaryWriter.Write((byte)value);
                break;

            case IEC61131_3_DataTypes.SInt:
                binaryWriter.Write(Convert.ToSByte(value));
                break;

            case IEC61131_3_DataTypes.USInt:
                binaryWriter.Write(Convert.ToByte(value));
                break;

            case IEC61131_3_DataTypes.UInt:
                binaryWriter.Write(Convert.ToUInt16(value));
                break;

            case IEC61131_3_DataTypes.Int:
                binaryWriter.Write(Convert.ToInt16(value));
                break;

            case IEC61131_3_DataTypes.DInt:
                binaryWriter.Write((int)value);
                break;

            case IEC61131_3_DataTypes.LInt:
                binaryWriter.Write(Convert.ToInt64(value));
                break;

            case IEC61131_3_DataTypes.Real:
                binaryWriter.Write((float)value);
                break;

            case IEC61131_3_DataTypes.LReal:
                binaryWriter.Write((double)value);
                break;

            case IEC61131_3_DataTypes.Word:
                binaryWriter.Write(Convert.ToUInt16(value));
                break;

            case IEC61131_3_DataTypes.DWord:
                binaryWriter.Write(Convert.ToUInt32(value));
                break;

            case IEC61131_3_DataTypes.LWord:
                binaryWriter.Write(Convert.ToUInt64(value));
                break;

            case IEC61131_3_DataTypes.ULInt:
                binaryWriter.Write(Convert.ToUInt64(value));
                break;

            case IEC61131_3_DataTypes.UDInt:
                binaryWriter.Write(Convert.ToUInt32(value));
                break;

            default:
                // handle STRINT DataType
                if (StructuredTextSyntaxRegexHelper.StringDataTypeRegex.IsMatch(tag.DataType))
                {
                    var tmpString = (string)value;
                    var length    = tag.BitSize / 8;

                    if (tmpString.Length > length)
                    {
                        throw new TagException("string is exceeds max defined lenght of " + length, tag);
                    }

                    binaryWriter.WritePlcString((string)value, (int)dataStream.Length);
                    break;
                }

                if (BeckhoffHelper.IsEnum(tag.DataType, tag.BitSize))
                {
                    binaryWriter.Write(Convert.ToInt16(value));
                    break;
                }

                switch (tag.BitSize)
                {
                case 1:
                    if (value is string)
                    {
                        value = Boolean.Parse((string)value);
                    }

                    binaryWriter.Write((bool)value);
                    break;

                case 8:
                    binaryWriter.Write(Convert.ToByte(value));
                    break;

                case 16:
                    binaryWriter.Write(Convert.ToUInt16(value));
                    break;

                case 32:
                    binaryWriter.Write(Convert.ToUInt32(value));
                    break;

                case 64:
                    binaryWriter.Write(Convert.ToUInt64(value));
                    break;

                default:
                    return(false);
                }
                break;
            }

            return(true);
        }
 /// <summary>
 /// Internal util function that maps arbitrary object write actions to the corresponding AdsBinaryWriter write function
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="value"></param>
 private void WriteObjectToWriter(AdsBinaryWriter writer, string typeName, object value)
 {
     if (typeName == "bool")
     {
         writer.Write(bool.Parse(value.ToString()));
     }
     else if (typeName == "byte")
     {
         writer.Write((byte)value);
     }
     else if (typeName == "sint")
     {
         writer.Write(Int16.Parse(value.ToString()));
     }
     else if (typeName == "int")
     {
         writer.Write(Int32.Parse(value.ToString()));
     }
     else if (typeName == "dint")
     {
         writer.Write(Int64.Parse(value.ToString()));
     }
     else if (typeName == "usint")
     {
         writer.Write(UInt16.Parse(value.ToString()));
     }
     else if (typeName == "uint")
     {
         writer.Write(UInt32.Parse(value.ToString()));
     }
     else if (typeName == "udint")
     {
         writer.Write(UInt64.Parse(value.ToString()));
     }
     else if (typeName == "real")
     {
         writer.Write(float.Parse(value.ToString(), CultureInfo.InvariantCulture.NumberFormat));
     }
     else if (typeName == "lreal")
     {
         writer.Write(Double.Parse(value.ToString(), CultureInfo.InvariantCulture.NumberFormat));
     }
     else if (typeName == "time")
     {
         writer.WritePlcType(TimeSpan.Parse(value.ToString()));
     }
     else if (typeName == "date")
     {
         writer.WritePlcType(DateTime.Parse(value.ToString()));
     }
     else if (typeName.StartsWith("string"))
     {
         int length = typeName == "string" ? 80 : int.Parse(typeName.Substring(6));
         writer.WritePlcAnsiString(value.ToString(), length);
     }
     else
     {
         Send_TcClient_EventHandling(DateTime.Now, LogTextCategory.Error,
                                     string.Format("Can not write to AdsBinaryReader. Data type '{0}' not supported", typeName));
     }
 }
Exemple #21
0
        internal void WritePrimitiveValue(string symbolPath, object value, Type managedType, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (byteSize < 0)
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type type  = value.GetType();
            bool flag2 = PrimitiveTypeConverter.IsPlcOpenType(value.GetType());

            if (PrimitiveTypeConverter.IsPlcOpenType(managedType))
            {
                Type tp = null;
                if (PrimitiveTypeConverter.TryGetManagedType(dataType, out tp))
                {
                    managedType = tp;
                }
                else if (managedType == typeof(LTIME))
                {
                    managedType = typeof(ulong);
                    dataType    = AdsDatatypeId.ADST_UINT64;
                }
            }
            if (managedType != type)
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, managedType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{managedType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT16:
                writer.Write((short)value);
                return;

            case AdsDatatypeId.ADST_INT32:
                writer.Write((int)value);
                return;

            case AdsDatatypeId.ADST_REAL32:
                writer.Write((float)value);
                return;

            case AdsDatatypeId.ADST_REAL64:
                writer.Write((double)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT8:
                writer.Write((sbyte)value);
                return;

            case AdsDatatypeId.ADST_UINT8:
                writer.Write((byte)value);
                return;

            case AdsDatatypeId.ADST_UINT16:
                writer.Write((ushort)value);
                return;

            case AdsDatatypeId.ADST_UINT32:
                writer.Write((uint)value);
                return;

            case AdsDatatypeId.ADST_INT64:
                writer.Write((long)value);
                return;

            case AdsDatatypeId.ADST_UINT64:
                writer.Write((ulong)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_STRING:
            {
                int  byteCount = Encoding.Default.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcAnsiString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_WSTRING:
            {
                int  byteCount = Encoding.Unicode.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcUnicodeString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_BIT:
                if ((bool)value)
                {
                    writer.Write((byte)1);
                    return;
                }
                writer.Write((byte)0);
                return;
            }
            throw new ArgumentException("Unexpected datatype. Cannot convert datatype of symbol to this type.", "type");
        }