简体   繁体   English

如何在C#中复制Tera Term SerialPort命令?

[英]How to replicate Tera Term SerialPort command in C#?

In Tera Term I'm connecting to a serial device over USB (on startup select radiobutton "serial" and correct port). 在Tera Term中,我通过USB连接到串行设备(启动时选择单选按钮“ serial”和正确的端口)。 Once connected I only change the default speed to 115200 (in setup=> serial port). 连接后,我仅将默认速度更改为115200(在设置中=>串行端口)。

After this, tera term asks me to fill in commands like so: 之后,tera term要求我填写如下命令:

Command> 命令>

I fill in the device specific command. 我填写设备特定的命令。 In this case it's "PC" and I receive an expected response ie. 在这种情况下,它是“ PC”,我收到一个预期的响应,即。 "ABC" “ ABC”


Now I'm trying to do the same in C#. 现在,我尝试在C#中执行相同的操作。 Unfortunately the response I get is always the same as the command I actually type in. 不幸的是,我得到的响应始终与我实际键入的命令相同。

So If I type in "PC", the response is "PC", but I expect "ABC". 因此,如果我输入“ PC”,则响应为“ PC”,但我希望输入“ ABC”。 Other commands have the same problem. 其他命令有相同的问题。 Command "?" 命令“?” responds with "?" 回答“?” while I expect "CBA". 而我期待“ CBA”。

If I type in a faulty command => then I get the message "Unknown command" So I suspect the device actually gets the right command. 如果我输入了错误的命令=>,那么我会收到消息“未知命令”,因此我怀疑设备实际上得到了正确的命令。

I'm using the following code: 我正在使用以下代码:

        SerialPort COMport = new SerialPort(Port_Name, Baud_Rate); //Create a new  SerialPort Object (defaullt setting -> 8N1)
        COMport.DataReceived += new SerialDataReceivedEventHandler(sPort_dataReceived);
        COMport.ErrorReceived += new SerialErrorReceivedEventHandler(sPort_ErrorReceived);


        COMport.BaudRate = 115200;
        COMport.Parity = Parity.None;
        COMport.DataBits = 8;
        COMport.StopBits = StopBits.One;
        COMport.RtsEnable = true;
        COMport.Handshake = Handshake.None;


        COMport.Open();


        COMport.WriteLine(Data);

        Thread.Sleep(1000); // Just discovered after a lot of testing that this is necessary to read the response before the Comport closes

        COMport.Close(); 

Then I do the following: 然后,我执行以下操作:

    private void sPort_dataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);

        MessageBox.Show(indata);

    }

I've tried different things, but I can't get this to work. 我尝试了不同的方法,但是无法正常工作。 Hopefully it's because I'm new to this. 希望这是因为我是新来的。 I've never worked with Tera term before. 我以前从未使用过Tera术语。

Thanks in advance, 提前致谢,

Some (but definately not all) things I've tried: 我尝试过的一些(但不是全部)事情:

tried this guys advise and code: https://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport 尝试过这些家伙的建议和代码: https : //www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport

downloaded and tried from here: https://www.xanthium.in/building-opensource-gui-based-serial-port-communication-program-dot-net-framework-and-arduino#simple-serial-source-code (Although my device is not arduino) 从这里下载并尝试: https : //www.xanthium.in/building-opensource-gui-based-serial-port-communication-program-dot-net-framework-and-arduino#simple-serial-source-code (虽然我的设备不是arduino)

Tried to add "\\r\\n" : C# Errors with SerialPort WriteLine commands 试图添加“ \\ r \\ n”: SerialPort WriteLine命令的C#错误


EDIT EDIT EDIT EDIT 编辑编辑编辑

So I found out more. 所以我发现了更多。 If I use the following code (Write instead of WriteLine), I do get good results but not every time: 如果我使用以下代码(用Write代替WriteLine),则可以得到不错的结果,但并非每次都可以:

Sending the full command now: "Command>PC" 立即发送完整命令:“命令> PC”

        string Command1 = txtCommand.Text;
        Command1 = Command1 + "\r\n";
        string CommandSent;
        int Length, j = 0;

        Length = Command1.Length;

        for (int i = 0; i < Length; i++)
        {
            CommandSent = Command1.Substring(j, 1);
            ComPort.Write(CommandSent);
            j++;
        }

The first time, now I get good results. 第一次,现在我得到了很好的结果。 The second time I get "Unknow Command", the 3rd time => good results, 4th = "Unknown Command"... etc... It always seems to work 1 time well, then 1 time not. 第二次我收到“未知命令”,第三次=>好的结果,第四次=“未知命令” ...等等...它似乎总是工作1次,然后工作1次。

I can only get it to work consistently if I switch the command formatting: 如果切换命令格式,我只能使其始终如一地工作:

First time command: "Command>PC" 第一次命令:“命令> PC”

Second time command: "PC" 第二次命令:“ PC”

Third time command : "Command>PC" 第三次命令:“命令> PC”

Fourth time command: "PC" 第四次命令:“ PC”

etc... 等等...

I've already tried to clear the buffer before sending but no effect. 我已经尝试在发送之前清除缓冲区,但没有效果。

        ComPort.DiscardInBuffer();
        ComPort.DiscardOutBuffer();

The Newline seemed to be a problem. 换行符似乎是个问题。

I needed to use Comport.Write (instead of WriteLine). 我需要使用Comport.Write(而不是WriteLine)。 Then Also I needed to append a carriage return "\\r" but NO newline '\\n' as I previously thought. 然后我还需要附加一个回车符“ \\ r”,但是没有像我以前想的那样换行符“ \\ n”。 (the incoming data showed a newline after the "Command>" making it impossible to send another meaningfull command => the cause of this was '\\n' => removing it solved the problem) (输入的数据在“ Command>”之后显示换行符,使得无法发送另一个有意义的命令=>原因是'\\ n'=>删除它可以解决问题)

This is my current code that seems to work (I no longer need to append "Command>", just sending the command as is): 这是我当前的似乎有效的代码(我不再需要附加“ Command>”,只需按原样发送命令即可):

        if (thecommand == "")
        {
            ComPort.Write("\r"); //start from a clean slate
            return;
        }

        ComPort.DiscardInBuffer();
        ComPort.DiscardOutBuffer();

        string Command1 = thecommand + "\r";

        ComPort.Write(Command1);

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

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