Ascii串口C# [英] Ascii serial port C#

查看:191
本文介绍了Ascii串口C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发一个配置工具。我是C#的初学者。我正在为我的应用程序使用winforms。

我有一个串行连接的设备,它会根据特定的ascii命令发回响应。直到现在我可以创建并打开端口,以便在列表框中获取默认响应。但我无法用ascii中的串行设备写入。我试着看帖子我怎么能在串口写ascii,但更加混乱。

任何人都可以帮助缓解语法或过程。



示例:其中一个命令是SMS0,它是一个4字节的命令,结尾为0。



任何帮助将不胜感激。



我尝试过:



使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO.Ports;
使用System.IO;

命名空间SerialGW
{
公共部分类Form1:表格
{

//字符串命令;
public Form1()
{
InitializeComponent();
getAvailablrPorts();


}

void getAvailablrPorts()
{

String [] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);

}
private void label1_Click(object sender,EventArgs e)
{

}

private void progressBar1_Click(object sender,EventArgs e)
{

}

private void button3_Click(object sender,EventArgs e)
{
try
{
if(comboBox1.Text ==|| comboBox2.Text ==)
{
textBox2.Text =请选择端口设置​​;
}
else
{

serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);

serialPort1.Open();

progressBar1.Value = 100;
button1.Enabled = true;
button2.Enabled = true;
textBox1.Enabled = true;
button3.Enabled = false;
button4.Enabled = true;

}
}

catch(UnauthorizedAccessException)
{
textBox2.Text =未经授权的访问;
}

}

private void button4_Click(object sender,EventArgs e)
{

serialPort1.Close() ;
progressBar1.Value = 0;

button1.Enabled = false;
button2.Enabled = false;
button4.Enabled = false;
button3.Enabled = true;
textBox1.Enabled = false;
}

private void button1_Click(object sender,EventArgs e)
{

serialPort1.WriteLine(textBox1.Text);

}

private void button2_Click(object sender,EventArgs e)
{
try
{

//textBox2.Text = serialPort1.ReadExisting();
listBox1.Items.Add(serialPort1.ReadExisting());
//textBox2.Text = serialPort1.ReadLine();它在我们使用ReadLines()时挂起


}
catch(TimeoutException)
{
textBox2.Text =Timeout Exception;
}
}

解决方案

使用Encoding类将字符串转换为ASCII字节:编码.GetBytes方法(字符串)(System.Text) [ ^ ]。

我会查询您拥有的任何设备的文档,并确保命令发送正确。即使 SerialPort 默认情况下也写入ASCII,您需要确保正确发送任何必要的控制字符和行结尾。如前一个解决方案中所述,您可以使用编码类来确保编码正确。


I am developing a configuration tool in C#. I am beginner in C#.I am using winforms for my application.
I have a serially connected device which send back respond on specific ascii command. Till now I can create and open port able to get the default response in list box. But I can not able write in serial device in ascii. I tried looking on post " how can I write ascii in serial port" but got more confuse.
Can anyone help in easing the syntax or process.

Example: One of the command is "SMS0" it is a 4 byte command with 0 at end.

Any help will be appreciated.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;

namespace SerialGW
{
    public partial class Form1 : Form
    {
 
        //string command;
        public Form1()
        {
            InitializeComponent();
            getAvailablrPorts();
            

        }
        
        void getAvailablrPorts()
        {

            String[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);

        }
        private void label1_Click(object sender, EventArgs e)
        {
            
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "" || comboBox2.Text == "")
                {
                    textBox2.Text = "Please select port settings";
                }
                else
                {
                   
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);

                    serialPort1.Open();

                    progressBar1.Value = 100;
                    button1.Enabled = true;
                    button2.Enabled = true;
                    textBox1.Enabled = true;
                    button3.Enabled = false;
                    button4.Enabled = true;

                }
            }

            catch(UnauthorizedAccessException)
            {
                textBox2.Text = "Unauthorised access";
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {

            serialPort1.Close();
            progressBar1.Value = 0;

            button1.Enabled = false;
            button2.Enabled = false;
            button4.Enabled = false;
            button3.Enabled = true;
            textBox1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            serialPort1.WriteLine(textBox1.Text);
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
               
                //textBox2.Text = serialPort1.ReadExisting();
                listBox1.Items.Add(serialPort1.ReadExisting());
                //textBox2.Text = serialPort1.ReadLine();     It hangs when we use ReadLines()


            }
            catch (TimeoutException)
            {
                textBox2.Text = "Timeout Exception";
            }
        }

解决方案

Use the Encoding class to convert a string to ASCII bytes: Encoding.GetBytes Method (String) (System.Text)[^].


I'd consult with the documentation for whatever device you have and ensure that the command is sent right. Even if SerialPort does write in ASCII by default, you need to make sure any necessary control characters and line endings are sent correctly. As noted in a previous solution, you can use the Encoding class to ensure your encoding is right.


这篇关于Ascii串口C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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