473,394 Members | 1,701 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,394 software developers and data experts.

synchronizing on serial port data

ssc
I'm new to C#, but have been doing embedded programming for years. I have
an application that talks to an embedded radio on the serial port of my PC.
I have most of the application running pretty well, but if I click a button
before the radio sends its response from the previous command, things get
ugly. I need the application to "lock" waiting on a response from the
serial port before accepting any more commands be sent.

I found the following on the web, but it locks everything including
serialport.readexisting(). I see the command result only after this routine
times out and returns control. I'm obviously doing something wrong. I
don't know exactly how this code works which is part of the problem.

I'm trying to do the following:

send serial command to the Radio (via serialPort)
wait until I get a result back from the radio before allowing any more data
sent to the serial port
I also need a programmable timeout so it doesn't get hung forever

What I have (that doesn't work) is below. Everything in the do-while loop
is what I found on the web. I'm still trying to work out how it works or
doesn't work in this case. Please forgive the messiness. I've been
experimenting with this for several days now.

Thanks for the help!

Scott

private Int16 WaitForResponse(string stringModem, string s, int
timeout)
{
DateTime startTime = DateTime.Now;
int remainingTime = timeout;
bool receivedChars;
object _receiveCharEvent = new Object();

richTextBoxResult.Text = "";

try
{
serialPort.Write(stringModem);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return -1;
}
do
{
lock (_receiveCharEvent)
{
// _receiveBuffer.Append(serialPort.ReadExisting());
// trims the nulls off..since there seems to be a
character
// coming in from the Radio at the beginning of each
command result.
// _receiveBuffer.Replace('\r', '_');

// The radio has a habit of sending a LOT of CR's and LF's
// Need to prune these to make it parseable
richTextBoxResult.Text += serialPort.ReadExisting();
richTextBoxResult.Text.Trim('\r', '\n');

if(richTextBoxResult.Text.Contains(s))
{
return 0;
}

// I have NO idea how Monitor.Wait works
// or if this is what I should even be using
if (0 < remainingTime)
{
// ????? receivedChars =
Monitor.Wait(_receiveCharEvent, remainingTime);
receivedChars = Monitor.Wait(_receiveCharEvent,
remainingTime);
remainingTime = timeout - (int)((DateTime.Now -
startTime).TotalMilliseconds);
}
else
{
receivedChars = false;
}
}
} while (receivedChars);
return -1; // error
}
Mar 14 '06 #1
1 9131
ssc
Of course I finally find something that seems to work AFTER I posted the
question. I changed to a simple do/while loop, then added:

System.Windows.Forms.Application.DoEvents();

to the middle of the loop and everything started working fine. I need to
clean it up, but this is what I have now.

I hope it helps someone else out.

Scott

do
{
System.Windows.Forms.Application.DoEvents();
//
_receiveBuffer.Append(serialPort.ReadExisting());
// trims the nulls off..since there seems to be a character
// coming in from the Radio at the beginning of each command
result.
// _receiveBuffer.Replace('\r', '_');

richTextBoxResult.Text += serialPort.ReadExisting();
richTextBoxResult.Text.Trim('\r', '\n');

if(richTextBoxResult.Text.Contains(s))
{
return 0;
}

if (0 < remainingTime)
{
remainingTime = timeout - (int)((DateTime.Now -
startTime).TotalMilliseconds);
}
else
{
receivedChars = false;
}
} while (receivedChars);
Mar 14 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
2
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. ...
14
by: McBooCzech | last post by:
Hi I am just trying to analyze (parse) data from the serial port (I have connected GPS receiver to the ttyS0, so I can read ASCII characters in the CSV form on the serial port 1). I am doing this...
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...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
5
by: Franklin M. Gauer III | last post by:
Hi All, I've written an ASP.NET application (webservice) that does simple serial communications via the .NET 2.0 SerialComm object. The application runs fine on my development machine. The...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
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...
4
by: max_mont | last post by:
Hi all, I'm a newbie in .NET technology. I've already developed Serial communication applications in C++ (WIN32). And I wanted to migrate to .NET technology. There is a serial component in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.