G
Guest
I am developing some firmware that needs to send binary data to a .NET app
via rs232. I have created a test .NET app that uses the SerialPort class to
read the data, however it is erroneously doing a replacement on the value
0xFF to be 0x3F (the default ParityReplace character). I am specifically
setting Parity to None and ParityReplace = 0 to stop this, however it
continues to replace the character.
I believe this might be a bug in the SerialPort class itself, because I can
use DOS or hyperterminal to view the incoming data, and neither appears to
replace the 0xFF with 0x3F (which is '?').
Here is the code I am testing with. Hook up a serial cable from COM1 to
COM2, run the app, press the buttin, and send some 0xFFs from COM1->COM2 to
test. Other characters appear to work fine.
//***************************************
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
private SerialPort _port;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_port == null)
{
_port = new SerialPort("COM2", 19200, Parity.None, 8,
StopBits.One);
_port.ParityReplace = (byte)'\0';
_port.ReadTimeout = 500;
_port.DataReceived += new
SerialDataReceivedEventHandler(_port_DataReceived);
_port.Open();
}
else
{
_port.Close();
_port.Dispose();
_port = null;
}
}
void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new Action<int>(handleData), 0);
}
private void handleData(int dummy)
{
char[] c = new char[_port.BytesToRead];
_port.Read(c, 0, c.Length);
for (int i = 0; i < c.Length; i++)
{
textBox1.Text += ((int)c).ToString("x") + " " + c +
"\r\n";
}
}
}
}
via rs232. I have created a test .NET app that uses the SerialPort class to
read the data, however it is erroneously doing a replacement on the value
0xFF to be 0x3F (the default ParityReplace character). I am specifically
setting Parity to None and ParityReplace = 0 to stop this, however it
continues to replace the character.
I believe this might be a bug in the SerialPort class itself, because I can
use DOS or hyperterminal to view the incoming data, and neither appears to
replace the 0xFF with 0x3F (which is '?').
Here is the code I am testing with. Hook up a serial cable from COM1 to
COM2, run the app, press the buttin, and send some 0xFFs from COM1->COM2 to
test. Other characters appear to work fine.
//***************************************
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
private SerialPort _port;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_port == null)
{
_port = new SerialPort("COM2", 19200, Parity.None, 8,
StopBits.One);
_port.ParityReplace = (byte)'\0';
_port.ReadTimeout = 500;
_port.DataReceived += new
SerialDataReceivedEventHandler(_port_DataReceived);
_port.Open();
}
else
{
_port.Close();
_port.Dispose();
_port = null;
}
}
void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new Action<int>(handleData), 0);
}
private void handleData(int dummy)
{
char[] c = new char[_port.BytesToRead];
_port.Read(c, 0, c.Length);
for (int i = 0; i < c.Length; i++)
{
textBox1.Text += ((int)c).ToString("x") + " " + c +
"\r\n";
}
}
}
}