SerialPort problem - part 2

  • Thread starter Thread starter Simon
  • Start date Start date
S

Simon

Hello All,

My problems continue I after three day I can not find solution for my
problems with SerialPort.

Now I have written simple application that will receive and transmit data.
Connected two PCs and started testing. I can transmit, but to receive data I
have problems. If I set both PCs to the same baud rate speed and other port
settings are also the same I will not get anything. If I set the other PC to
lower speed, than I will get characters on the first PC (in .NET
application). Of course I receive garbage, but the characters are there.

Is there a any known problem with SerialPort?
Is there a working sample of the SerialPort use?

Best regards

Simon



My code:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

serialPort.PortName = "COM1";
serialPort.BaudRate = 115200;
serialPort.DataBits = 8;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.Handshake = System.IO.Ports.Handshake.None;
}

delegate void SetTextCallback(string text);

private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.AppendText(text);
}
}

private void serialPort_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}

private void button7_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen)
{
button7.Text = "Open";
serialPort.Close();
}
else
{
button7.Text = "Close";
serialPort.Open();
}
}

private void button8_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen)
serialPort.Write("Test\r\n");
}
}
 
Hello,

I notice the thread "Problems with SerialPort data received event" you
posted yesterday, in which ManualResetEvent is used to sync the threads.
However, in this thread, I do not find the use of the Port class. Are you
still using ManualResetEvent?

In the previous post, ReadThread is used to read messages and process the
inputs with ReadMsg. How did you call ReadMsg? Would you paste more codes
about ReadThread so that we can have a clearer picture of the routine? In
the ReadMsg call, why did you call msgEvent.Reset(); when msgList.Count<=0?

Here are some tips and examples of SerialPort for your reference:
Top 5 SerialPort Tips
http://blogs.msdn.com/bclteam/archive/2006/10/10/Top-5-SerialPort-Tips-_5B00
_Kim-Hamilton_5D00_.aspx
SerialPort and DataReceived Event
http://blogs.msdn.com/bclteam/archive/2006/05/15/596656.aspx

Hope it helps

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
How are the PCs connected? Are you using a null modem (you must, of
course)? You can download a working terminal example from my homepage.
Where is the DoUpdate code?

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
I have deleted the wrong code:

private void DoUpdate(object s, EventArgs e)
{
//textBox1.Text = textBox1.Text + serialPort.ReadExisting();
textBox1.AppendText(serialPort.ReadExisting());
}

And yes, I am using the null modem cable. I have tested the connection with
other software and is working correctly. The problem with my .NET code is
that when I set the speed to wrong setting it will start working (receiving
characters - garbage of course).

Best regards

Simon
 
I have tested your application V2005Term and is not working. I am testing on
two PC's:
- Vista 64 bit
- XP 32 bit

The transmit is OK, but the receive is not working. The symptom is the same
as with my application - no characters are received.

I suspect that this is problem with .NET framework, because this simple
program could not go so wrong.

I have tested the program BaseTerm and this one is working (from MSDN). Most
likely I will drop the SerialPort class and start using class from this
sample.

Best regards

Simon
 
Hi,

There is no known problem with the framework, at least in this area. I have
used it on dozens of projects -- and for more than three years. You can
download DesktopSerialIO (2005 version) from my homepage, too.

Have you tried HyperTerminal? There is something wrong with your code or
computer(s), I think.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
HyperTherminal is working. Also some old sample from MSDN. I have tested
this on 2 PCs and the results are the same.

This is strange for me also.

Best regards

Simon
 
I have no idea. I use .NET serial port code (similar to the code on my
homepage, though different in detail) on a daily basis. As long as
DataBits, BaudRate and Parity are correct (in agreement), then the data are
received correctly. This is purely text data, correct? If there is binary
data mixed with the ASCII, you must us Read, not ReadExisting.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
I am having the same trouble of the code below failing to read. The
write code works fine, and Hyperterminal will go and pick up my failed
reads once I open it up. Hyperterminal sends and recieves fine.

This same software was tested and worked in past, but is failing now.
It is a mystery to me. Perhaps a patch for .NET was rolled out that is
causing this??

Mark


private void btnRead_Click(object sender, EventArgs e)
{
strReadText = "";
if (_serialPort.IsOpen)
{

strReadText = _serialPort.ReadExisting();
if (strReadText.Length > 0)
{
strLog.AppendText(strReadText.TrimEnd('\r', '\n', '
') + "\r");
}
}
}
 
Back
Top