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

SerialPort - How to send a message and await a reply?

1
I’ve written a program in C# which scans a network of devices connected to the serial port.
I use a thread to send a message addressed to a external device then I use ManualResetEvent.WaitOne(200) to pause the thread and wait for a reply. If a correct response is received from the serial port, an event is fired which restarts the scan thread with ManualResetEvent.Set(). When a correct response is received or the ManualResetEvent times out, I increment the address and scan for the next device.

This works fine until another program loads up the processor, which can cause the SerialPort DataReceived event to be delayed by over 2 seconds and the ManualResetEvent to time out therefore missing a valid response from a device.

Pseudo code:

Expand|Select|Wrap|Line Numbers
  1. // Scan network method
  2. {
  3.     private void  Scan (void)
  4.     {
  5.         MessageFilter.MessageFilterEvent += new MessageFilter.MessageReceivedHandler(MessageFilter_MessageFilterEvent);
  6.  
  7.         for(int address = 1; address <= 127; address ++)
  8.         {
  9.     send serial message();
  10.     ManualResetEven.WaitOne(200);
  11.     if(message was received)
  12.     {
  13.         Device found
  14.     }
  15.     ManualResetEvent.Reset();
  16.        }
  17.     }
  18.  
  19.     private void MessageFilter_MessageFilterEvent(object sender,MessageReceivedEventArgs e)
  20.     {
  21.         ManualResetEvent.Set();
  22.     }
  23. }
  24.  
  25. // SerialPort received event
  26. private void Serial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  27. {
  28.     Process data
  29.     if(this is a valid message)
  30.     {
  31.         Trigger MessageReceivedEvent event
  32.     }
  33. }
  34.  
  35. // Valid message received event
  36. private void MessageReceivedEvent(object sender, MessageReceivedEventArgs e)
  37. {
  38.     // Filter messages
  39.     if(received message == required message)
  40.     }
  41.         Trigger MessageFilter_MessageFilterEvent event
  42.     }
  43. }

Can anyone think of a better way of writing the send – await response process that doesn’t rely on processor speed?

Thanks in advance,
James
Mar 13 '10 #1
1 7704
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Mar 13 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: DraguVaso | last post by:
Hi, I'm experimenting with the Serial Port in VB.NET 2005. Although it isn't that easy to get any feedback from my COM-port as I thought it would be... How can I read all the Data that the...
1
by: Tolgay Gül | last post by:
I need some codes that able to send and read data by serialport in VB.Net 2005 beta 2. I wrote some codes and It can send data via serialport but It cannot read what I send. I have looked up on...
2
by: Andrea Judge | last post by:
Hi there, I'm developing an application to catch OBD sensors' data from my car. I connect it via a RS232 interface. Now. with HyperTerminal I can connect and send/receive data, but I'd like to...
0
by: Tony Kwong | last post by:
I am using my mobile phone as modem to send some SMS using modem AT commands and have the SerialPort.DataReceived to get the "reply" from the Port using "m_SerialPort.ReadExisting();" private...
3
by: Eric | last post by:
Hi, In my application for PDA I use the serialport class to send data to a navigation computer of VDO Dayton. Everything works fine as long as it is connected to the nav-computer. When the...
13
by: Jean Paul Mertens | last post by:
Hello, Someone can tell me why I dont get serial port events in a Service, I created a separate Thread to open the port but no events are coming up (the same happens when I use the timer...
0
by: nmsreddi | last post by:
Hi friends I am working on serialport in c# ,i am using C#2005 i have successfully done the serial communication with GSM modem and able to send and receive data , the main problem ,the serial...
3
by: Adriano | last post by:
Hello, I'm developing an application in VB.NET 2005 that communicates with a device through RS232, and need to send the following sequence of hexadecimal data to the device: 0xFF, 0x01, 0xC3,...
3
by: arjan321 | last post by:
Hello all, I have a strange problem with the .NET serialPort class. When I write some data to the serialport, not all the data is immediately send: every once in a while, only ~50 characters...
10
by: sklett | last post by:
I'm trying to send some printer commands (ZPLII) to an attached USB printer using the SerialPort component. I didn't get very far at all. In fact I haven't gotten anywhere. The first problem I...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.