[
Advertise | Submit Code | About us | Contact us | Link us
]
Go!
Membership Services
Login
Register

Home
C# General

General

C# Language

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting
C# Windows Forms

General

Combo and List boxes

Miscellaneous Controls

Button Controls

Edit Controls
Cutting Edge

ASP.NET 2.0

Visual Studio 2005

Windows Longhorn

SQL Server 2005
C# Multimedia and GDI+

General

DirectX

GDI+

Audio
Internet & Web

General

Images and multimedia

Database

Utilities

Security

ASP.NET Controls

Design and Architecture

Webservices
.NET

General

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting

ADO.NET

XML.NET

Tools

Enterprise

IDE
Visual Basic .NET

VB.NET General

VB.NET Controls
General Reading

.NET Books Review

Product Showcase

Book Chapters

Business Design & Strategy
Community

Discuss

Job Board

Discussion

CodeXchange
DeveloperLand

Advertise

Submit Code

About us

Contact us

Link us
Miscellaneous

Favorite Links

Downloads

Programming Sites

Top Stories
Regular Expressions

E-Mail

Date/Time
Home > .NET > General
Advanced .NET Reflection
Posted by on Friday, October 01, 2004 (EST)

.NET is a new generation of technology based on Web services.Is .NET able to handle the nuts-and-bolts of low-level serial communications? Let's use .NET and Reflection to implement serial communications software for controlling a large congress network.

This article has been viewed: 2,780 times
Technology: General.

Contents

Introduction

Late march this year my company has been asked to take part in a joint project to deliver a complete congress solution for a major government organization here in Slovakia. The hardware was the Philips Digital Congress Network (DCN, see
links [^]) line of products and my company had to develop new control software for the DCN Central Control Unit (CCU).

I took over the software part of the project and started analyzing the requirements. Here they are in a nutshell:

  • Write a serial protocol handler to communicate with the CCU using its custom protocol.
  • Write a set of end-user applications to control and manage the various subsystems found in the CCU, such as microphone management, voting, delegate registration, access control, etc.

