By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,962 Members | 2,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,962 IT Pros & Developers. It's quick & easy.

Serial Port keeps echoing back what I send

P: n/a
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");
}

Sep 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"vorange" <or*******@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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
Sep 26 '06 #2

P: n/a

Michael C wrote:
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

Sep 26 '06 #3

P: n/a
PS

"vorange" <or*******@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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");
}
Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.