Polling is a technique you can use when a program needs to repeatedly check a signal for a change of state. When polling, usually performed in a loop, detects a change, the program exits the loop. Another name for this technique is busy waiting.

Polling is not an advisable technique because it consumes considerable amounts of processor power until the expected event. A much preferred technique uses interrupts, that is, a change of state of an input causes the interruption of a program by the hardware and the triggering of a handler. However, in embedded systems, when the processor can only perform a single task and there may be restrictions on the ability of the hardware to actually generate interrupt signals, polling is sometimes the only way to react to a signal. One way to improve performance, and allow other threads to execute while waiting is to cause the thread to pause execution by calling a Sleep method in the polling loop. Chapter 7, "Creating a User Display," describes this technique.


A port is the generic name for any form of connection to a hardware device. A port can be a single pin, which can reflect the state of one bit that is either on or off, or it can be a number of bits grouped together, which can deliver a larger number of possible states. It can also be a connection using a particular protocol, for example, a serial port that a program can use to transfer data between the computer and a connected device. In programming terms, an instance of a class represents a port. You call methods on the instance to get information out of the port, for example, to read a data byte received from a serial connection. A port object may also generate events to which your program can bind so that when a pin changes state, or information arrives, the program can call a method to deal with the event. In C#, you bind events to program code by using delegates.


You can make members of a class private, meaning that only methods within the class have access to the member and ensures that other programmers can't get access to the data payload of your objects and corrupt them. You can also make methods in a class private, which means that their use is possible only within that class (you would do this with utility methods that should not be visible outside the class). As a rule, data members are made private so that you can manage their access. The only exception to this is when your concern about speed (accessing via methods will slow things down) exceeds your concern about security.


A property is extremely useful and makes your code a lot cleaner. Essentially you can have code like the following:

 x.Width = 99; 

This looks like an assignment to a member of a class, but it can be much more than that, and result in code running. You could manage the Width property like this:

 class ThingWithWidth {    private int widthValue;    public int Width    {       get       {          return widthValue;       }       set       {          widthValue = value;       }    } } 

When a program performs the assignment to the property, the set portion runs. In the body of the set portion, the keyword value represents the value to which the property is to be set. The previous code does a simple assignment, but you could validate the value and throw an exception if you don't like it. Both the property and the internal data value it represents must have an identifier. One convention is to put the word value on the end of the name of the internal value. Of course, you don't actually have to have a value inside the class; you could calculate a result rather than return a member.

When it comes to setting the value, you can get code to run when you change the value of your property. This makes creating state machines easy. Furthermore, you don't have to implement both a get and a set behavior; you can have just one so that you can create write-only (or read-only) properties. You can have many instances of get for the same property value; perhaps you would like to read the speed in kilometers per hour (KPH) as well as miles per hour (MPH).

The only downside is that you must be aware that substantial amounts of code can run when you perform innocent-looking assignments.


A protected member of a class is visible to methods in the class and to methods in classes that extend this class. It is kind of a halfway house between private (no access to methods outside this class) and public (everyone has access). It lets you designate members in parent classes as being visible in the child classes.


A public member of a class is visible to methods outside the class. It is conventional to make the method members of a class public so that code in other classes can use them. A public method is the way a class provides services to other classes.


A reference is a bit like a tag that you can attach to an instance of a class. The reference has a particular name. C# uses a reference to find its way to an instance of the class and use its methods and data. You can assign one reference to another. If you do this, the result will be two references referring to that single object in memory. In C#, references are type safe because a reference to one particular object, for example, an InputPort, would not be able to refer to any other object. Thus, when the reference is followed to an object, the actions performed with that object will always be correct. Some languages, for example, C++, provide a means to bypass this type validation. This can increase the speed of the program, but it does mean that a program may be less reliable, and the consequences of a mistake could be the complete failure of the program.


The System.Reflection classes provide a means by which a program can determine information about compiled program code and the assembly of which it is a part. It is even possible to create instances of classes and call methods inside these instances by use of the reflection classes. Reflection is used when class instances are serialized. A serialization save method works through the members of the class, obtains their values, and then writes them to a stream for storage or transmission. When a program reads the data back in, the serialization library can use reflection methods to create new instances of the classes and set their internal values correctly.

Reflection is possible because a large amount of metadata, used by reflection methods, is stored with compiled code in an assembly. You can use reflection techniques to deconstruct existing code. For this reason, if you are producing code for sale, you should investigate obfuscation technology that encodes the contents of the assemblies in such a way that preserves the functionality but makes it impossible for someone to reverse-engineer the compiled program.


Serialization is a technique whereby the information in an object, that is, the values of all the properties in the object, is converted into a form that can be stored or transferred to another device. There are essentially two forms of serialization in .NET: binary serialization, in which the object content is converted into a sequence of 8-bit data values that can be stored; and XML serialization, in which object content is converted into an XML document containing elements that describe the content values.

Static members of a class are not persisted when an instance of a class is serialized. These are regarded as part of the class itself, not a particular instance. You can use the [Serializable] attribute to mark a class as having a need for serialization of instances. There is also a [NonSerialized] attribute that you can use to flag members of a class so they are not serialized:

 [Serializable] class Reading {    int x;    int y;    string description;    [NonSerialized]    int temp; } 

In the previous class, if an instance is serialized, the x, y, and description members would be saved but the integer temp value would not. There is an additional attribute mechanism that allows you to provide hints to the serialization process that will improve its efficiency. You should investigate the SerializationHintsAttribute class to find out more about this.


A signature is the particular combination of parameters and return type that can be used to characterize a method. For example, the method float sine (float x) accepts a single floating-point parameter and returns a floating-point result. It therefore has the same signature as float cosine (float x) but not the same one as float random().