The "funny" part of this project turned out to be the protocol handler, mainly because the protocol specifications were a bit incomplete and imprecise (and I'm very tolerant with this wording;-).

The protocol basically describes how to transfer messages between the CCU and the controlling PC. The message data are opaque streams of bytes. The actual structure of the data is implied by the message type, which is defined in the documentation using C-style syntax. For illustration, here is the definition of a message that the CCU sends when a delegate inserts a PIN card into her delegate unit (don't worry about the semantics, its not important for this discussion):

typedef struct
{
 WORD  wUnitId;
 WORD  wDelegateId;
 BYTE  byAttend;
} AT_T_DEL_ATTEND;
Because there are lots of different message types defined, I wanted to design some generic mechanism that would allow me to:
  • Easily define the structures as VB.NET classes.
  • Automatically handle the conversion of the classes to and from byte streams transmitted over the serial line.
In other words, I wanted to provide a kind of remoting infrastructure for transferring .NET objects across the serial line. Reflection seemed to be an ideal tool for solving this kind of problem.

I came up with the following design:

The CcuSerializable attribute can be applied to a class or a field to control the serialization process.

The SerializationManager class that implements the actual serialization and deserialization of CcuSerializable objects to and from byte streams. SerializationManager uses Reflection extensively to interrogate a set of CcuSerializable fields for a given class, as well as to actually write and read the fields' values to and from byte streams, for example:

Private Sub SerializeInteger( _
  ByVal obj As Object, _
  ByVal field As FieldInfo, _
  ByVal attr As CcuSerializableAttribute, _
  ByVal writer As System.IO.BinaryWriter)
 
 Dim Value As Integer = CInt(field.GetValue(obj))
 writer.Write(Value)
End Sub
Private Sub DeserializeInteger( _
 ByVal obj As Object, _
 ByVal field As FieldInfo, _
 ByVal attr As CcuSerializableAttribute, _
 ByVal reader As System.IO.BinaryReader)
 
 Dim Value As Integer = reader.ReadInt32()
 field.SetValue(obj, Value)
End Sub
You can find a link to the complete SerializationManager source code at the end of this article. (Yes, complete code; taken straight from the project and placed on my website. For you:-)).

With SerializationManager, implementing the various control applications was easy. I've had just to tag the serializable classes with the CcuSerializable attribute like this...

<CCUSERIALIZABLE()> _
Public Class AttnEntry
 <CCUSERIALIZABLE()> Private _UnitID As Short
 <CCUSERIALIZABLE()> Private _DelegateID As Short
 <CCUSERIALIZABLE()> Private _Status As Byte
 ...
...And the rest was handled automatically.

That's it.

Well, the project isn't completed yet. It will enter beta testing at the beginning of August (this year;-). The software will control over 150 delegate units and I've tested with just 15 so far. Nevertheless, I'm quite confident that the software will survive the load of the production environment and that the use of Reflection won't be a problem. Stay tuned, I'm going to write another article after the project completes.

(And please, don't worry--I'll be honest and tell you when something would go wrong. After all, VBInfoZine is about real projects and they have a tendency to fail sometimes:-)

© Palo Mraz - Monday, July 14, 2003

Top Go to Table of Contents

Links

http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/ [^] - John Hind's article about serial communications in .NET. Accompanied with C# source for the CommBase class used within the project described here.

http://www.philipscsi.com/ [^] - The makers of the DCN product line. Formerly Philips Communication, Security & Imaging, now owned by Bosch.

Top Go to Table of Contents

About Palo Mraz

Click here if you want to know more about .

Other articles that may interest you

  • Write a Word Add-In – Part 0
  • Write a Word Add-In – Part I
  • Lengthy Operations on Single Thread in .NET Application
  • Learning Draughts
  • Exceptions and Performance
  • Average Rating :

    Discussion Forums
    Got a programming related question? Hopefully someone has the answer... Want to help out other developers? Visit our discussion forums.

    Sponsored by:

    New Articles

  • Exceptions and Performance
    Almost every time exceptions are mentioned in mailing lists and newsgroups, people say they're really expensive.Let's examine that claim, shall we?

  • Creating multilingual websites - Part 1
    Extend the existing globalization capabilities of .NET to create flexible and powerful multilingual web sites. First, create a custom ResourceManager, and then create custom localized-capable server controls to easily deploy multilingual functionality.

  • Parameter passing in C#
    Many people have become fairly confused about how parameters are passed in C#, particularly with regard to reference types. This page should help to clear up some of that confusion

  • Most Popular Articles

  • LDAP, IIS and WinNT Directory Services
    This article explains how to use .NET Directory Services to retrieve and search directory objects, create new directory objects and edit or delete existing directory objects. Describes Active Directory Application Mode (ADAM) and how to use the IIS, WinNT and LDAP directory (ADSI) provider.

  • An in-depth look at WMI and instrumentation, Part II
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article is the second part of a two-part series.

  • An in-depth look at WMI and instrumentation, Part I
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article provides an in-depth look at WMI and MOM 2005

  • New Books

  • Murach's ASP.NET 2.0 Upgrader's Guide: VB Edition
    What’s new and how to use it! That’s what this book delivers if you’re a VB developer who’s interested in upgrading from ASP.NET 1.x to ASP.NET 2.0.

  • C# in easy steps
    Learn to program with Microsoft’s premier programming language. No previous programming knowledge is assumed. With numerous easy-to-follow examples, this title explains the essentials of object-oriented programming with C#.

  • Murach's ASP.NET web programming with VB.NET
    Murach's ASP.NET web programming with VB.NET by Doug Lowe and Anne Prince is a in depth training and reference book for ASP.NET programming using VB.NET. The book builds upon Murach's previous books and covers more advanced concepts for programming ASP.NET pages.

  • Got Code?

    if you have any article , source code , or anything else you'd like to share with this community that you think others might find useful, please submit it here and we will gladly make it available on this site. submit@developerland.com.
    Partners

    All articles are copyrighted by their individual authors unless otherwise specified , everything else Copyright ©2004-2006 DeveloperLand