Beispiel #1
0
        private void dgvSendList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0)
            {
                return;
            }

            DataGridViewRow row = dgvSendList.Rows[e.RowIndex];

            if (e.ColumnIndex == 1)   // Send button
            {
                CSendParam sendParam = getSendParamByMode(row.Cells[RowParamIndex].Value.ToString(),
                                                          row.Cells[RowContentIndex].Value.ToString());
                sendParam.Mode      = SendParamMode.SendAfterLastSend;
                sendParam.DelayTime = 0;
                if (ManualSendEvent != null)
                {
                    ManualSendEvent(this, new ManualSendEventArgs(sendParam));
                }
            }
            else if (e.ColumnIndex == RowEnableIndex) // enable
            {
                row.Cells[e.ColumnIndex].Value = !Convert.ToBoolean(row.Cells[e.ColumnIndex].Value);
            }
        }
Beispiel #2
0
        public frmQueueSetting(object[] obj)
            : this()
        {
            if (obj != null && obj.Length >= 5)
            {
                sendObj         = new QueueSendObject();
                sendObj.Index   = Convert.ToInt32(obj[0]);
                sendObj.Title   = Convert.ToString(obj[1]);
                sendObj.Enable  = Convert.ToBoolean(obj[2]);
                sendObj.Mode    = Convert.ToString(obj[3]);
                sendObj.Content = Convert.ToString(obj[4]);

                CSendParam sp = getSendParamByMode(sendObj.Mode, sendObj.Content);

                chkSendHex.Checked         = sp.Format == SendParamFormat.Hex ? true : false;
                cbSendMode.SelectedIndex   = (int)sp.Mode;
                numSendListDelayTime.Value = sp.DelayTime;
                txtTitle.Text = sendObj.Title;

                if (sp.Format == SendParamFormat.Hex)
                {
                    txtSend.Text = sp.HexString;
                }
                else
                {
                    txtSend.Text = sp.ASCIIString;
                }
            }
        }
