Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


ReadLine returns a String containing everything up to the first NewLine char-
acter.
Peek and ReadToEnd are methods that might appear useful but in reality aren’t
practical for most COM-port applications.
Peek returns the next value in the stream without removing the value from the
stream or returns -1 if no character is available. The method can be useful when
reading from resources such as files but is less helpful in COM-port applica-
tions that send data continuously. When the buffer is empty, Peek returns -1
but then continues to return -1 even after more characters have arrived at the
port.
ReadToEnd returns a String containing everything from the current position to
the end of the stream. The stream normally has no way of knowing when it has
reached the end of a particular block of COM-port data. (When reading from a
StreamWriter object, ReadToEnd returns when the StreamWriter object closes.)

    



Users often find it convenient when an application remembers and selects the
port used the last time the application ran. One way to save parameters is to
store them in the system registry using the RegistryKey class in the
Microsoft.Win32 namespace. The .NET Framework provides another option
in the Application Settings architecture, which enables storing and retrieving
settings in files that don’t clog the registry.

1
  
   


The System.Configuration namespace includes the AppSettingsSection class,
which provides the Settings property for saving and retrieving settings. The Set-
tings property is a NameValueCollection of strings and string values. (The
NameValueCollection class is in the System.Collections.Specialized
namespace.) The Application Settings architecture was introduced in .NET
2.0.
To create a setting, in Visual Studio, in the Project menu, select Project_name
Properties, where Project_name is the name of your project. In the window that
appears, select Settings (Figure 9-1). For each setting, enter a name and default
value and select a data type and scope. User scope is for settings that users can
change, while application scope is generally for settings that never change.
Free download pdf