发送ASCII控件(STX和ETX) [英] Sending ASCII Controls (STX and ETX)

查看:1449
本文介绍了发送ASCII控件(STX和ETX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续...完全是编程新手,但我需要将数据数组发送到一个串口设备,说明< stx> 01P00104 ##< etx>。我可以获得如何正确格式化的帮助吗?





Continuation... Completely new to programming but I need to send the array of data to a serial device stating "<stx>01P00104##<etx>". Can I get help of how to format this correctly?


private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;

            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }
        private const byte stx = 0x02;
        private const byte etx = 0x03;
        private byte[]WrapString(string send)
        {
            int length = send.Length;
            byte[] data = new byte[length + 2];
            data[0] = stx;
            data[length + 1] = etx;
            Array.Copy(System.Text.Encoding.ASCII.GetBytes(send), 0, data, 1, length);
            return data;
        }
        byte data = WrapString("01P00104##");
        private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
                serialPort1.Write(data);
        }

推荐答案

你做错了。将STX和ETX添加为字符,之前获取ASCII。使用(char)STX (char)ETX ,将其添加到仅字符串数据,然后使用 System.Text.Encoding.ASCII.GetBytes 整个字符串,以获取你的字节。



原因很明显,但你自己(我希望)比解释它更容易理解。你不应该混合字节和字符,因为你没有处理1字节的字符串编码。



这将解决问题。



-SA
You are doing it wrong. Add STX and ETX as characters, before getting ASCII. Use (char)STX and (char)ETX, add it to string-only data, and then use System.Text.Encoding.ASCII.GetBytes to the whole string, to get your bytes.

The reason for this is quite obvious, but it would be easier for you to understand it yourself (I hope) than explaining it. You should not mix bytes and characters, as you are not dealing with 1-byte string encoding.

That will solve the problem.

—SA


作为Sergey的解决方案1的补充,以下是如何将事件处理程序链接到你的一个控件事件:



  • 在表单的设计器窗口中,选择你的LinkLabel控件。
  • 在属性窗口(CTRL W,P - 显示它),单击事件按钮。
  • 在事件列表中,搜索LinkClicked事件,然后单击旁边的列
  • 键入事件处理程序的名称:( MyLink_LinkClicked,例如,如果您已将控件命名为MyLink),并在完成后按TAB键。这将创建一个带有您刚刚输入的名称的方法。
  • 在代码窗口中,您现在可以在单击LinkLabel时执行任何操作。
As a complement of solution 1 by Sergey, here is how you can do to link an event-handler to one of your control's events:

  • In the designer window of your form, select your LinkLabel control.
  • In the 'Properties' Window (CTRL W, P - to show it), click on the 'Events' button.
  • In the event list, search for the 'LinkClicked' event, and click on the column next to it to get the focus.
  • Type the name of the event-handler: (MyLink_LinkClicked, for example if you have named your control 'MyLink'), and press TAB when you're done. This will create a method with the name you just entered.
  • In the code window, you can now do whatever you want to when the LinkLabel has been clicked.
private void MyLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
   byte data = WrapString("01P00104##");
} 



请记住解决方案1中给出的答案:将字符前缀/附加到原始发送 string,并在结尾处获取字节数。


Keep in mind the answer given in solution 1, though: prepend/append the chars to the original send string, and get the bytes out of it at the end.


这篇关于发送ASCII控件(STX和ETX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