• 三菱PLC网络MC3E通信—读取或写入——字符串或数字


    1. 写入32位数值

    写入32位的int类型数据,代码如下:

    1. public bool WriteSingle32(string address, int value)//写入32位的int数据
    2. {
    3. try
    4. {
    5. var tempByte = BitConverter.GetBytes(value);
    6. var temp = new short[2];
    7. temp[0] = BitConverter.ToInt16(tempByte, 0);
    8. temp[1] = BitConverter.ToInt16(tempByte, 2);
    9. plcs.WriteBlock16(address, temp);
    10. return true;
    11. }
    12. catch (Exception ex)
    13. {
    14. return false;
    15. }
    16. }

    2.读取写入字符串到三菱PLC—R32位寄存器

    写入字符串到PLC,代码如下:

    1. private void button3_Click(object sender, EventArgs e)
    2. {
    3. WriteMachineName();
    4. }
    5. SvMitsubishiMC3E plcs = new SvMitsubishiMC3E("192.168.1.38:8088");
    6. private void WriteMachineName()
    7. {
    8. string value = textBox3.Text;
    9. string input = "";
    10. if (value.Length==19)
    11. {
    12. input = value;
    13. }
    14. else if(value.Length < 19)
    15. {
    16. input = value.ToString().PadRight(20, ' ');
    17. }
    18. else
    19. {
    20. input = value.Substring(0, 20);
    21. }
    22. int aaa = input.Length;
    23. plcs.Initial();
    24. WriteString("R32210", input);
    25. plcs.Close();
    26. MessageBox.Show("寫入成功!");
    27. }
    28. public bool WriteString(string address, string value)//写入 value的长度为偶数
    29. {
    30. try
    31. {
    32. byte[] tempByte = System.Text.Encoding.ASCII.GetBytes(value);
    33. // var temp = tempByte.Select(x => Convert.ToInt16(x)).ToArray();
    34. var temp = new short[tempByte.Length/2];
    35. for(int i = 0; i < temp.Length; i++)
    36. {
    37. temp[i] = BitConverter.ToInt16(tempByte, 2*i);
    38. }
    39. plcs.WriteBlock16(address, temp);
    40. return true;
    41. }
    42. catch (Exception ex)
    43. {
    44. return false;
    45. }
    46. }

    其中WriteString(string address, string value)value的长度为偶数

    另一种方式读取字符串数据,代码如下:

    1. CsPlcMC PLC = new CsPlcMC();
    2. private void ReadMachineName(string IP, int Port)
    3. {
    4. if (PLC.Connect(IP, Convert.ToInt16(Port.ToString().Trim())))
    5. {
    6. short[] READ = new short[10];
    7. if (PLC.ReadBlocks(CsPlcMC.eAddress.R, 32210, ref READ))//地址
    8. {
    9. byte[] READ1 = new byte[20];
    10. //將數據轉換成machine名稱,
    11. for (int i = 0; i < 20; i = i + 2)
    12. {
    13. //READ1[i] = (byte)READ[i];
    14. string qw = READ[i / 2].ToString("X4");
    15. READ1[i] = Convert.ToByte(qw.Substring(2, 2), 16);
    16. READ1[i + 1] = Convert.ToByte(qw.Substring(0, 2), 16);
    17. }
    18. textBox3.Text = (Convert.ToString(System.Text.Encoding.ASCII.GetString(READ1))).Trim();
    19. PLC.Disconnect();
    20. }
    21. }
    22. }

    写入int数据,代码如下:

    1. int y, mon, d, h, min, s;
    2. private void WriteTime(String IP, int Port)
    3. {
    4. y = DateTime.Now.Year;
    5. mon = DateTime.Now.Month;
    6. d = DateTime.Now.Day;
    7. h = DateTime.Now.Hour;
    8. min = DateTime.Now.Minute;
    9. s = DateTime.Now.Second;
    10. if (PLC.Connect(IP, Convert.ToInt16(Port.ToString().Trim())))
    11. {
  • 相关阅读:
    java垃圾回收基础
    【重识云原生】第六章容器基础6.4.9.4节——Service拓扑感知提示
    【GdiplusTypes.h error C3861 “min“ 找不到标识符】的终极方案
    MATLAB中syms函数使用
    MySQL与ES数据同步之异步调用
    【气动学】基于matlab GUI弹道问题(含初始角度、速度、空气阻力、水平风)【含Matlab源码 2117期】
    Qt 5中文乱码问题
    标准化、逻辑回归、随机梯度参数估计
    mybatis-plus 基本CRUD
    Linux | scp远程拷贝命令
  • 原文地址:https://blog.csdn.net/qq_42711010/article/details/126864895