Serial.Write C# (CSharp) Method

Write() public static method

Send data to the serial port.
public static Write ( string message ) : void
message string
return void
    public static void Write(string message)
    {
        if (checkOpen ())
            s_serial.Write (message);
    }

Usage Example

Exemplo n.º 1
0
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (!serial.IsOpen || textBoxTx.Text == string.Empty)
            {
                return;
            }

            //定义一个变量,记录发送了几个字节
            int n = 0;

            //16进制发送
            if (checkBoxHexSend.Checked)
            {
                //我们不管规则了。如果写错了一些,我们允许的,只用正则得到有效的十六进制数
                MatchCollection mc  = Regex.Matches(textBoxTx.Text, @"(?i)[\da-f]{2}");
                List <byte>     buf = new List <byte>();//填充到这个临时列表中
                //依次添加到列表中
                foreach (Match m in mc)
                {
                    buf.Add(byte.Parse(m.Value, System.Globalization.NumberStyles.HexNumber));
                }

                if (checkBoxNewline.Checked)
                {
                    buf.Add((byte)'\r');
                    buf.Add((byte)'\n');
                }
                //转换列表为数组后发送
                serial.Write(buf.ToArray(), 0, buf.Count);
                //记录发送的字节数
                n = buf.Count;
            }
            else//ascii编码直接发送
            {
                Encoding encoding = serial.Encoding;
                byte[]   gb       = encoding.GetBytes(textBoxTx.Text);

                serial.Write(gb, 0, gb.Length);
                n = gb.Length;

                if (checkBoxNewline.Checked)
                {
                    serial.Write("\r\n");
                    n += 2;
                }
            }
            tx_bytes += n;//累加发送字节数
            UpdateSendCount();
        }
All Usage Examples Of Serial::Write