TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Lynn Emery
NA
4
18.9k
Terminate Serial Recieved Data Handler
May 27 2016 6:16 AM
Hi. I am receiving data from a serial port, but there is no command in the data to state that the message has ended, therefore my code is just hanging in the data deceived handler and timing out.
My question is, is there a way which i can terminate the handler once i have receiving a certain piece of data or character?
I am currently using the below code to read data from the serial port.
Due to the serial port connection i am unable to use readline() or read existing(), instead i read the data by readbytes.
private void serial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
//Initialize a buffer to hold the received data
byte[] buffer = new byte[sp.ReadBufferSize];
//There is no accurate method for checking how many bytes are read
//unless you check the return from the Read method
try
{
int bytesRead = sp.Read(buffer, 0, buffer.Length);
}
catch { }
//data we are received is ASCII data.
tString += Encoding.ASCII.GetString(buffer, 0, buffer.Length);
//Check if string contains the terminator
if (tString.IndexOf((char)_terminator) > -1)
{
//If tString does contain terminator we cannot assume that it is the last character received
string workingString = tString.Substring(0, tString.IndexOf((char)_terminator));
//Remove the data up to the terminator from tString
tString = tString.Substring(tString.IndexOf((char)_terminator));
// MessageBox.Show(workingString);
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
tString = "";
ProcessSerialBuffer(workingString);
localStatus = "Aknowledgement Recieved";
}
// we then pass the buffer value to the process serial buffer method.
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I welcome any ideas or suggestions, many thanks.
Reply
Answers (
1
)
Abstract class interface implementaion
Concurrency violation: the DeleteCommand