Serial Port keeps echoing back what I send

V

vorange

Hello,
I have a problem I have been unable to solve for quite some
time now. I'm using the Serialport class and opening the port and
writing a byte to it. The byte is successfully received by the other
device. The other device then sends back a response.

But what I get back is the same character I sent! e.g. if i send 2, i
get back 2.

What is going on here? Why are things being echoed back. My code is
below. Please someone help me as I am going crazy with this? I even
tried using a thread with the same characteristic lack of success. It
works ok with a VC++ program I have so the mistake must be somewhere in
the c# code below. Can you spot it?

--------------------------

// connect statement. it also sets up the receiver event
handler
public void Connect()
{
port = new SerialPort("COM3", 57600, Parity.None, 8,
StopBits.One);
port.ReadTimeout = 500;
if (!port.IsOpen)
{
port.Open();
port.DataReceived += new
SerialDataReceivedEventHandler(Receiver);
}
}

// eventhandler that activates when a byte is received
public void Receiver(object sender, SerialDataReceivedEventArgs
e)
{
intBytes = port.BytesToRead;
byte[] bytes = new byte[intBytes];
port.Read(bytes, 0, intBytes);
str2 += bytes[0].ToString();
MessageBox.Show(str2);
}

// to send the number 2 to the other device (gets there
successfully)
public void Sender()
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.Write("2");
}
 
M

Michael C

vorange said:
Hello,
I have a problem I have been unable to solve for quite some
time now. I'm using the Serialport class and opening the port and
writing a byte to it. The byte is successfully received by the other
device. The other device then sends back a response.

But what I get back is the same character I sent! e.g. if i send 2, i
get back 2.

It's possible the device is echoing back the chr. Many do this so that you
can see what you type in terminal programs. if you connect to your device
using hyperterminal and push 2 does a 2 appear on the screen (if it does
then it's echoing the chr back).

Michael
 
V

vorange

Michael said:
It's possible the device is echoing back the chr. Many do this so that you
can see what you type in terminal programs. if you connect to your device
using hyperterminal and push 2 does a 2 appear on the screen (if it does
then it's echoing the chr back).


When I do that, nothing happens.

I do know with my C# program that my 2 is received as it reacts to it.
But when I send something back, I don't seem to get it.

At all times, I just receive in the C# program exactly what I sent.

Why is this happening? I know its something to do with the C# program
as everything works when i test it with a VC++ program. I've even
tested it on different computers on a different port.

Please, if anyone has any ideas or knows why this is happening, let me
know. It must be something in the way I'm setting up the port or
something in C#.

Thank you
 
P

PS

vorange said:
Hello,
I have a problem I have been unable to solve for quite some
time now. I'm using the Serialport class and opening the port and
writing a byte to it. The byte is successfully received by the other
device. The other device then sends back a response.

But what I get back is the same character I sent! e.g. if i send 2, i
get back 2.

If the other device is disconnected then what happens?

PS
What is going on here? Why are things being echoed back. My code is
below. Please someone help me as I am going crazy with this? I even
tried using a thread with the same characteristic lack of success. It
works ok with a VC++ program I have so the mistake must be somewhere in
the c# code below. Can you spot it?

--------------------------

// connect statement. it also sets up the receiver event
handler
public void Connect()
{
port = new SerialPort("COM3", 57600, Parity.None, 8,
StopBits.One);
port.ReadTimeout = 500;
if (!port.IsOpen)
{
port.Open();
port.DataReceived += new
SerialDataReceivedEventHandler(Receiver);
}
}

// eventhandler that activates when a byte is received
public void Receiver(object sender, SerialDataReceivedEventArgs
e)
{
intBytes = port.BytesToRead;
byte[] bytes = new byte[intBytes];
port.Read(bytes, 0, intBytes);
str2 += bytes[0].ToString();
MessageBox.Show(str2);
}

// to send the number 2 to the other device (gets there
successfully)
public void Sender()
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.Write("2");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top