Example #1
0
        public CSendParam(SendParamFormat format, SendParamMode mode, int delayTime, string data)
        {
            _Format    = format;
            _Mode      = mode;
            _DelayTime = delayTime;
            _Data      = data;

            switch (_Format)
            {
            case SendParamFormat.ASCII:
                _DataBytes = System.Text.ASCIIEncoding.Default.GetBytes(_Data);
                break;

            case SendParamFormat.Hex:

                string   inputText = Regex.Replace(_Data, @"[0-9A-Fa-f]{2}", "$0 ");
                string[] strArray  = inputText.Split(new string[] { ",", " ", "0x", ",0X", ",", "(", ")" }, StringSplitOptions.RemoveEmptyEntries);


                StringBuilder sbOut = new StringBuilder();
                foreach (string s in strArray)
                {
                    sbOut.AppendFormat("{0:X2} ", Convert.ToByte(s, 16));
                }
                _Data = sbOut.ToString().TrimEnd(' ');


                _DataBytes = Array.ConvertAll <string, byte>(strArray, new Converter <string, byte>(HexStringToByte));

                break;

            default:
                break;
            }
        }
Example #2
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);
            }
        }
Example #3
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;
            }
        }
Example #4
0
        public CSendParam(SendParamFormat format, SendParamMode mode, int delayTime, byte[] data, int startIndex, int count)
        {
            _Format    = format;
            _Mode      = mode;
            _DelayTime = delayTime;
            _Data      = string.Empty;
            if (data != null)
            {
                _DataBytes = new byte[count];
                Array.Copy(data, startIndex, _DataBytes, 0, count);

                if (Format == SendParamFormat.Hex)
                {
                    _Data = BitConverter.ToString(_DataBytes).Replace('-', ' ').TrimEnd(new char[] { ' ' });
                }
                else
                {
                    _Data = System.Text.ASCIIEncoding.Default.GetString(_DataBytes);
                }
            }
        }
Example #5
0
        public CSendParam(SendParamFormat format, SendParamMode mode, int delayTime, string data)
        {
            _Format    = format;
            _Mode      = mode;
            _DelayTime = delayTime;
            _Data      = data;

            switch (_Format)
            {
            case SendParamFormat.ASCII:
                _DataBytes = StreamConverter.AsciiStringToArray(Global.Encode, data);
                break;

            case SendParamFormat.Hex:
                _DataBytes = StreamConverter.HexStringToArray(data);
                break;

            default:
                break;
            }
        }
Example #6
0
        public CSendParam(SendParamFormat format, SendParamMode mode, int delayTime, byte[] data, int startIndex, int count)
        {
            _Format    = format;
            _Mode      = mode;
            _DelayTime = delayTime;
            _Data      = string.Empty;
            if (data != null)
            {
                _DataBytes = new byte[count];
                Array.Copy(data, startIndex, _DataBytes, 0, count);

                if (Format == SendParamFormat.Hex)
                {
                    _Data = StreamConverter.ArrayToHexString(data, startIndex, count);
                }
                else
                {
                    _Data = StreamConverter.ArrayToAsciiString(Global.Encode, _DataBytes, startIndex, count);
                }
            }
        }
Example #7
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);
            }
        }
        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;
            }
        }