解决方案 »

  1.   

    标准的TCP访问,跟PC之间访问没有区别。
      

  2.   

    1.建立Socket通信 IpAddr Port
    2.读写送信,结果受信。
    具体的还是要看你用的是那家的PLC,通信协议是什么
      

  3.   

    还是得看PLC是谁家的,如果是三菱的话,有免费的dll,引用了以后直接就可以使用,如果是西门子那比较牛逼,只提供收费的dll,如果是欧姆龙,那就得看你和业务员的关系了,不过所有厂家的还都可以用通用的协议OPC,就是写起来麻烦些
      

  4.   


    我这里用的是三菱的MX Component有案例吗?求救啊!
      

  5.   

    http://forums.mrplc.com/index.php?app=downloads&showfile=1000
      

  6.   

    和你的PLC不一样,都Mitsumishi的都是通过Socket给PLC发消息,然后读取返回
    protected void Write(byte[] sendBuffer)
            {
                //TCPクライアントが接続されていない場合は、接続する 
                if (_socket == null || !_socket.Connected)
                {
                    if (_logs != null) _logs.WriteT(GetType().Name, MethodBase.GetCurrentMethod().Name, "Connect");
                    Connect();
                }            _socket.Send(sendBuffer);
            }        /// <summary>
            /// PLCから返信データを受信する
            /// </summary>
            /// <param name="recvBuffer">受信バッファ
            /// 異常のときは、Length=0で返す</param>
            protected void Read(out byte[] recvBuffer)
            {
                    recvBuffer = new byte[0];                //1回目の読出:応答データ長まで
                    byte[] buf = new byte[RES_DATA_LENGTH_READ_COUNT];
                    //int readBytes = _stream.Read(buf, 0, buf.Length);
                    int readBytes = Read(buf, 0, buf.Length);                //// readBytes != resDataLength のときは例外が発生するので、コメントアウト
                    ////if (readBytes == buf.Length)
                    ////{                ushort resDataLength = (ushort)BinaryConvert.ConvByteToShort(buf[IDX_OF_RES_DATA_LENGTH], buf[IDX_OF_RES_DATA_LENGTH + 1]);                //2回目の読出:応答データ長で指定されたバイト数読出
                    if (resDataLength > 0)
                    {
                        readBytes = 0;                    //読出バッファのサイズを全応答データ長にする
                        Array.Resize(ref buf, buf.Length + resDataLength);                    //応答データ長の次のインデックスから受信データを格納
                        ////readBytes = _stream.Read(buf, RES_DATA_LENGTH_READ_COUNT, resDataLength);
                        readBytes = Read(buf, RES_DATA_LENGTH_READ_COUNT, resDataLength);
                        recvBuffer = buf;
                    }
                    else
                    {
                        if (_logs != null) _logs.WriteErr(enmErrCodeFlag.Warning, GetType().Name, MethodBase.GetCurrentMethod().Name, "resDataLength:" + resDataLength.ToString());
                    }        }        /// <summary>
            /// size数になるまで複数回Readする
            /// </summary>
            /// <param name="buffer"></param>
            /// <param name="offset"></param>
            /// <param name="size"></param>
            /// <returns></returns>
            protected int Read(byte[] buffer, int offset, int size)
            {
                int ret = 0;
                int cnt = 0;
                int readOffset = offset;
                int readSize = size;            while (0 < readSize)
                {
                    try
                    {
                        int readcount = _socket.Receive(buffer, readOffset, readSize, SocketFlags.None);
                        if (readcount == 0) break;              //相手側がShoutdown,Closeで0が返ってくることがある。
                        ret += readcount;
                    }
                    catch (Exception exc)
                    {
                        string msg = "Socket Timeout:" + _socket.ReceiveTimeout.ToString() + "[ms], Count:" + cnt.ToString() + ", size:" + size.ToString() + ", readSize:" + readSize.ToString();
                        if (_logs != null) _logs.WriteErr(enmErrCodeFlag.Warning, GetType().Name, MethodBase.GetCurrentMethod().Name, msg, exc.Message);
                        throw exc;
                    }
                    cnt++;
                    readOffset += ret;
                    readSize -= ret;
                }            if (ret != size)
                {
                    string msg = "ret != size NG. Count:" + cnt.ToString() + ", size:" + size.ToString() + ", ret:" + ret.ToString();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        sb.Append(buffer[i].ToString("X2")); // 16進
                    }
                    throw new InvalidOperationException(msg);
                }             return ret;
            }