473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Serial Port keeps echoing back what I send

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
3 5029
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Chong | last post by:
I wrote a program that communicate with SerialComm. In every 300 milliseconds, my program continuously send & receive data via the serial port once the program starts. My program is once in a...
6
by: Casey Bralla | last post by:
I'd like to read ASCII data from a serial port, but (once again) I'm having trouble getting started. (Can't seem to find the basic level of docs to get going <sigh>) I'd like to use only...
0
by: Stephan Rose | last post by:
Anyone have any experience working with USB->Serial adapters?? Yes, some of us, like me, still need Serial ports!!! Drives me nuts that it is almost impossible to still get a high end motherboard...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
2
by: joaquimfpinto | last post by:
Dear All, I made an app in c# that uses several serial ports. For the serial ports I use a pnp Sunix board, some with 8 serial ports other with 4 or even 2 serial ports. Whenever I use the...
2
by: colin | last post by:
Hi, Im having a tiresome amount of trouble with using a bluetooth serial link. The receiving end is a bluetooth-rs232 module conected to my embeded system. The PC has a little usb bluetooth...
3
by: Nasif | last post by:
HELLO ALL I am new in serial port programming. I am currently doing a program that communicates to DSP board through COM1 port. But I am using Microsoft Visual Studio 2005. The language that I...
6
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send...
1
by: lutherchp | last post by:
A baffling end to my week! I open my serial port on my Debian PC (Debian version 5.0.1) I have a decent Null Modem lead going to another PC (I'll call it PC#2), with its port open with the same...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.