在C#中发送(STX和ETX) [英] Sending (STX and ETX) in C#

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

问题描述

我发送此查询发送





但问题是这个

i有使用C#代码这个查询

n发送然后没有输出结果接收终端。



PLZ求解



我的代码是





I have send this query send


but problem is this
i have use C# code this query
n send then no output result receive for terminal.

plz solve

my code is


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
        }
    }
}





我的尝试:



发送(STX和ETX)在C#



What I have tried:

sending(STX and ETX) in C#

推荐答案

如果您尝试发送包括ASCII控制代码(如STX和ETX)的数据,那么您真的需要要非常小心你发送的是什么。

当你从C#app发送文本时:

If you are trying to send data including ASCII control codes such as STX and ETX, then you really need to be very careful about what else you are sending.
When you send Text from a C# app:
myserial.Write(richTextBoxSend.Text);

您发送的是Unicode,这是一个更大的字符集,并且字符可以作为两个字节进行传输。如果你的其他终端设备需要STX / ETX,那么很可能它也需要ASCII数据,而Unicode很可能会混淆这个问题。

所以首先转换你的数据,然后将它们全部作为字节值发送而不是字符:

What you send is in Unicode, which is a "bigger" character set, and where the characters can be trasnmitted as two bytes. If your other end equipment is expecting STX / ETX, then it's likely that it also expects ASCII data, and Unicode may well confuse the issue.
So convert your data first, and send it all as byte values instead of character:

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或在发送字符串之前和之后发送这些单个字符。



BTW:

为什么要初始化并打开 transmit 函数内的端口?

我会将打开的调用移动到 Init()函数的末尾,并且只调用一次(例如,当点击提供的打开按钮时)。最后关闭析构函数中的端口。
Just prefix the string to be send with "\x02" and append "\x03" or send these single characters before and after sending the string.

BTW:
Why you are initialising and opening the port inside your transmit function?
I would move the open call to the end of the Init() function and call that only once (e.g. when clicking on a provided Open button). Finally close the port in the destructor.


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


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

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