我做了一个小程序,女巫可以通过串口发送和接收。我的问题是,当我发送命令 ex: "netcfg" 我得到这个回声:
在空格处显示 UTF8 代码 08。如果转换为 java 脚本,则会显示:\b 但输出必须是:netcfg,而不是:n ne net netc netcf netcfg
所以我想要的输出是这样的:“sds://>netcfg”然后换行。
我可以看到设备理解我的命令,但这看起来不太好,所以请帮助我。
我的代码现在很乱,但无论如何:
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private string _TerminalString;
public Form1()
{
InitializeComponent();
if (!mySerialPort.IsOpen)
{
mySerialPort.Open();
txtTerminal.Text = "The port is now open";
}
else
{
txtTerminal.Text = "The port is in use";
}
}
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
_TerminalString = mySerialPort.ReadExisting();
Invoke(new EventHandler(displayText));
}
private void displayText(object o, EventArgs e)
{
byte[] str2 = Encoding.ASCII.GetBytes(txtSend.Text);
var testList = new List<byte>(str2);
testList.Add(0x0D);
byte[] str3 = testList.ToArray();
mySerialPort.Write(str3, 0, str3.Length);
txtTerminal.AppendText(_TerminalString);
}
private void bSend_Click(object sender, EventArgs e)
{
byte[] str2 = Encoding.ASCII.GetBytes(txtSend.Text);
var testList = new List<byte>(str2);
testList.Add(0x0D);
byte[] str3 = testList.ToArray();
mySerialPort.Write(str3, 0, str3.Length);
}
private void bClear_Click(object sender, EventArgs e)
{
txtTerminal.Clear();
txtSend.Clear();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
mySerialPort.Close();
}
private void btnSearchForComPorts_Click(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
cbmComPorts.Items.Add(port);
}
}
private void btnSelectComPort_Click(object sender, EventArgs e)
{
if (mySerialPort.IsOpen)
{
mySerialPort.Close();
}
mySerialPort.PortName = cbmComPorts.Text;
txtTerminal.AppendText(cbmComPorts.Text);
mySerialPort.Open();
}
private void btnNetcfg_Click(object sender, EventArgs e)
{
// netcfg + enter
byte[] str = {0x6E, 0x65, 0x74, 0x63, 0x66, 0x67, 0x0D, 0x0A};
mySerialPort.Write(str, 0, str.Length);
}
}
}