在C中发送(STX和ETX)# 您所在的位置:网站首页 prolific官网 在C中发送(STX和ETX)#

在C中发送(STX和ETX)#

2023-04-23 08:58| 来源: 网络整理| 查看: 265

问题描述

我已发送此查询发送

但问题是这个我已经使用 C# 代码这个查询 n 发送然后没有输出结果接收到终端.

请解决

我的代码是

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; //add this,represents a serial port resource namespace Serial_Comunication { public partial class Form1 : Form { SerialPort myserial = new SerialPort(); //create of serial port public Form1() { InitializeComponent(); getportnames(); //load all port names in this computer } public void Init() { if (myserial.IsOpen ) //if port is open { myserial.Close(); //close port } myserial.PortName = comboBoxPort.Text; //selected name of port myserial.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text); //selected parity myserial.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopB.Text);//selected stopbits myserial.DataBits = int.Parse(comboBoxDataB.Text); //selected data bits myserial.BaudRate = int.Parse(comboBoxBaudR.Text); //selected baudrate myserial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);//received even handler } public void getportnames() { string[] portnames = SerialPort.GetPortNames(); //load all names of com ports to string comboBoxPort.Items.Clear(); //delete previous names in combobox items foreach (string s in portnames) //add this names to comboboxPort items { comboBoxPort.Items.Add(s); } if (comboBoxPort.Items.Count > 0) //if there are some com ports ,select first { comboBoxPort.SelectedIndex = 0; } else { comboBoxPort.Text = "No COM Port "; //if there are no com ports ,write No Com Port } } public void transmit() { Init(); //init parameters of serial comunication before every touch button "send" try { myserial.Open(); //open serial port myserial.Write(richTextBoxSend.Text); //write string to serial port from rich text box } catch { MessageBox.Show("com port is not available"); //if there are not is any COM port in PC show message } } public void DataReceivedHandler( object sender,SerialDataReceivedEventArgs e) { string indata= myserial.ReadExisting(); //read data to string Invoke(new Action(() => richTextBoxReceive.Text += indata)); //invoke method use for write receive data to richtextbox } private void buttonSend_Click(object sender, EventArgs e) // send button event { transmit(); //transmit data } private void buttonReload_Click(object sender, EventArgs e)//reload button event ,most useful if you use virtual COM port e.g FTDI,Prolific { getportnames(); //get all port names connected to this computer } } }

我尝试过的:

在 C# 中发送(STX and ETX)

推荐答案如果你要发送的数据包括 STX 和 ETX 等 ASCII 控制码,那么你真的需要非常小心关于您要发送的其他内容.当您从 C# 应用程序发送文本时:myserial.Write(richTextBoxSend.Text);您发送的是 Unicode,这是一个"更大"的字符集,其中的字符可以作为两个字节传输.如果您的另一端设备需要 STX/ETX,那么它很可能也需要 ASCII 数据,而 Unicode 很可能会混淆这个问题.因此,首先转换您的数据,并将其全部作为字节值而不是字符发送:private byte[] STX = new byte[] { 0x02 }; private byte[] EXT = new byte[] { 0x03 }; ... byte[] bytes = System.Text.Encoding.ASCII.GetBytes(richTextBoxSend.Text); myserial.Write(STX, 0 , 1); myserial.Write(bytes, 0, bytes.Length); myserial.Write(ETX, 0, 1);只需在要发送的字符串前加上"\x02"并附加"\x03"或在发送字符串之前和之后发送这些单个字符.

顺便说一句:为什么要在 transmit 函数中初始化和打开端口?我会将 open 调用移到 Init() 函数的末尾并仅调用一次(例如,单击提供的 Open 按钮时).最后在析构函数中关闭端口.

var serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); serialPort.Open(); serialPort.Write(new byte[] {0x02}, 0, 1); // Send STX serialPort.Write(new byte[] {0x03}, 0, 1); // Send ETX

本文地址:https://www.itbaoku.cn/post/1124306.html?view=all



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有