C#与三菱PLC通信详细使用(FX5U工控设备)

上一篇聊到了C#与三菱PLC设备任何通信,这篇详细介绍结合本人项目中的使用。包括32位数据寄存器的读取写入。

1.数据寄存器(如D100),数据寄存器直接使用32位即可

private void btn_ReadDeviceBlock_Click(object sender, EventArgs e)
        {
            int iReturnCode;
            short[] arrDeviceValue = new short[2]; // 表示 2个 寄存器软元件
            byte[] byarrBufferByte = new byte[4]; // 表示 4个 字节的数据
            byte[] byarr;
            try
            {
                String objText = ((Button)sender).Text;
                String[] sText = objText.Split('-');
                String szDevice = sText[0];
                Control col = this.groupBox1.Controls.Find(szDevice + "text", true)[0];
                TextBox textBox = (TextBox)col;
                iReturnCode = axActUtlType1.ReadDeviceBlock2(szDevice, 2, out arrDeviceValue[0]);

                for (int i = 0; i < 2; i++)
                {
                    byarr = BitConverter.GetBytes(arrDeviceValue[i]);
                    byarrBufferByte[i * 2] = byarr[0];
                    byarrBufferByte[i * 2 + 1] = byarr[1];
                }

                textBox.Text = BitConverter.ToInt32(byarrBufferByte, 0).ToString();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, Name,
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //The return code of the method is displayed by the hexadecimal.
            txt_ReturnCode.Text = String.Format("0x{0:x8}", iReturnCode);
        }

        private void btn_WriteDeviceBlock_Click(object sender, EventArgs e)
        {

            int iReturnCode;
            int iNumberOfData = 0;
            byte[] byarrBufferByte;
            short[] arrDeviceValue;
            try
            {
                String objText = ((Button)sender).Text;
                String[] sText = objText.Split('-');
                String szDevice = sText[0];
                Control col = this.groupBox1.Controls.Find(szDevice + "text", true)[0];
                TextBox textBox = (TextBox)col;
                if (!GetIntValue(textBox, out iNumberOfData))
                {
                    //If failed, this process is end.
                    return;
                }
                arrDeviceValue = new short[2];
                byarrBufferByte = BitConverter.GetBytes(iNumberOfData);
                arrDeviceValue[0] = BitConverter.ToInt16(byarrBufferByte, 0);
                arrDeviceValue[1] = BitConverter.ToInt16(byarrBufferByte, 2);
                iReturnCode = axActUtlType1.WriteDeviceBlock2(szDevice, 2, ref arrDeviceValue[0]);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, Name,
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //The return code of the method is displayed by the hexadecimal.
            txt_ReturnCode.Text = String.Format("0x{0:x8}", iReturnCode);
        }
        

2.内部继电器如(M10),内部继电器相当于一个开关0,1

#region "软元件1点的开启关闭设置" 如:M80
        private void btn_SetDeviceType_Click(object sender, EventArgs e)
        {
            int iReturnCode; //Return code
            try
            {
                String objText = ((Button)sender).Text;
                String[] sText = objText.Split('-');
                String szDevice = sText[0];
                String clickType = sText[1];
                if (clickType.Equals("ON"))
                {
                    iReturnCode = axActUtlType1.SetDevice(szDevice, 1);
                }
                else
                {
                    iReturnCode = axActUtlType1.SetDevice(szDevice, 0);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, Name,
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //The return code of the method is displayed by the hexadecimal.
            txt_ReturnCode.Text = String.Format("0x{0:x8}", iReturnCode);
        }
        #endregion

3.监听继电器如(X0),监听要注意如果批量监听2个,就是2点方法的软元件点数要传2。

这个官方有Demo就不贴代码了。

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 要读取ASCII码,我们可以使用Python中的内置函数ord()。ord()函数接受一个字符作为参数,并返回该字符的ASCII码值。 对于字符串"axactutltype",我们可以遍历每个字符,并使用ord()函数将字符转换为ASCII码。例如: ```python string = "axactutltype" for char in string: ascii_code = ord(char) print(char, "的ASCII码是:", ascii_code) ``` 运行上述代码,输出将会是: ``` a 的ASCII码是: 97 x 的ASCII码是: 120 a 的ASCII码是: 97 c 的ASCII码是: 99 t 的ASCII码是: 116 u 的ASCII码是: 117 t 的ASCII码是: 116 l 的ASCII码是: 108 t 的ASCII码是: 116 y 的ASCII码是: 121 p 的ASCII码是: 112 e 的ASCII码是: 101 ``` 这样,我们就能够通过ord()函数读取字符串中每个字符的ASCII码了。 ### 回答2: 要读取ASCII码,可以使用编程语言提供的方法或函数。 对于axactutltype,如果它是一个编程语言中的字符串变量,可以使用字符串处理函数中的相应方法来读取ASCII码。 例如,在Python编程语言中,可以使用内置的ord()函数来获取一个字符的ASCII码。下面是一个示例代码: ```python axactutltype = "A" ascii_code = ord(axactutltype) print("ASCII码为:" + str(ascii_code)) ``` 运行以上代码会输出:ASCII码为:65。因为字母"A"的ASCII码是65。 如果axactutltype是一个用户输入的字符串,也可以通过索引获取字符串中单个字符的ASCII码。 例如,在C编程语言中,可以使用数组和循环来逐个读取字符串中的字符并获取其ASCII码。下面是一个示例代码: ```c #include<stdio.h> int main() { char axactutltype[10] = "ABCD"; int i; for (i = 0; i < strlen(axactutltype); i++) { printf("字符 %c 的ASCII码为 %d\n", axactutltype[i], axactutltype[i]); } return 0; } ``` 运行以上代码会输出: 字符 A 的ASCII码为 65 字符 B 的ASCII码为 66 字符 C 的ASCII码为 67 字符 D 的ASCII码为 68 以上是两个简单的示例,实际上不同的编程语言和平台有不同的方法来读取ASCII码,但基本思路是相同的。通过使用相应的函数、方法或语法,可以将字符转换为ASCII码。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值