Reputation: 340
I have made a tiny program, witch can send and receive over a serial port. My problem is that when I send commands ex: "netcfg" I get this echo:
At the spaces it displays the UTF8-code 08. If you convert to java script it displays: \b But the output just have to be: netcfg, not: n ne net netc netcf netcfg
so the output i want loks like this: "sds://>netcfg" and then newline.
I can see that the device understands my command, but this doesn't look nice, so please help me.
My code is messy right now but here it is anyway:
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);
}
}
}
Upvotes: 0
Views: 2781
Reputation: 6444
The ASCII character 8 is a backspace character. It appears that the program on the other end of your serial cable is trying to erase characters entered at its prompt before reprinting the came thing, with your new character added to the end.
Change your program to detect when ASCII 8 is received, and then delete the last character and move the cursor position back one. Might be tricky if you backspace through a new-line... I would ignore it if you're already at the left edge.
You might see other things too. There is a BEL character (7?) that should cause the terminal to beep. There are other characters for other things...
It's possible too, if you start seeing other weird things, that this device expects the user (you) to handle VT100 or ANSI terminal commands. You'll probably see things like ^[12n
if that's the case (replace the ^[
with a little square with a lot of fonts in Windows. It's the escape character.
Ah, the good ol' days of BBSes.
Upvotes: 1