예제 #1
0
        private void sendBCD(uint address, uint value)
        {
            byte[] dataTx;
            ////////////// data link layer /////////////////////////////
            DataLink_Header DL = new DataLink_Header();

            DL.Control     = new ControlByte(0xC4); // user data (unconfirm);
            DL.Destination = 65535;
            DL.Source      = 1;

            ////////////// transport /////////////////////////////
            Transport_Header TH = new Transport_Header(0xc0); // transport alway 0xc4 for master

            ///////////// Application Header /////////////////////////////
            Application_Header AP = new Application_Header();
            // Application Control
            ApplicationControl AC = new ApplicationControl();

            AC.FIN = true;
            AC.FIR = true;
            AC.SEQ = 0;
            AP.ApplicationControl = AC;
            AP.FunctionCode       = 2; // Write Function = 2

            //////////////////////////// Object ////////////////////////////////
            //----------------------- Object Field---------------------------//
            Object_Field[] OF = new Object_Field[1]; //have 1 object type

            // Object Header //
            Object_Header OH;
            Qualifier     Q;
            ObjectData    OD;

            // Object Header 1 O:60 V:2
            OH                 = new Object_Header();
            OH.ObjectGroup     = 101;                 //Object
            OH.ObjectVariation = 2;                   // Var
            Q                  = new Qualifier(0x17); // Qualifier Code = 0x17 fixed
            OH.Qualifier       = Q;
            OH.Count           = 1;                   // have 1 data object
            OF[0]              = new Object_Field();
            OF[0].ObjectHeader = OH;                  // Object Header
            OD                 = new ObjectData();    //Data

            //Object Data
            OD.ObjData   = new O101V2(value);
            OF[0].Obj    = new ObjectData[1];
            OD.address   = address;
            OF[0].Obj[0] = OD;
            // move to application header class
            AP.Object = OF;

            ////////////////////////// DNP /////////////////////////////
            DNP_Frame dnpFrame = new DNP_Frame(DL, TH, AP);

            dataTx = dnpFrame.ToRawData();
            this.serialPort1.Write(dataTx, 0, dataTx.Length);
        }
예제 #2
0
        private void timer2_Tick(object sender, EventArgs e)
        {
            this.timer2.Enabled = false;
            this.dataRx         = this.inputBuffer.ToArray();
            this.inputBuffer.Clear(); //clear Serial Port Rx Buffer

            //check start bit for DNP
            if ((dataRx[0] != 0x05) || (dataRx[1] != 0x64))
            {
                return;
            }

            //check CRC
            if (CRC.dnpCheckCRC(dataRx) != true) // drop package if dnp check if fail
            {
                return;
            }
            //check length again (useless checking)
            if (dataRx.Length < 10)
            {
                return;
            }

            if (dataRx.Length == 10)
            {
                //check acknowleage here
                DataLink_Header DLHeader      = new DataLink_Header(dataRx);
                int             masterAddress = Convert.ToInt32(this.textBox1.Text.ToString());
                int             rtuAddress    = Convert.ToInt32(this.textBox2.Text.ToString());

                //Acknowleage
                if ((DLHeader.Source == rtuAddress) && (DLHeader.Destination == masterAddress) && (DLHeader.Control.f_code == 0))
                {
                    this.timer1.Stop();
                    System.Threading.Thread.Sleep(200);
                    uploadState = (byte)State.masterAddress;
                    toolStripStatusLabel1.Text = "Connection : Success";

                    MessageBox.Show("Test Connection : Success" + Environment.NewLine
                                    + "  Master Address = " + masterAddress + Environment.NewLine
                                    + "  RTU Address = " + rtuAddress
                                    , "Test Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    toolStripStatusLabel2.Text = "";
                    //this.timer1.Stop();
                    this.serialPort1.Close();
                    this.button1.Enabled            = true;
                    this.button2.Enabled            = true;
                    this.toolStripComboBox1.Enabled = true;
                    this.textBox1.Enabled           = true;
                    this.textBox2.Enabled           = true;
                    this.textBox3.Enabled           = true;
                    this.textBox4.Enabled           = true;
                }

                return;
            }

            DNP_Frame responseDNP = new DNP_Frame(dataRx);


            if (responseDNP.applicationHeader.FunctionCode == 129) //check response function
            {
                //progress bar
                try
                {
                    this.toolStripProgressBar1.Value += 12;
                }
                catch
                {
                    this.toolStripProgressBar1.Value = 100;
                }


                //next state
                if (uploadState == (byte)State.masterAddress)
                {
                    this.timer1.Stop();
                    uint rtuAddress = Convert.ToUInt32(this.textBox2.Text.ToString());
                    uploadState = (byte)State.rtuAddress;
                    //sendBCD
                    System.Threading.Thread.Sleep(200);
                    try
                    {
                        this.sendBCD(2, rtuAddress);
                    }
                    catch { }
                    //start timer
                    this.timer1.Interval = waitResponseInterval;
                    this.timer1.Start();
                    //progress bar
                    toolStripStatusLabel2.Text        = "Writing RTU address";
                    this.toolStripProgressBar1.Value += 13;
                }
                else if (uploadState == (byte)State.rtuAddress)
                {
                    this.timer1.Stop();
                    uint preDelay = Convert.ToUInt32(this.textBox3.Text.ToString());
                    uploadState = (byte)State.preDelay;
                    //sendBCD
                    System.Threading.Thread.Sleep(200);
                    try
                    {
                        this.sendBCD(3, preDelay);
                    }
                    catch { }
                    //start timer
                    this.timer1.Interval = waitResponseInterval;
                    this.timer1.Start();
                    //progress bar
                    toolStripStatusLabel2.Text        = "Writing pre-delay time";
                    this.toolStripProgressBar1.Value += 13;
                }
                else if (uploadState == (byte)State.preDelay)
                {
                    this.timer1.Stop();
                    uint postDelay = Convert.ToUInt32(this.textBox4.Text.ToString());
                    uploadState = (byte)State.postDelay;
                    //sendBCD
                    System.Threading.Thread.Sleep(200);
                    try
                    {
                        this.sendBCD(4, postDelay);
                    }
                    catch { }
                    //start timer
                    this.timer1.Interval = waitResponseInterval;
                    this.timer1.Start();
                    //progress bar
                    toolStripStatusLabel2.Text        = "Writing post-delay time";
                    this.toolStripProgressBar1.Value += 13;
                }
                else if (uploadState == (byte)State.postDelay)
                {
                    this.timer1.Stop();
                    uploadState = (byte)State.masterAddress;
                    toolStripStatusLabel1.Text = "Complete";
                    toolStripStatusLabel2.Text = "";
                    //this.timer1.Stop();
                    this.serialPort1.Close();
                    this.button1.Enabled            = true;
                    this.button2.Enabled            = true;
                    this.toolStripComboBox1.Enabled = true;
                    this.textBox1.Enabled           = true;
                    this.textBox2.Enabled           = true;
                    this.textBox3.Enabled           = true;
                    this.textBox4.Enabled           = true;
                }
                return;
            }
        }