永发信息网

如何使用C#语言,通过TCP/UDP方式读写三菱PLC???读写是否需要先发送读写的指令???

答案:1  悬赏:70  手机版
解决时间 2021-02-06 08:29
  • 提问者网友:欲劫无渡
  • 2021-02-05 14:12
网上看到一些相关代码,看不懂,求解释?
public static bool PlcRead(int adres, int count, ref string ReadData)
{
if (Sys.PlcComUse == 0)
return true;

PlcReadBusy = true;
string s_adres = (adres + Sys.PlcBaseAddress).ToString("0#####");
string s_count = count.ToString("0###");

string hdr = "500000FF03FF00";
string s_data = "000A04010000D*" + s_adres + s_count;
string n = (s_data.Length.ToString("X")).PadLeft(4, '0');

string dtfram = (hdr + n + s_data);
byte[] msg = Encoding.ASCII.GetBytes(dtfram);
if (Sys.Deb != 0)
{
PlcReadBusy = false;
return true;
}
int byteSend = Sock.Send(msg);
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
string smsg = encoding.GetString(msg);

System.DateTime Stime = DateTime.Now;
TimeSpan Ctime = new TimeSpan(10000000);
do
{
TimeSpan Ptime = (DateTime.Now - Stime);
if (Ptime > Ctime)
{
MessageBox.Show("PLC comm. time out error.");
PlcReadBusy = false;
return false;
}
Application.DoEvents();
} while (Sock.Available == 0);
int byteRec = Sock.Receive(RcvBytes);
代码过长后面........
最佳答案
  • 五星知识达人网友:蓝房子
  • 2021-02-05 15:26
正好我前段时间做过三菱Modbus协议电表监控开发。我只是通过自己的摸索,用C#来做监控。因为我不是专门做硬件开发的,所以可能给你的回答不是很准确,硬件监控是实时的,通过TCP、UDP什么的我还真不知道有没有这一说,我这个电表的协议是Modbus。
读写电表需要发送读写的命令,一般是  “地址 命令 个数 验证”这样的格式,接收的话,一般也是这样的。
具体的东西,其实也不难,只要你掌握了发送和接收,其他的都好说。
我给你贴主要代码。
1、发送,这里是参考了网上别人的代码,可以用,里面的一些方法,你可以找我要 master@buxiaolong.com
byte[] defByte = new byte[6];
//设备号
string str1x_03 = SendDataDevice.ToString(); // "1";
string str1_03 = "";
Boolean Macvalid1_03;
AppCode.MonitorHelper.formatstring(str1x_03, 2, out str1_03, out Macvalid1_03);
byte[] numbyte1_03 = AppCode.MonitorHelper.mysendb(str1_03);
defByte[0] = numbyte1_03[0];
//功能码 - 03
string fun_str1_03 = "03";
byte[] fun_numbyte1_03 = AppCode.MonitorHelper.mysendb(fun_str1_03);
defByte[1] = fun_numbyte1_03[0];
//起始地址
string str2x_03 = AppCode.ClientDefine.dt_sendDataList.Rows[SendDataIndex]["DataSend"].ToString(); //"0311";
AppCode.ClientDefine.SendData = AppCode.ClientDefine.dt_sendDataList.Rows[SendDataIndex]["DataSend"].ToString(); //"0311";
string str2_03 = "";
Boolean addrvalid1_03;
AppCode.MonitorHelper.formatstring(str2x_03, 4, out str2_03, out addrvalid1_03);
byte[] numbyte2_03 = AppCode.MonitorHelper.mysendb(str2_03);
defByte[2] = numbyte2_03[0];
defByte[3] = numbyte2_03[1];
// 数据数量(长度)
string str_num_03 = "1";
string str_num2_03 = Convert.ToString(Convert.ToInt16(str_num_03), 16);
string str3_03 = "";
Boolean value_valid_03;
AppCode.MonitorHelper.formatstring(str_num2_03, 4, out str3_03, out value_valid_03);
                if (value_valid_03)
                {
                    byte[] numbyte3_03 = AppCode.MonitorHelper.mysendb(str3_03);
                    defByte[4] = numbyte3_03[0];
                    defByte[5] = numbyte3_03[1];
                    //计算CRC
                    byte crch = 0;
                    byte crcl = 0;
                    AppCode.MonitorHelper.CalculateCRC(defByte, defByte.Length, out crch, out crcl);
                    // MOVE给新的数组
                    byte[] rebyte = new byte[defByte.Length + 2];
                    for (int i = 0; i < defByte.Length; i++)
                    {
                        rebyte[i] = defByte[i];
                    }
                    rebyte[6] = crcl;
                    rebyte[7] = crch;
                    mainPort.Write(rebyte, 0, rebyte.Length); // 发送 
                    Thread.Sleep(50); //暂停50 ms
                    //
                    
                }其中mainPort是一个全局(整个项目全局)的SerialPort对象
2、接收
void mainPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string errorMsg_d = "";
            try
            {
                Thread.Sleep(40);
                byte[] bs = new byte[mainPort.BytesToRead];
                mainPort.Read(bs, 0, bs.Length);
                errorMsg_d = bs.ToString();
                string str = "";
                for (int i = 0; i < bs.Length; i++)
                {
                    str += string.Format("{0:X2} ", bs[i]);
                    errorMsg_d += str + "|";
                }
                str = str.Replace(" ", "");
                //MessageBox.Show(str);
                //CheckSendData(str); //检测要发送的数据,看是属于哪一个的,就发送给谁
                //Thread.Sleep(200);
            }
            catch (Exception ex)
            {

            }

        }

其中,接收到的数据就是str,是一组16进制的数据,需要转换。
这就是主要的代码,其实差不多就够了。
希望能帮到你。这是思路。


目前我所做的功能是监控11个电表的各个数值,并有报警功能!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