danielkelly
IS-IT--Management
Hi All,
Im hoping someone can assist me with a project I am working on. I have a device connected to the serial port which is continuously sending data. I have included relevant code below.
I am trying to read this data into a buffer for processing as I will be looking for certain characters within the buffer to indicate a new message (this device does not use a "End of Line" character but rather a start of line.
So far I have the following :-
//Class Wide Variable - [0] is just an arbitrary value at this stage.
private byte[] _currentBuffer = new byte[0];
//Method for Receiving Data
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
//Write The Data into The Com Buffer
//See How Many Bytes are Available
int newBytes = comPort.BytesToRead;
//Create a New Array of Bytes
byte[] newBuffer = new byte[newBytes];
comPort.Read(newBuffer, 0, newBytes);
//Add The Old Array to New Array
//Send the Data of For Processing To Look For Characters.
//If messages are found in buffer, remove from buffer and start again
}
catch (Exception exc)
{
DisplayData(MessageType.Error, exc.Message);
}
}
I am fine with adding the arrays etc and processing, my problem really comes down to where to store the "old data". The idea was to store it in a class wide variable but since you cant dynamically resize an array of bytes, I cant add to the original array.
My issue appears to be with the design and not really the coding side. The original thought was to continually increase / decrease the size of the class wide array so the processing functions can access it.
Can anyone offer some suggestions as to a solid design principle.
THanks in advance,
Daniel
Im hoping someone can assist me with a project I am working on. I have a device connected to the serial port which is continuously sending data. I have included relevant code below.
I am trying to read this data into a buffer for processing as I will be looking for certain characters within the buffer to indicate a new message (this device does not use a "End of Line" character but rather a start of line.
So far I have the following :-
//Class Wide Variable - [0] is just an arbitrary value at this stage.
private byte[] _currentBuffer = new byte[0];
//Method for Receiving Data
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
//Write The Data into The Com Buffer
//See How Many Bytes are Available
int newBytes = comPort.BytesToRead;
//Create a New Array of Bytes
byte[] newBuffer = new byte[newBytes];
comPort.Read(newBuffer, 0, newBytes);
//Add The Old Array to New Array
//Send the Data of For Processing To Look For Characters.
//If messages are found in buffer, remove from buffer and start again
}
catch (Exception exc)
{
DisplayData(MessageType.Error, exc.Message);
}
}
I am fine with adding the arrays etc and processing, my problem really comes down to where to store the "old data". The idea was to store it in a class wide variable but since you cant dynamically resize an array of bytes, I cant add to the original array.
My issue appears to be with the design and not really the coding side. The original thought was to continually increase / decrease the size of the class wide array so the processing functions can access it.
Can anyone offer some suggestions as to a solid design principle.
THanks in advance,
Daniel