예제 #1
0
        public bool SendFc3TcpArchive_v2(string host, byte address, ushort start, ushort offset, ref ushort[] values, ref ushort hataKodu)
        {
            #region remote baglanti
            //------------------remote baglanıyor
            //try
            //{

            //    // Create new modbus master and add event functions

            //    if (host != "Kayıt Yok")
            //    {
            //        //MBmaster = new ModbusTCP.Master(txtIP.Text, 502);
            //        Master MBmaster = new Master(host, 502);
            //        //MBmaster.OnResponseData += new Master.ResponseData(MBmaster_OnResponseData);
            //        //MBmaster.OnException += new Master.ExceptionData(MBmaster_OnException);
            //        //cihazIP = host;
            //        //DoGUIStatus("Connected to IP." + ((IPEndPoint)MBmaster.tcpAsyCl.RemoteEndPoint).Address.ToString());
            //    }
            //    else if (host == "Kayıt Yok")
            //    {
            //        return false;
            //        //DoGUIStatus("Problem with the definition of this IP. Revise the Definitions...");
            //    }

            //}
            //catch (Exception ex)
            //{
            //    System.Windows.Forms.MessageBox.Show(ex.Message);
            //    return false;
            //}
            //------------------remote baglanıyor
            // tcpAsyCl.Connect(host, 502);
            #endregion
            //System.Windows.Forms.MessageBox.Show("bağlantı durumu 1" + tcpSynCl.Connected.ToString());
            if (!tcpSynCl.Connected)
            {
                Master MBmaster = new Master(host, 502);
            }

            //System.Windows.Forms.MessageBox.Show("bağlantı durumu 2 " + tcpSynCl.Connected.ToString());

            byte[] write_data = CreateReadHeader(address, start, offset, fctReadHoldingRegister);
            string arsiv_write_data = "";
            for (int i = 0; i < write_data.Length; i++)
            {
                arsiv_write_data = arsiv_write_data + "_" + write_data[i];
            }
            //System.Windows.Forms.MessageBox.Show("bağlantı durumu 21 " +arsiv_write_data );

            try
            {

                //System.Windows.Forms.MessageBox.Show("bağlantı durumu 3 " + tcpSynCl.Connected.ToString());

                tcpSynCl.Send(write_data, 0, write_data.Length, SocketFlags.None);
                //System.Windows.Forms.MessageBox.Show("bağlantı durumu 4 " + tcpSynCl.Connected.ToString());
                int result = tcpSynCl.Receive(tcpSynClBuffer, 0, tcpSynClBuffer.Length, SocketFlags.None);
                //System.Windows.Forms.MessageBox.Show("bağlantı durumu 5 " + result + "__" + tcpSynCl.Connected.ToString());
                CloseTcp();
                //System.Windows.Forms.MessageBox.Show("bağlantı durumu 51 " + result + "__" + tcpSynCl.Connected.ToString());
                #region check response
                byte function = tcpSynClBuffer[7];
                byte[] data;
                //----check response kısmı
                //System.Windows.Forms.MessageBox.Show("bağlantı durumu 52 " + result + "__" + tcpSynCl.Connected.ToString());
                if (result == 0)
                {
                    //System.Windows.Forms.MessageBox.Show(" call exceptiona geldı 53 " + result + "__" + tcpSynCl.Connected.ToString());
                    CallException(address, write_data[7], excExceptionConnectionLost);
                    // System.Windows.Forms.MessageBox.Show("hata 1");
                }
                // ------------------------------------------------------------
                // Response data is slave exception
                //System.Windows.Forms.MessageBox.Show(" function ve excExceptionOffset 54 " + result + "__" + tcpSynCl.Connected.ToString() + "__" + function + "__" + excExceptionOffset);
                //System.Windows.Forms.MessageBox.Show(" function ve excExceptionOffset 54 " + result + "__" + tcpSynCl.Connected.ToString() + "__" + function + "__" + fctWriteSingleCoil + "__" + fctReadWriteMultipleRegister);
                if (function > excExceptionOffset)
                {
                    //System.Windows.Forms.MessageBox.Show("hata 2");
                    function -= excExceptionOffset;
                    //-- sımdılık kaldırdım   20.11.2011CallException(address, function, tcpSynClBuffer[8]);
                    hataKodu = tcpSynClBuffer[8];
                    //   CloseTcp(); //1
                    return false;

                }
                // ------------------------------------------------------------
                // Write response data

                else if ((function >= fctWriteSingleCoil) && (function != fctReadWriteMultipleRegister))
                {
                    //System.Windows.Forms.MessageBox.Show("hata 3");
                    data = new byte[2];
                    Array.Copy(tcpSynClBuffer, 10, data, 0, 2);
                    //  CloseTcp(); //2
                }
                // ------------------------------------------------------------
                // Read response data
                else
                {
                    //System.Windows.Forms.MessageBox.Show("hata 4");
                    data = new byte[tcpSynClBuffer[8]];
                    //  System.Windows.Forms.MessageBox.Show(" function ve excExceptionOffset 54 " + result + "__" + tcpSynCl.Connected.ToString()+"__"+data );
                    Array.Copy(tcpSynClBuffer, 9, data, 0, tcpSynClBuffer[8]);

                }
                //----------------20.11.2011 de kaldırdım  return data;
                //----check response kısmı
                #endregion
                //Evaluate message:
                //System.Windows.Forms.MessageBox.Show(" function ve excExceptionOffset 54 " + result + "__" + tcpSynCl.Connected.ToString() + "__" + hataKodu);
                //System.Windows.Forms.MessageBox.Show(" sımdı hata kodunu kontrol edecekkk  "  + hataKodu);
                if (hataKodu == 0)
                {
                    //System.Windows.Forms.MessageBox.Show(" data.lenght " + data.Length+"   vlaues lenght   "+values.Length);
                    //Return requested register values:
                    //string data_mesaj = "";
                    string values_mesaj = "";
                    for (int i = 0; i < (data.Length) / 2; i++)
                    {
                        //System.Windows.Forms.MessageBox.Show(" i degeri   values i   data2*i+3   data2*i+4 "+i+"   data max   "+(2*i+1)     );
                        values[i] = data[2 * i];
                        values[i] <<= 8;
                        values[i] += data[2 * i + 1];
                        values_mesaj = values_mesaj + "_" + values[i];
                        //data_mesaj = data_mesaj +"_"+ data[2 * i] + "_" + data[2 * i+1];
                        //values[i] = data[2 * i + 3];
                        //values[i] <<= 8;
                        //values[i] += data[2 * i + 4];
                    }

                    // CloseTcp(); //3
                    //System.Windows.Forms.MessageBox.Show(" valuess " + values_mesaj);
                    //System.Windows.Forms.MessageBox.Show(" data " + data_mesaj);
                    // System.Windows.Forms.MessageBox.Show(" simdi true donuyor");
                    return true;
                    // return false;

                }
                else
                {
                    return false;
                    //return true;
                }
                //System.Windows.Forms.MessageBox.Show(" sonm artık " + result + "__" + tcpSynCl.Connected.ToString() + "__hata kodu " + hataKodu);

            }
            catch (SystemException nesne)
            {
                //    System.Windows.Forms.MessageBox.Show(" exceptiona geldi   " + err.Message+"___"+ tcpSynCl.Connected.ToString() + "__hata kodu " + hataKodu);
                CallException(address, write_data[7], excExceptionConnectionLost);
                modbusStatus = nesne.Message;
                //System.Windows.Forms.MessageBox.Show(" simdi ezception false  donuyor");

                //else CallException(id, write_data[7], excExceptionConnectionLost);
                //return null;
                modbusStatus = "No Connection has Been Established....";
                return false;
            }
        }
