Esempio n. 1
0
        public void writeRequestWindow_OnDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                dynamic control, data = null;
                if (sender is TextBox)
                {
                    control = (TextBox)sender;
                }
                else
                {
                    control = (LedBulb)sender;
                }

                int startAddress      = int.Parse(control.Name);
                int totalReg          = 1;
                int writeFunctionCode = int.Parse(baseFunctionCode);
                if (baseFunctionCode.Equals("01"))
                {
                    writeFunctionCode = 5;
                    data = CoilStatus;
                }
                else if (baseFunctionCode.Equals("03"))
                {
                    if (baseDatatype.Contains("Int16") || baseDatatype.Equals("Hex") || baseDatatype.Equals("Binary"))
                    {
                        writeFunctionCode = 6;
                    }
                    else
                    {
                        totalReg          = 2;
                        writeFunctionCode = 16;
                        startAddress     -= 1;
                    }
                    data = new byte[2 * totalReg];
                    Array.Copy(RegisterData, (startAddress - baseStartAddress) * 2, data, 0, 2 * totalReg);
                }
                else
                {
                    throw new Exception();
                }


                writeRequestForm             = new WriteRequestForm("");
                writeRequestForm.FormClosed += writeRequestForm_FormClosed;
                writeRequestForm.buttonSendWriteRequest.MouseClick += writeRequestForm_buttonSubmitWrite_Click;
                writeRequestForm.setWriteRequestForm(this, startAddress, totalReg, writeFunctionCode, baseDatatype, data);
                writeRequestForm.ShowDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("Write Request is possible only for Function Code 01 and 03.Change function code", "Wrong Function Code", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Esempio n. 2
0
        public void openWriteRequestWindow()
        {
            int totalRegister = writeMultiReqTotalReg;

            byte[] writeRegData = null, buffer;
            string datatype     = "";

            var backgroundScheduler = TaskScheduler.Default;
            var uiScheduler         = TaskScheduler.FromCurrentSynchronizationContext();

            try
            {
                //=====================================================================================

                writeTask = Task.Factory.StartNew(delegate
                {
                    if (form1.buttonStop.Enabled)
                    {
                        if (form1._connectType.Equals("TCP"))
                        {
                            form1.tcpclient.stopTransfer();
                        }
                        else
                        {
                            form1.serialDataTable.StopTransfer();
                        }
                    }
                    Task.Delay(form1.formBaseDataTable.baseReceiveDelay).Wait();
                    previousFunctionCode = form1.formBaseDataTable.baseFunctionCode;
                }, backgroundScheduler).
                            ContinueWith(delegate
                {
                    writeRegData = getRegisterDataToChange();


                    if (writeRegData == null)
                    {
                        form1.AppendLog("Starting Address or total register is wrong. Please change it and try Again.");
                        return;
                    }

                    if (writeMultiReqFuncCode == 15)
                    {
                        datatype = "LED";
                    }
                    else
                    {
                        form1.Invoke(new MethodInvoker(delegate { datatype = form1.comboBoxDatatype.SelectedItem.ToString(); }));
                    }

                    form1.formBaseDataTable.baseWriteFunctionCode = writeMultiReqFuncCode;

                    writeRequestForm = new WriteRequestForm("");
                    writeRequestForm.buttonSendWriteRequest.Click += writeRequestForm_buttonSubmitWrite_Click;
                    writeRequestForm.setWriteRequestForm(form1.formBaseDataTable, writeMultiReqStartAdd, totalRegister, writeMultiReqFuncCode, datatype, writeRegData);
                    writeRequestForm.ShowDialog();
                }).ContinueWith(delegate {
                    form1.formBaseDataTable.baseFunctionCode = previousFunctionCode;       //setting previous read function code
                    if (form1.buttonStop.Enabled)
                    {
                        if (form1._connectType.Equals("TCP"))
                        {
                            form1.tcpclient.startTransfer();
                        }
                        else
                        {
                            form1.serialDataTable.StartTransfer();
                        }
                    }
                    Task.Delay(500).Wait();
                });
            }
            catch (Exception ae)
            {
                MessageBox.Show("Please change function Code for read request.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }