金沙官网线上C# SerialPort 读写三菱FX系列PLC

                
                int ReturnValue;
                lock (lockobj)
                {
                    ReturnValue = _ActUtlType.WriteDeviceBlock(AddressArray[k], lSize, m_write_data[k]);
                }

        }

            string[] AddressArray = new string[18] { "D720", "D721", "D722", "D723", "D724", "D725", "D726", "D727", "D728", "D729"
            ,"D730", "D731", "D732", "D733", "D734", "D735", "D736", "D737"};

                    this.txtRead0.Text += res.ToString() + ",";
                }              
                
            }

 public void Data_Write_Value(int[] m_write_data)
        {
            int lSize = 2;

        {

*        }*

           //如果首位为2,则表示数据有效.这里有个问题,在第二次读,第2位才为'2',第三次又是首位为2,需要再测试
            if (data[0]==2)
            {
                string sReceiveData = System.Text.Encoding.ASCII.GetString(data);
                //MessageBox.Show(sReceiveData);
               //解析命令,将读到的字符解析成数字,注意高低位的转换
                for (int i = 1; i < 8; i += 4)
                {
                    string sLow = sReceiveData.Substring(i,2);
                    string sHigh = sReceiveData.Substring(i + 2, 2);
                    //int res = Convert.ToInt32(sHigh)+ Convert.ToInt32(sLow);
                    int res = Convert.ToInt32(sHigh,16) + Convert.ToInt32(sLow,16);

            for (int k = 0; k < m_write_data.Length; k++)
            {

if (com.IsOpen)

    if (ReturnValue != 0)    //返回0,写入成功。
                {
                    throw new Exception("PC Left Write Data Error!");
                }
            }

1:串口初始化 

/// <summary>
        /// 和校验
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string SumCheck(string data)
        {
            int sum = 0;
            for (int i = 0; i < data.Length; i++)
            {
                sum += Asc(data.Substring(i, 1));
            }

4:写入串口的命令字符串的和校验

        }

 private void btnRead_Click(object sender, EventArgs e)
        {
           
            this.txtRead0.Clear();
            string sReadData = "";

{ com.Close();}

private void btnWrite_Click(object sender, EventArgs e)

public static int Asc(string character)
        {
            if (character.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

6:读PLC

 

 

            string res = sum.ToString("X");
            res = res.Substring(res.Length - 2, 2);

2:打开关闭串口

            string[] write = new string[] { "2","2"}; //将准备写入PLC的值
            //将要写入的值转换成16进制数,补齐两个字节,注意高低字节需要交换
            string sWriteData = "";
            for (int i = 0; i < write.Length; i++)
            {
                int val = Convert.ToInt32(write[i].Length>0?write[i]:"0");
                string s = val.ToString("X");
                while (s.Length<4)
                {
                    s = "0" + s;
                }
                sWriteData += s.Substring(2,2)+s.Substring(0,2);
            }

本文由金沙官网线上发布于编程,转载请注明出处:金沙官网线上C# SerialPort 读写三菱FX系列PLC

您可能还会对下面的文章感兴趣: