简体   繁体   English

如何使用 C# 表单应用程序将十六进制字符发送到串口?

[英]How to send hex character to serial port with C# form app?

I want to send the hex over the COM port.我想通过 COM 端口发送十六进制。 I have prepared code manually, but its for string sending.我已经手动准备了代码,但它用于字符串发送。

This is how application looks like currently:这是应用程序当前的样子:

1

I need to read data from textbox and then send in hex format to COM port.我需要从文本框中读取数据,然后以十六进制格式发送到 COM 端口。
Currently I am handling the button click with the following sending code, however currently it is sending hard coded values, but I need ones taken from text field.目前我正在使用以下发送代码处理按钮单击,但是目前它正在发送硬编码值,但我需要从文本字段中获取的值。

private void btnSendData_Click(object sender, EventArgs e) {
 if (serialPort1.IsOpen) { 
     dataOUT = tBoxDataOut.Text;
     if (sendWith == "WriteLine") { 
        serialPort1.WriteLine(dataOUT);
     } else if (sendWith == "Write") {
        serialPort1.Write(dataOUT);
     }
  }
}

You can read about Standard Numeric Format Strings here .您可以在此处阅读有关标准数字格式字符串的信息。 Read the section "The Hexadecimal ("X") Format Specifier".阅读“十六进制(“X”)格式说明符”部分。

private void btnSendData_Click(object sender, EventArgs e)
{
    if (serialPort1.IsOpen)
    {
        //dataOUT = tBoxDataOut.Text;
        int intVal = Int32.Parse(tBoxDataOut.Text);
        dataOUT = intVal.ToString("X");
        if (sendWith == "WriteLine")
        {
            serialPort1.WriteLine(dataOUT);
        }
        else if (sendWith == "Write")
        {
            serialPort1.Write(dataOUT);
        }
    }
}

I would like to include a simple, working example.我想包括一个简单的工作示例。 I do not have a COM port, but the com0com virtual driver works fine for this demonstration;我没有 COM 端口,但 com0com 虚拟驱动程序在此演示中运行良好; you can download it here .你可以在这里下载。 I also used Putty to display the ports' received data;我还使用了 Putty 来显示端口接收到的数据; you can download it here .你可以在这里下载。

Here's my program:这是我的程序:

public class HexConvert
{
    SerialPort mySerialPort;

    public HexConvert()
    {
        InitPort();
    }

    void InitPort()
    {
        mySerialPort = new SerialPort("COM1");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
    }

    public void SendMessage()
    {
        try
        {
            string s = "15"; // "F" In Hexadecimal
            int x = Int32.Parse(s);
            string s2 = x.ToString("X");

            if(!mySerialPort.IsOpen)
                mySerialPort.Open();

            mySerialPort.WriteLine(s2);

            mySerialPort.Close();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Here's my com0com utility's Configuration (I added the COM1 and COM2 pair; no other actions taken in this emulator):这是我的 com0com 实用程序的配置(我添加了 COM1 和 COM2 对;在此模拟器中未采取其他操作):

在此处输入图像描述

Here is my Putty Configuration (Note that I used COM1 in the program, and COM2 in Putty):这是我的 Putty 配置(注意我在程序中使用了 COM1,在 Putty 中使用了 COM2):

在此处输入图像描述

And, finally, here is the output that my demo app produces:最后,这是我的演示应用程序生成的 output:

在此处输入图像描述

enter image description here在此处输入图像描述

enter image description here i.stack.imgur.com/LfKM5.png在此处输入图像描述i.stack.imgur.com/LfKM5.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM