-1

我有一个带有 2 个按钮(BIT 和数据)的图形用户界面。当我按下 BIT 按钮时,它会将十六进制数据发送到连接设备的端口。设备使用另一个十六进制重放。我用 port.DataReceived 函数读取了这个十六进制,将它显示在一个文本框中,并检查每个字节的每一位,并在文本框中显示值的位。这个想法工作正常。当我按下数据按钮时,我将不同的十六进制发送到同一设备,并以不同的十六进制重播。现在我只想显示回放而不检查它。我不知道怎么做,因为当按下数据按钮发送十六进制时,接收来自设备的响应并检查每一位。我不想这样做,我只想以十六进制显示它。如果有人可以帮助我,我会在这里发布我的代码。

我尝试制作两种不同的功能来从设备读取重播,但我认为这种方式不好。我阅读了关于标志的相同内容,我希望这是解决我的问题的更好方法,我想寻求帮助。

我也会在这里发布我的代码。

4

2 回答 2

0

为什么不分离出 readFromDevice() 代码,并将接收到的值传递回调用方法?这样,您可以根据需要以多种不同的方式处理返回的值。

编辑:

我猜这个设计会异步回复(即它会在你发送数据后一段时间回复)?如果是这种情况,您将需要向 com 端口添加一个事件处理程序,然后处理您收到的数据。

您还需要跟踪单击了哪个按钮。您可以为此使用一个简单的布尔值:

//In the data buttons click handler
DataButtonClicked = true;
BitButtonClicked = false;


//add an event handler to the port
String RecievedData;
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

//code for the event handler
private static void DataReceivedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    RecievedData = sp.ReadExisting();

    if(DataButtonClicked)
    {
         //display the value one way
    }
    if(BitbuttonClicked)
    {
         //display the value another way
    }
}

我基于我在下面发布的 MSDN 链接。

于 2012-10-05T12:13:52.843 回答
0
namespace read_display_data

{公共部分类Form1:表格{

    SerialPort port = new SerialPort("COM2", 19200, Parity.None, 8, StopBits.One);


    public Form1()
    {
        InitializeComponent();



        port.Open();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceivedBIT);

      // port.ReadTimeout = 500;



    }



    private void BITbutton_Click(object sender, EventArgs e)
    {




            port.Write(new byte[] { 0x10 0x04, 0x06 }, 0, 3);

            byte[] buffer1 = new byte[] {  0x10 0x04, 0x06 };

            LogSent(LogMsgType.Outgoing, DateTime.Now + "  --  " + ByteArrayToHexString(buffer1) + "\n");

            //port.Close();

    }

私人无效Databutton_Click(对象发送者,EventArgs e){

        port.Write(new byte[] { 0x11, 0x43, 0x0D}, 0, 3);


        byte[] buffer1 = new byte[] {0x11, 0x43, 0x0D };

        LogSent(LogMsgType.Outgoing, DateTime.Now + "  --  " + ByteArrayToHexString(buffer1) + "\n");
    }





    private Color[] LogMsgTypeColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };


    public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };








    void port_DataReceivedBIT(object sender, SerialDataReceivedEventArgs e)
    {

            //port.Open();

            this.Invoke(new EventHandler(AddReceiveBIT));

    }


    private void AddReceiveBIT ( object s, EventArgs e)
    {


            Thread.Sleep(100);


            byte[] buffer = new byte[port.BytesToRead];

            port.Read(buffer, 0, buffer.Length);


            LogReceive(LogMsgType.Incoming, DateTime.Now + "  --  " + ByteArrayToHexString(buffer) + "\n");


            Log(LogMsgType.Incoming, checkBit(buffer) + "\n");

        }




    private string checkBit(byte[] buffer)
    {
        StringBuilder message = new StringBuilder();
        var responseByte = buffer[7];
        if ((responseByte & (1 << 0)) == 0)
        {
            message.AppendFormat("rezerved                                                                            ok    ");
        }
        else
        {
            message.AppendFormat("rezerved                                                                          error    ");
        }




        if ((responseByte & (1 << 1)) == 0)
        {
            message.AppendFormat("rezerved                                                                            ok   ");
        }
        else
        {
            message.AppendFormat("rezerved                                                                          error   ");
        }



        if ((responseByte & (1 << 2)) == 0)
        {
            message.AppendFormat("rezerved                                                                            ok  ");
        }
        else
        {
            message.AppendFormat("rezerved                                                                          error  ");
        }



        if ((responseByte & (1 << 3)) == 0)
        {
            message.AppendFormat("rezerved                                                                            ok  ");
        }
        else
        {
            message.AppendFormat("rezerved                                                                          error  ");
        }





        if ((responseByte & (1 << 4)) == 0)
        {
            message.AppendFormat("rezerved                                                                            ok  ");
        }
        else
        {
            message.AppendFormat("rezerved                                                                          error  ");
        }





        if ((responseByte & (1 << 5)) == 0)
        {
            message.AppendFormat("rezerved                                                                            ok  ");
        }
        else
        {
            message.AppendFormat("rezerved                                                                          error  ");
        }




        if ((responseByte & (1 << 6)) == 0)
        {
            message.AppendFormat("Serial Communication                                                        ok  ");
        }
        else
        {
            message.AppendFormat("Serial Communication                                                              error  ");
        }





        if ((responseByte & (1 << 7)) == 0)
        {
            message.AppendFormat("Optical Zoom                                                                     ok ");
        }
        else
        {
            message.AppendFormat("Optical Zoom                                                                     error  ");
        }

        return message.ToString();
    }



    private string ByteArrayToHexString(byte[] PortBuffer)
    {
        StringBuilder sb = new StringBuilder(PortBuffer.Length * 3);
        foreach (byte b in PortBuffer)
            sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
        return sb.ToString().ToUpper();


    }




    private void LogReceive(LogMsgType msgtype, string msg)
    {
        richTextBox1.Invoke(new EventHandler(delegate
        {
            richTextBox1.SelectedText = string.Empty;
            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
            richTextBox1.SelectionColor = LogMsgTypeColor[(int)msgtype];
            richTextBox1.AppendText(msg);
            richTextBox1.ScrollToCaret();


        }
        ));


    }
    private void LogSent(LogMsgType msgtype, string msg)
    {
        textBox2.Invoke(new EventHandler(delegate
        {
            textBox2.SelectedText = string.Empty;
            textBox2.AppendText(msg);
            textBox2.ScrollToCaret();

        }
        ));


    }


    private void Log(LogMsgType msgtype, string msg)
    {
        textBox3.Invoke(new EventHandler(delegate
        {
            textBox3.Clear();
            textBox3.SelectedText = string.Empty;
            textBox3.AppendText(msg);
            textBox3.ScrollToCaret();


        }
        ));


    }


    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }


    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }
于 2012-10-08T11:36:59.327 回答