コード例 #1
0
        public SerialDataTable(ConnectForm connectForm)
        {
            cf = connectForm;
            f  = connectForm.form1;
            try
            {
                _uart = new SerialPort(cf.comboBoxSerialPorts.Text, Int16.Parse(cf.comboBoxBaudRate.Text), cf.Parity, cf.DataBits, cf.StopBits);

                _uart.Open();
                _uart.DataReceived += SerialPort_DataReceived;
                _uart.ReadTimeout   = 50;
                f.AppendLog(cf.comboBoxSerialPorts.Text + " is opened");
                serialBaseDataTable = f.formBaseDataTable;

                this.serialbackgroundWorker1 = new BackgroundWorker();
                this.serialbackgroundWorker1.WorkerSupportsCancellation = true;
                this.serialbackgroundWorker1.DoWork             += new DoWorkEventHandler(this.BackgroundWorker1_DoWork);
                this.serialbackgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.BackgroundWorker1_RunWorkerCompleted);

                logFile_SerialDataTable = f.logFile_Form1;
            }
            catch (Exception e)
            {
                f.AppendLog(e.Message);
                f.AppendLog("Please try connecting other port.");
                throw new Exception();
            }
        }
コード例 #2
0
        public TCPClient(Form1 f, string address, int port)
        {
            this.f                 = f;
            TCPDataTable           = new TCPDataTable(f);
            tcpClientBaseDataTable = f.formBaseDataTable;
            this.backgroundWorker1 = new BackgroundWorker();
            this.backgroundWorker1.WorkerSupportsCancellation = true;
            this.backgroundWorker1.DoWork             += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);

            SetupTCPConnection(address, port);
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: Tejas8118/ModbusCodeTest
        private void Form1_Load(object sender, EventArgs e)
        {
            TrialTime = DateTime.Now;
            //Check Software Activation
            if (!Settings1.Default.Activated)
            {
                if (!Settings1.Default.Checked)
                {
                    Settings1.Default.TrialTime = TrialTime;
                    Settings1.Default.Checked   = true;
                    Settings1.Default.Save();
                    MessageBox.Show("Trial Version valid till " + TrialTime.Add(new TimeSpan(31, 0, 0, 0)).ToString("dd/MM/yyyy"));
                }

                if ((Settings1.Default.TrialTime.Add(new TimeSpan(31, 0, 0, 0)) > DateTime.Now))
                {
                    int daysRemBeforeExpire = Settings1.Default.TrialTime.Add(new TimeSpan(31, 0, 0, 0)).Subtract(DateTime.Now).Days;
                    MessageBox.Show(daysRemBeforeExpire.ToString() + " days Remaining. Activate before it expires.\n\tDemo will run for 10 minutes only.");


                    myTimer.Interval = 10 * 60000;
                    myTimer.Tick    += new EventHandler(myTimer_Tick);
                    myTimer.Start();
                }
                else
                {
                    MessageBox.Show("Trial Expired...");
                    this.Close();
                }
            }
            formBaseDataTable = new BaseDataTable(this, _datatype);

            // Form loadtime setup
            comboBoxFunctionCode.SelectedIndex = 2;
            comboBoxDatatype.SelectedIndex     = 4;
            comboBoxFormat.SelectedIndex       = 1;
            enableComponentsAfterDisconnection();


            //  SetValuesOnSubItems(this.menuStrip1.Items.OfType<ToolStripMenuItem>().ToList());
            //baseDataTable.generateTextBox();
        }
コード例 #4
0
 public TCPDataTable(Form1 form1)
 {
     f = form1;
     tcpBaseDataTable = form1.formBaseDataTable;
 }
コード例 #5
0
        public void setWriteRequestForm(BaseDataTable bs, int writeReqStartAddress, int writeReqTotalRegister, int functionCode, string datatype, dynamic writeRegisterData)
        {
            writeFunctionCode = functionCode;

            WRFBaseDataTable.baseTotalRegister = writeReqTotalRegister;
            WRFBaseDataTable.baseStartAddress  = writeReqStartAddress;
            WRFBaseDataTable.baseDatatype      = datatype;
            WRFBaseDataTable.baseFormatType    = bs.baseFormatType;
            WRFBaseDataTable.f           = bs.f;
            labelWriteStartAddress.Text  = writeReqStartAddress.ToString();
            labelWriteTotalRegister.Text = writeReqTotalRegister.ToString();
            switch (writeFunctionCode)
            {
            case 5:
                this.Text = "Write Single Coil Status";
                break;

            case 6:
                this.Text = "Write Single Holding Register";
                break;

            case 15:
                this.Text = "Write Multiple Coil Status";
                break;

            case 16:
                this.Text = "Write Multiple Holding Register";
                break;
            }

            if (writeFunctionCode == 5)
            {
                WRFBaseDataTable.CoilStatus = new int[writeReqTotalRegister];

                Array.Copy(writeRegisterData, writeReqStartAddress - bs.baseStartAddress, WRFBaseDataTable.CoilStatus, 0, writeReqTotalRegister);
            }
            else if (writeFunctionCode == 6)
            {
                int readPreviousDataIndex = 0;

                if (bs.baseDatatype.Contains("Int16") || bs.baseDatatype.Equals("Hex") || bs.baseDatatype.Equals("Binary"))
                {
                    //WRFBaseDataTable.RegisterData = new byte[2 * writeReqTotalRegister];
                    readPreviousDataIndex = 2 * (writeReqStartAddress - bs.baseStartAddress);//for 2 bytes
                }
                else
                {
                    writeFunctionCode = 16;
                    //WRFBaseDataTable.RegisterData = new byte[2 * writeReqTotalRegister];
                    readPreviousDataIndex = 2 * (writeReqStartAddress - bs.baseStartAddress) - 2;  //for 4 bytes
                }
                WRFBaseDataTable.RegisterData = new byte[2 * writeReqTotalRegister];
                Array.Copy(writeRegisterData, readPreviousDataIndex, WRFBaseDataTable.RegisterData, 0, WRFBaseDataTable.RegisterData.Length);
            }
            else if (writeFunctionCode == 15)
            {
                WRFBaseDataTable.CoilStatus = readMultipleCoilStatus(writeRegisterData);
                //Array.Copy(writeRegisterData, 0, WRFBaseDataTable.RegisterData, 0, writeReqTotalRegister);
            }

            else
            { //function code ==16
                WRFBaseDataTable.RegisterData = writeRegisterData;

                Array.Copy(writeRegisterData, 0, WRFBaseDataTable.RegisterData, 0, writeReqTotalRegister);
            }

            this.generateTextBox();
            this.ActiveControl = buttonSendWriteRequest;
        }