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

Serial communication

Hi,

every 250 ms (timer event) I must send several commands via serial
port. The serial unit will answer to each command sent. I must wait the
answer before send the next command.

How can I achieve this? I receive the incoming data in the "data
received" event of the serial port. Furthermore I need to manage a
timeout: if I receive no answer within a specified amount of time an
event will be generated.

Here an example in pseudo-code:

Timer event:
Send command 1
Wait for answer
Send command 2
Wait for answer
Send command 3
Wait for answer... the communication was broken so the timeout event
will be generated

Data_received event:
Store the incoming string into the right variable (selected by the
command sent).

Timeout event:
Message to the user "Communication fault!"

Thank you for any advice :)

Marco / iw2nzm
Jun 25 '07 #1
2 1903
Hi,

By far the easiest way to do this is NOT to use DataReceived. Simply poll
the BytesToReceive property in a loop and grab the data needed when the full
response has been received -- or if you have to parse receive data to
determine when a completer message has been received, then append newly
received data to a buffer in the loop and parse it. Exit the loop only when
the data you want is there (or if a timeout occurs).

BTW, the polling method WILL NOT block the receive thread, which executes in
the background. However, if you need yout application to be responsive to
user input during polling, you will need to call Application.DoEvents()
inside the loop.

This approach (IMO) is better than using ReceiveData because state
information does not have to be passed around, AND because timeouts are
easily handled. This makes the code simpler and simpler means both better
performance AND more easily debugged.

The actual constuction of the loop can (and should) be in a method that
allows the code it contains to be reused by other commands and responses.

I have more details on the pros and cons of each approach in my book (see
below).

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.
Jun 26 '07 #2
Dick Grier wrote:
Hi,

By far the easiest way to do this is NOT to use DataReceived. Simply poll
the BytesToReceive property in a loop and grab the data needed when the full
response has been received -- or if you have to parse receive data to
determine when a completer message has been received, then append newly
received data to a buffer in the loop and parse it. Exit the loop only when
the data you want is there (or if a timeout occurs).

BTW, the polling method WILL NOT block the receive thread, which executes in
the background. However, if you need yout application to be responsive to
user input during polling, you will need to call Application.DoEvents()
inside the loop.

This approach (IMO) is better than using ReceiveData because state
information does not have to be passed around, AND because timeouts are
easily handled. This makes the code simpler and simpler means both better
performance AND more easily debugged.

The actual constuction of the loop can (and should) be in a method that
allows the code it contains to be reused by other commands and responses.

I have more details on the pros and cons of each approach in my book (see
below).

It sounds very interesting. Thanks for your answer.

Marco / iw2nzm
Jun 26 '07 #3

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

Similar topics

1
by: Andreas Horneff | last post by:
Hi @ all, I've got a problem with serial communication in Borland C++ Builder. I've already found a lot of stuff about serial communication in the internet, but it dosen't work. What I want...
11
by: sarah | last post by:
hI, I have a project. I need to realise serial communication between PC and Altera FPGA in c or c++. The function of FPGA is working well. it can send data to uart. Does anybody know how to...
3
by: carmen | last post by:
I'm working in an aplication for a Smart Device that need to "talk" with a printer continuosly through the serial port. I'm trying to use the John Hint's sample code "Use P/Invoke to develop a .NET...
4
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code into the Visual C#.NET. My C++ code is mainly based on the serial communication. So I am using the windows structure such as DCB.. etc and the windows...
6
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in...
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. ...
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...
2
by: Adrian Chen | last post by:
please help me! I come across a problem. Now I develop a finger print management system which is based on B/S.When users click a button in the web pages, a device connected to the COM1 serial port...
0
by: Dhananjay | last post by:
Hi, I am working on an VB.Net application which I want to communicate to external device using comm port (Serial Port) . So for that first I am trying to simulate the communication on serial...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
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
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...

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.