Beispiel #3
0
        private void lnkAddCheckCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (txtSend.Text != String.Empty)
                {
                    bool IsHex = chkSendHex.Checked;

                    frmDataCheck frm = new frmDataCheck();
                    frm.CalculateCheckData(txtSend.Text, IsHex);
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        if (IsHex)
                        {
                            CSendParam p = new CSendParam(SendParamFormat.Hex, SendParamMode.SendAfterLastSend, 0, frm.CrcResult);
                            txtSend.AppendText(string.Format(" {0}", p.Data));
                        }
                        else
                        {
                            txtSend.AppendText(string.Format("{0}", frm.CrcResult));
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void LoadConfig()
        {
            chkSendHex.Checked       = Properties.Settings.Default.normalHexSend;
            chkFormat.Checked        = Properties.Settings.Default.normalHexFormat;
            chkSendThenClear.Checked = Properties.Settings.Default.normalSendClear;
            chkAutoSend.Checked      = Properties.Settings.Default.normalLoopSend;
            numSendCount.Value       = Properties.Settings.Default.normalLoopCount;
            numSendOnceBytes.Value   = Properties.Settings.Default.normalPacketBytes;
            numSendInterval.Value    = Properties.Settings.Default.normalInterval;
            txtSend.Text             = Properties.Settings.Default.normalContent;

            SendTempIndex = 0;
            if (Properties.Settings.Default.HistorySendList == null)
            {
                Properties.Settings.Default.HistorySendList = new System.Collections.Specialized.StringCollection();
            }
            for (int i = 0; i < Properties.Settings.Default.HistorySendList.Count; i++)
            {
                lock (SendTempList)
                {
                    //SendTempList.Add(Properties.Settings.Default.HistorySendList[i]);
                    CSendParam param = new CSendParam(SendParamFormat.Hex, SendParamMode.SendAfterLastSend, 0, Properties.Settings.Default.HistorySendList[i]);
                    SendTempList.Add(param.ASCIIString);
                    SendTempIndex++;
                    if (SendTempIndex == 100)
                    {
                        SendTempList.RemoveAt(0);
                        SendTempIndex--;
                    }
                }
            }
        }
Beispiel #5
0
        CSendParam getSendParamByMode(string mode, string content)
        {
            string[]   paramsArray = mode.Split(new char[] { ':' });
            CSendParam sendParam   = new CSendParam(
                (SendParamFormat)Convert.ToInt32(paramsArray[0]),
                (SendParamMode)Convert.ToInt32(paramsArray[1]),
                Convert.ToInt32(paramsArray[2]),
                content);

            return(sendParam);
        }
 /// <summary>
 /// 格式化显示
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void chkFormat_CheckedChanged(object sender, EventArgs e)
 {
     try
     {
         CSendParam param = new CSendParam(SendParamFormat.Hex, SendParamMode.SendAfterLastSend, Convert.ToInt32(numSendInterval.Value), txtSend.Text);
         if (chkSendHex.Checked && chkFormat.Checked)
         {
             txtSend.Text = param.Data;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Beispiel #7
0
        private void btnSaveSendParam_Click(object sender, EventArgs e)
        {
            if (txtSend.Text == string.Empty)
            {
                MessageBox.Show("发送数据不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                SendParamFormat format = SendParamFormat.ASCII;
                if (chkSendHex.Checked)
                {
                    format = SendParamFormat.Hex;
                }

                CSendParam param = new CSendParam(format,
                                                  (SendParamMode)cbSendMode.SelectedIndex,
                                                  Convert.ToInt32(numSendListDelayTime.Value),
                                                  txtSend.Text);

                if (sendObj == null)
                {
                    sendObj        = new QueueSendObject();
                    sendObj.Index  = -1;
                    sendObj.Enable = true;
                }

                if (chkTitleAuto.Checked)
                {
                    sendObj.Title = txtSend.Text;
                }
                else
                {
                    sendObj.Title = txtTitle.Text;
                }

                sendObj.Mode    = param.ParameterString;
                sendObj.Content = param.Data;

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #8
0
        public SendCompletedEventArgs(CSendParam sendParam)
        {
            _SendTime  = DateTime.Now;
            _SendParam = sendParam;

            //if (sendParam!=null)
            //{
            //    _SendParam = new CSendParam(sendParam.Format,
            //    sendParam.Mode,
            //    sendParam.DelayTime,
            //    sendParam.DataBytes, 0, sendParam.DataLen);
            //}
            //else
            //{
            //    _SendParam = null;
            //}
        }
Beispiel #9
0
        public void CalculateCheckData(string text, bool IsHex)
        {
            try
            {
                SendParamFormat format = SendParamFormat.ASCII;
                if (IsHex)
                {
                    format = SendParamFormat.Hex;
                }

                CSendParam param = new CSendParam(format, SendParamMode.SendAfterLastSend, 0, text);
                _DataBytes = param.DataBytes;
                CalCheckData();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        public void SaveConfig()
        {
            Properties.Settings.Default.normalHexSend     = chkSendHex.Checked;
            Properties.Settings.Default.normalHexFormat   = chkFormat.Checked;
            Properties.Settings.Default.normalSendClear   = chkSendThenClear.Checked;
            Properties.Settings.Default.normalLoopSend    = chkAutoSend.Checked;
            Properties.Settings.Default.normalLoopCount   = (int)numSendCount.Value;
            Properties.Settings.Default.normalPacketBytes = (int)numSendOnceBytes.Value;
            Properties.Settings.Default.normalInterval    = (int)numSendInterval.Value;
            Properties.Settings.Default.normalContent     = txtSend.Text;

            Properties.Settings.Default.HistorySendList.Clear();
            for (int i = 0; i < SendTempList.Count; i++)
            {
                CSendParam param = new CSendParam(SendParamFormat.ASCII, SendParamMode.SendAfterLastSend, 0, SendTempList[i]);
                Properties.Settings.Default.HistorySendList.Add(param.HexString);
            }

            Properties.Settings.Default.Save();
        }
Beispiel #11
0
        public List <CSendParam> getSendList()
        {
            List <CSendParam> list = new List <CSendParam>();

            foreach (DataGridViewRow row in dgvSendList.Rows)
            {
                if ((bool)row.Cells[RowEnableIndex].Value == true)
                {
                    //string[] paramsArray = row.Cells[3].Value.ToString().Split(new char[] { ':' });
                    //CSendParam sendParam = new CSendParam(
                    //    (SendParamFormat)Convert.ToInt32(paramsArray[0]),
                    //    (SendParamMode)Convert.ToInt32(paramsArray[1]),
                    //    Convert.ToInt32(paramsArray[2]),
                    //    row.Cells[4].Value.ToString());

                    CSendParam sendParam = getSendParamByMode(row.Cells[RowParamIndex].Value.ToString(),
                                                              row.Cells[RowContentIndex].Value.ToString());
                    list.Add(sendParam);
                }
            }

            return(list);
        }
Beispiel #12
0
        /// <summary>
        /// 保存发送参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveSendParam_Click(object sender, EventArgs e)
        {
            if (txtSend.Text == string.Empty)
            {
                MessageBox.Show("发送数据不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                SendParamFormat format = SendParamFormat.ASCII;
                if (chkSendHex.Checked)
                {
                    format = SendParamFormat.Hex;
                }

                CSendParam param = new CSendParam(format,
                                                  (SendParamMode)cbSendMode.SelectedIndex,
                                                  Convert.ToInt32(numSendListDelayTime.Value),
                                                  txtSend.Text);

                object[] array = new object[5];
                array[0] = dgvSendList.Rows.Count;
                array[1] = "Send";
                array[2] = true;
                array[3] = param.ParameterString;
                array[4] = param.Data;
                dgvSendList.Rows.Add(array);

                btnCancelSaveParam.PerformClick();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #13
0
 public ManualSendEventArgs(CSendParam sendParam)
 {
     _SendList = new List <CSendParam>();
     _SendList.Clear();
     _SendList.Add(sendParam);
 }
Beispiel #14
0
        /// <summary>
        /// 发送线程
        /// </summary>
        private void SendThreadHandler()
        {
            bool IsFirstSend = true;

            if (LoopCount == 0)
            {
                LoopCount = int.MaxValue;
            }
            try
            {
                while (LoopCount > 0 && IsSendStart)
                {
                    LoopCount--;

                    waitParseEvent.Reset();

                    int index = 0;
                    while (index < sendList.Count && IsSendStart)
                    {
                        _SendThreadBusy = true;

                        CSendParam sendParam = null;
                        lock (sendList)
                        {
                            sendParam = sendList[index];
                        }
                        index++;

                        if (sendParam != null)
                        {
                            bool DelayEnable = true;
                            if (sendParam.Mode == SendParamMode.SendAfterLastSend)
                            {
                                if (IsFirstSend)
                                {
                                    DelayEnable = false;
                                }
                            }
                            else if (sendParam.Mode == SendParamMode.SendAfterReceived)
                            {
                                waitParseEvent.WaitOne();
                            }
                            IsFirstSend = false;

                            if (DelayEnable && sendParam.DelayTime > 0)
                            {
                                DateTime startTime = DateTime.Now;
                                TimeSpan ts        = DateTime.Now - startTime;
                                while (ts.TotalMilliseconds < sendParam.DelayTime)
                                {
                                    Thread.Sleep(delayTime);
                                    ts = DateTime.Now - startTime;
                                    if (!IsSendStart)
                                    {
                                        break;
                                    }
                                }
                                ;
                            }

                            if (IsSendStart && SerialPortWrite(sendParam.DataBytes, 0, sendParam.DataBytes.Length))
                            {
                                if (SendCompletedEvent != null)
                                {
                                    SendCompletedEventHandler handler = SendCompletedEvent;
                                    handler(this, new SendCompletedEventArgs(sendParam));
                                }
                            }
                            else
                            {
                                IsSendStart = false;
                            }

                            waitParseEvent.Reset();
                        }
                        _SendThreadBusy = false;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                if (SendOverEvent != null)
                {
                    SendOverEvent(this, null);
                }
            }
        }
        public List <CSendParam> GetSendList()
        {
            try
            {
                List <CSendParam> list = new List <CSendParam>();

                if (txtSend.Text.Trim() == string.Empty)
                {
                    return(list);
                }

                SendParamFormat format = SendParamFormat.ASCII;
                if (chkSendHex.Checked)
                {
                    format = SendParamFormat.Hex;
                }

                int        sendInterval = Convert.ToInt32(numSendInterval.Value);
                CSendParam param        = new CSendParam(format, SendParamMode.SendAfterLastSend, sendInterval, txtSend.Text);

                if (chkSendHex.Checked && chkFormat.Checked)
                {
                    txtSend.Text = param.Data;
                }

                int packetLen = Convert.ToInt32(numSendOnceBytes.Value);

                if (packetLen == 0)
                {
                    list.Add(param);
                }
                else
                {
                    byte[] dataBytes = param.DataBytes;

                    int bytesIndex = 0;
                    while (bytesIndex < param.DataLen)
                    {
                        int len = packetLen;
                        if (bytesIndex + packetLen > param.DataLen)
                        {
                            len = param.DataLen - bytesIndex;
                        }

                        int delayTime = sendInterval;

                        //if (bytesIndex==0)
                        //{
                        //    //delayTime = 0;
                        //}
                        CSendParam p = new CSendParam(format, SendParamMode.SendAfterLastSend, delayTime, dataBytes, bytesIndex, len);
                        list.Add(p);

                        bytesIndex += len;
                    }
                }

                updateSendState();

                return(list);
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
        }