In the context of C#, the keyword static makes a member of a class part of a class rather than part of an instance of the class, so you don't need to create an instance of a class to make use of a static member. It also means that programs access static members by the name of their class rather than a reference to an instance. Static members are useful for creating class members that the program is to share with all the instances, for example, interest rates for all the accounts in your bank.


A structure is a collection of data items, declared using the keyword struct. It is managed by value, not by reference, and struct contents are copied on assignment. Structures are also passed by value into methods. Structures are useful for holding a set of related data in single units. They are not as flexible as objects managed by reference, but they are more efficient to use because accessing structure items does not require following a reference in the same way as an object. An array of struct items exists in a single block of memory, as opposed to an array of references.


A subscript is a value used to identify an element in an array. It must be an integer value. Subscripts in C# always start at 0 (this identifies the initial element of the array) and end at one less than the number of elements in the array. Thus, if you create a four-element array, you designate elements in the array by subscript values of 0, 1, 2, or 3. You can best regard a subscript as the number of array elements you need to skip over to get the element that you want. This means that the first element in the array must have a subscript value of 0.


The C# keyword this has different meanings depending on the context in which it appears. A constructor of a class uses it to call another constructor. A program can also use it as a reference to the current instance, for use in nonstatic methods running inside that instance.


A thread is a unit of execution. If you visualize program execution as trains running down tracks through your source code, executing a statement at each station that they pass through, a thread represents a particular train. At any given instant, there may be multiple threads executing a given sequence of code. Threads all share the same variable space, which can lead to problems with synchronization if two of them try to use the same variable at the same time. This will not necessarily cause the threads themselves to fail, but it will mean that the variables do not hold the expected values, which might cause the program to fail. To resolve this issue, the .NET Micro Framework provides a synchronization mechanism using the Monitor class, whereby one thread can guarantee that it is the only one executing a particular set of statements at a time. This is analogous to a length of railway track down which only one train can travel at a time. Thread synchronization is a complex issue, and one to which you should apply considerable thought when designing your programs. Using a thread as a means of abstraction, where you create a thread to deal with a particular issue, is very useful in program design. Whenever an event occurs in a .NET Micro Framework device, for example, an input pin changes state, a thread is created to run the event handler. When the event handler completes, the thread ends. When using threads, you have to be careful that multiple threads do not get stuck waiting for each other to finish using a particular resource. Debugging programs that contain more than one thread of execution is particularly difficult because the problem may only occur when a particular set of timing issues arise. However, used with care, threads make programming significantly easier.


In C#, all data items have a particular type associated with them. Some types are built into the C# language. These types, for example int, float, and bool are available to all programs written in the language. You can add other types from libraries, for example, DateTime. Finally, you can create your own types to hold a collection of data and behaviors that are specific to the problem at hand.

The C# compiler will ensure that whenever a program uses variables of different types together, there is no potential for error or loss of data. As an example, an attempt to move a value from a variable of floating-point type into an integer will result in the compiler generating an error unless the programmer uses a cast to indicate that, in this context, the action is valid. Type checking is performed at compile time (called static type checking) and when the program runs. Thus, even if the programmer uses a cast to force one type to be used as another, at run time such actions would be rejected. This extra stage makes C# programs much safer, but the extra runtime type checking does slow the program down.

Unit test

A unit test is a small test that exercises a component and ensures that it performs a particular function correctly. You should write unit tests during the development process so that you can perform them just after (or in test-drive development just before) you write the code.

Value type

A value type holds a simple value. A program passes value types as values into method calls and copies their values upon assignment; that is, x = y causes the copying of the value in y into x. Changes to the value in x will not affect the value of y. Note that this is in contrast to reference types for which the result of the assignment would make x and y refer to the same instance.

Virtual Machine

A computer system contains a hardware processor that takes machine code programs and executes them. The machine code instructions tell the processor to move data to and from memory, perform tests on the values, and change what it does depending on the results of these tests. The structure of the hardware, the types of data-processing registers, and the instructions themselves are particular to a given computer architecture. This is why it is not possible to take a program written for a Smartphone and execute it on a Microsoft Windows Vista PC.

A virtual machine is a processor architecture designed for implementation by a program that runs on a computer, rather than by hardware directly. A virtual machine program performs the same functions as a hardware processor, but does it in code.

The benefit of a virtual machine is that it can hide the nature of the underlying hardware from the program that runs on it. The virtual machine can also validate the instructions and ensure that the program does not perform operations that would affect the integrity of the host computer. The result of this is that implementations of the Framework can execute programs compiled to Microsoft intermediate language (MSIL) unchanged on a wide range of different platforms.

The disadvantage of a virtual machine is primarily one of speed; the layer of software sitting between the user program and the underlying hardware will tend to slow programs down. The .NET Framework addresses this problem by performing just-in-time (JIT) compilation, that is, it converts the virtual machine instructions into native machine code just before it executes them. The .NET Micro Framework interprets instructions instead of doing JIT compilation. In other words, the virtual machine in a .NET Micro Framework device is continuously loading an instruction, decoding it, and then acting on it before moving on to the next one. This form of execution does allow the interpreter to be a very small program, but it does further restrict the speed of execution.

Virtual Method

A method is a member of a class. You can call the method to do a job. Sometimes you may want to extend a class to produce a child class that is a more specialized version of that class. In that case, you may want to replace (override) the method in the parent with a new one in the child class. To override a method, you must have marked the method in the parent class as virtual. Making a method virtual slightly slows down access to it because the program must look for any overrides of the method before calling it. This is why all methods are initially not virtual.

Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net