예제 #2
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            try
             {

                     MBmaster = new Master(host, 502);

                     MBmaster.OnResponseData += new Master.ResponseData(MBmaster_OnResponseData);
                     MBmaster.OnException += new Master.ExceptionData(MBmaster_OnException);

                     DoGUIStatus("Connected to IP." + ((IPEndPoint)MBmaster.tcpAsyCl.RemoteEndPoint).Address.ToString());

                 ArrayList hList = new ArrayList();

                 bool calisti = Connect_v2(host, TcpSlaveId, (ushort)4096, (ushort)18, hList);

                 if (calisti == true)
                 {
                     //MessageBox.Show("calisti tamam " + hList.Count);
                     if (hList.Count == 15)
                     {
                         #region yeniekranaGetir
                         MethodInvoker action = delegate
                         {
                             txtKabinSicaklik.Text = (Convert.ToInt16(hList[0])/10).ToString();
                             txtKabinNem.Text      = (Convert.ToInt16(hList[1]) / 10).ToString();
                             txtPompaHizi1.Text = hList[2].ToString();
                             txtPompaHizi2.Text = hList[3].ToString();
                             txtCalismaModu.Text = hList[4].ToString();
                             txtPompa1HizSeti.Text = hList[5].ToString();
                             txtPompa2HizSeti.Text = hList[6].ToString();
                             txtScadaSaat.Text = hList[7].ToString();
                             txtScadaDakika.Text = hList[8].ToString();
                             txtScadaSaniye.Text = hList[9].ToString();
                             txtScadaGun.Text = hList[10].ToString();
                             //11
                             txtDebi.Text = hList[12].ToString();
                             txtToplamDebi.Text = hList[13].ToString();
                             //TODO  txtPompaDebi.Text = hList[14].ToString();// artý spare eklenecek mý?

                         };
                         txtKabinSicaklik.BeginInvoke(action);
                         #endregion

                     }
                     else
                     {
                         DoGUIStatus("Error in List Count");
                     }
                 }

                 //MessageBox.Show("simdði Connectten cikti ");
                 //string aa = "";
                 //foreach (var item in hList)
                 //{
                 //    aa += "***" + item.ToString();
                 //}
                 //MessageBox.Show("biten aa degeeri " + aa);
             }
             catch (Exception ex)
             {
                 System.Windows.Forms.MessageBox.Show(ex.Message);
             }
        }