SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 221 | Next

Jan Axelson

"Serial Port Complete: COM Ports, USB Virtual COM Ports, and Ports for Embedded Systems"


! $ +  
For storing bytes and other data types as well as strings, the List(Of T) class (the
List class in C#) offers a solution. The class is in the System.Collections.Generic
namespace and supports methods for manipulating, searching, and sorting list
elements. The AddRange method can add the contents of an array to the end of
a list, expanding the list??™s capacity as needed. The RemoveRange method can
remove a range of elements from a list. The Clear method removes all elements
in a list.
A List(Of T) object can hold a series of bytes:
 Imports System.Collections.Generic
Friend portBuffer As New List(Of Byte)
 using System.Collections.Generic;
internal List portBuffer = new List();
Chapter 10
200
To reduce the number of resizing operations needed as elements are added to
the list, you can specify an initial capacity when creating the list object:
 Friend portBuffer As New List(Of Byte)(1024)
 internal List portBuffer = new List(1024);
The code below appends received bytes to the end of a list and calls a routine to
process the data. The code can execute in a DataReceived event or Timer event:
 Dim numberOfBytesToRead As Integer
' Get the number of bytes available to read.
numberOfBytesToRead = myComPort.BytesToRead
' Create a byte array large enough to hold the bytes to be read.
Dim newReceivedData(numberOfBytesToRead - 1) As Byte
' Read the bytes into the byte array.


Pages:
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233