C# 代码对串行端口的响应与 putty 不同

问题描述 投票:0回答:1

从过去两天以来我一直在努力解决这个问题。真的很想得到任何指点。

Summery:我正在通过串行连接到系统。我可以用腻子让它做任何我想做的事。对于我发送的任何命令,它都会返回命令,后跟 <00> 表示成功,<21> 表示错误。清除错误的命令是“KL”,因此如果在 Putty 中我发送 KL 并返回 KL<21>,如果我再次发送 KL,我每次都会返回 KL<00>,不会失败。挑战是,当我从 C# 代码发送相同的命令序列时,我总是得到 KL<21>。即使我在 putty 中连接,发送 KL,获取 KL<21>,再次发送 KL 以获取 KL<00> 响应(以便在打开 C# 代码之前清除错误)。如果我断开 Putty 的连接,然后使用 C# 连接,我得到的第一个响应为 KL<00>,所有其他命令(包括 KL)为 <21>。

腻子配置:

enter image description here

腻子回应:

enter image description here

C# 用户界面:

enter image description here

C# 代码:[忽略秤和 Vacc,因为它们到目前为止工作正常]

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

namespace PortApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
            {
                serialPort1.PortName = com.Text;
                serialPort1.BaudRate = int.Parse(baud.Text);
                serialPort1.DataBits = int.Parse(databit.Text);
                // Parity
                if (parity.Text.ToLower() == "n")
                {
                    serialPort1.Parity = System.IO.Ports.Parity.None;
                }
                else if (parity.Text.ToLower() == "o")
                {
                    serialPort1.Parity = System.IO.Ports.Parity.Odd;
                }
                else if (parity.Text.ToLower() == "e")
                {
                    serialPort1.Parity = System.IO.Ports.Parity.Even;
                }
                else if (parity.Text.ToLower() == "m")
                {
                    serialPort1.Parity = System.IO.Ports.Parity.Mark;
                }
                else if (parity.Text.ToLower() == "s")
                {
                    serialPort1.Parity = System.IO.Ports.Parity.Space;
                }
                // StopBits
                if (stopbit.Text.ToLower() == "n")
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.None;
                }
                else if (stopbit.Text.ToLower() == "1")
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.One;
                }
                else if (stopbit.Text.ToLower() == "1.5")
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.OnePointFive;
                }
                else if (stopbit.Text.ToLower() == "2")
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.Two;
                }
                // FlowControl
                if (flowcontrol.Text.ToLower() == "n")
                {
                    serialPort1.Handshake = Handshake.None;
                    serialPort1.RtsEnable = true;
                    serialPort1.DtrEnable = true;
                }
                else if (flowcontrol.Text.ToLower() == "h")
                {
                    serialPort1.Handshake = Handshake.RequestToSend;
                }
                else if (flowcontrol.Text.ToLower() == "xonxoff")
                {
                    serialPort1.Handshake = Handshake.XOnXOff;
                }
                else if (flowcontrol.Text.ToLower() == "hxonxoff")
                {
                    serialPort1.Handshake = Handshake.RequestToSendXOnXOff;
                }


                // connect
                // Set the read/write timeouts
                serialPort1.ReadTimeout = 500;
                serialPort1.WriteTimeout = 1000;

                try
                {
                    serialPort1.Open();
                    textBox2.Text += "\r\n==================";
                    textBox2.Text += "\r\nPort Opened";
                }
                catch
                {
                    Debug.Print(DateTime.Now.ToString("dd/MM/yy HH:mm:ss: ") + "Com port failed to open.");
                }
            }
            else
            {
                textBox2.Text += "\r\n==================";
                textBox2.Text += "\r\nPort already Open";
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
            {
                textBox2.Text += "\r\n==================";
                textBox2.Text += "\r\nPort not Open";
            }
            else
            {
                serialPort1.Write(this.textBox1.Text+"\r\n");

                var data = "";
                var counter = 20;
                while (data == "" && counter > 0)
                {
                    try
                    {
                        counter -= 1;
                        data = serialPort1.ReadLine();
                        Console.Write(".'"+data+"'\n");
                        //Thread.Sleep(1000);
                        textBox2.Text += "\r\n" + textBox1.Text;
                        textBox2.Text += "\r\n: '" + data+"'";
                        textBox1.Text = "";
                    }
                    catch(Exception ex) {
                        Debug.Print(DateTime.Now.ToString("dd/MM/yy HH:mm:ss: ") + "Exception1:::: "+ ex.Message);
                        Console.Write(">X>'" + ex.Message + "'\n");
                    }
                }

                try
                {
                    data = serialPort1.ReadLine();
                    Console.Write(">'" + data + "'\n");
                    textBox2.Text += "\r\n:: '" + data + "'";
                }

                catch (Exception ex)
                {
                    Debug.Print(DateTime.Now.ToString("dd/MM/yy HH:mm:ss: ") + "Exception2:::: " + ex.Message);
                    Console.Write(">XX>'" + ex.Message + "'\n");
                }
            }

            
        }

        private void disconnect_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
            {
                textBox2.Text += "\r\n==================";
                textBox2.Text += "\r\nPort not Open";
            }
            else
            {
                Console.Write("Port Closed\n");
                textBox2.Text += "\r\nPort Closed";
                serialPort1.DiscardInBuffer();
                serialPort1.DiscardOutBuffer();
                serialPort1.Dispose();
                serialPort1.Close();
            }
        }

        private void robot_Click(object sender, EventArgs e)
        {
            com.Text = "COM3";
            baud.Text  = "9600";
            databit.Text = "8";
            parity.Text = "o";
            stopbit.Text = "1";
            flowcontrol.Text = "N";
        }

        private void scale_Click(object sender, EventArgs e)
        {
            com.Text = "COM4";
            baud.Text = "9600";
            databit.Text = "7";
            parity.Text = "o";
            stopbit.Text = "1";
            flowcontrol.Text = "N";

        }

        private void vacc_Click(object sender, EventArgs e)
        {
            com.Text = "COM6";
            baud.Text = "9600";
            databit.Text = "8";
            parity.Text = "n";
            stopbit.Text = "1";
            flowcontrol.Text = "N";

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
            }
        }
    }
}


c# serial-port
1个回答
0
投票

所以,当我几乎用尽所有选择时,我沮丧地喝了杯咖啡休息一下。 回来后,我试图更多地探索腻子起作用的原因。我启用了所有会话日志记录,然后打开日志,就像我之前在记事本++中所做的那样。

这次我做的不同的一件事是,我启用了在记事本++上显示所有字符。

enter image description here

enter image description here

这就是我挣扎的答案..因为我要么正在通过 (换行符)和我的回车符( )或者我使用 port.write"line"() ,它总是最终在最后发送换行,从而导致错误。

我的其他 COM 对此更加宽容 / 换行所以这从来都不是问题。我想每天都是学习吧。

© www.soinside.com 2019 - 2024. All rights reserved.