Click here to Skip to main content
15,881,833 members
Home / Discussions / C#
   

C#

 
AnswerRe: FullText Search Pin
I Believe In GOD31-May-09 2:01
I Believe In GOD31-May-09 2:01 
QuestionForms, Transparencykey and Windows VISTA Pin
franco nero30-May-09 18:39
franco nero30-May-09 18:39 
AnswerRe: Forms, Transparencykey and Windows VISTA Pin
franco nero30-May-09 20:00
franco nero30-May-09 20:00 
QuestionWeb service Pin
chotuithamgia30-May-09 18:13
chotuithamgia30-May-09 18:13 
AnswerRe: Web service Pin
Dave Kreskowiak30-May-09 18:42
mveDave Kreskowiak30-May-09 18:42 
JokeRe: Web service Pin
Manas Bhardwaj30-May-09 20:13
professionalManas Bhardwaj30-May-09 20:13 
QuestionOne apps causes another app to crash.. [modified] Pin
Jonathan Tripathy30-May-09 11:28
Jonathan Tripathy30-May-09 11:28 
AnswerRe: One apps causes another app to crash.. Pin
Luc Pattyn30-May-09 15:22
sitebuilderLuc Pattyn30-May-09 15:22 
Hi,

first of all, please fix the post with real < pre> tags so code gets shown formatted and hopefully becomes readable. (you must now have & lt; instead of < signs)

there probably are several errors:

1.
I trust spt.BaudRate = 9200; is a typo (9600 and 19200 would be normal values). AFAIK SerialPort would not even accept that odd value.

2.
from the documentation: "The DataReceived event is raised on a secondary thread when data is received from ..."
you are not allowed to touch Controls inside the DataReceived handler, it will sooner or later cause nasty behavior such as freezing GUI or hanging app.

The one correct solution is using Control.InvokeRequired and Control.Invoke; google for that.

3.
in general when messages are coming in over a serial port, the SerialPort class only understand characters, not messages, and the DataReceived event may fire and offer you one partial message, one complete message, two partial messages, whatever. So your code needs to know how to handle these. Quite often by storing it all into a buffer, then looking for entire messages, and removing them when processed.

There probably are two exceptions:
- when quiet periods in between messages exist, you could insert a Thread.Sleep at the start of the DataReceived, with a delay large enough to be sure an entire message would have been received by the time you actually read it out.
- text messages ending on NewLine (some string you can set to SerialPort.NewLine) would cause a SerialPort.ReadLine to return just one message; this means you organize a thread calling ReadLine, and not using DataReceived event at all.


Overall remark:
your code has several try-catch blocks where the information is thrown out; that is a stupid idea. Best is to have
try {...}
catch(Exception exc) {
   log(exc.ToString());
}

where log would be a method able to somehow log/print/show a multiline text.

In the rare case you don't want to see a very specific exception, check for that specific one like this:
try {...}
catch(SomeVerySpecificException exc) {
    // empty block, it is OK to ignore this specific exception
}
catch(Exception exc) {
   log(exc.ToString());
}


This may be relevant to the networking part, where maybe something unexpected is happening without you knowing it at all.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.

GeneralRe: One apps causes another app to crash.. Pin
Jonathan Tripathy30-May-09 23:32
Jonathan Tripathy30-May-09 23:32 
QuestionVehicle Lane Tracking Algorithm Pin
sebogawa30-May-09 9:44
sebogawa30-May-09 9:44 
AnswerRe: Vehicle Lane Tracking Algorithm Pin
Uffe Rask30-May-09 10:52
Uffe Rask30-May-09 10:52 
AnswerRe: Vehicle Lane Tracking Algorithm Pin
Henry Minute30-May-09 11:01
Henry Minute30-May-09 11:01 
QuestionRe: Vehicle Lane Tracking Algorithm Pin
sebogawa30-May-09 11:11
sebogawa30-May-09 11:11 
AnswerRe: Vehicle Lane Tracking Algorithm Pin
Henry Minute30-May-09 11:28
Henry Minute30-May-09 11:28 
GeneralRe: Vehicle Lane Tracking Algorithm Pin
sebogawa30-May-09 11:33
sebogawa30-May-09 11:33 
GeneralRe: Vehicle Lane Tracking Algorithm Pin
Henry Minute30-May-09 11:53
Henry Minute30-May-09 11:53 
QuestionAdding an event to a dynamically created Button Pin
Douglas Kirk30-May-09 8:08
Douglas Kirk30-May-09 8:08 
AnswerRe: Adding an event to a dynamically created Button Pin
Luc Pattyn30-May-09 8:15
sitebuilderLuc Pattyn30-May-09 8:15 
QuestionSQL Server 2008 in C# Console Appications Pin
bigjoe11a30-May-09 4:09
bigjoe11a30-May-09 4:09 
AnswerRe: SQL Server 2008 in C# Console Appications Pin
Mbah Dhaim30-May-09 5:17
Mbah Dhaim30-May-09 5:17 
GeneralRe: SQL Server 2008 in C# Console Appications Pin
bigjoe11a30-May-09 5:35
bigjoe11a30-May-09 5:35 
AnswerRe: SQL Server 2008 in C# Console Appications Pin
Henry Minute30-May-09 7:48
Henry Minute30-May-09 7:48 
GeneralRe: SQL Server 2008 in C# Console Appications Pin
bigjoe11a30-May-09 8:17
bigjoe11a30-May-09 8:17 
GeneralRe: SQL Server 2008 in C# Console Appications Pin
bigjoe11a30-May-09 8:33
bigjoe11a30-May-09 8:33 
GeneralRe: SQL Server 2008 in C# Console Appications Pin
Henry Minute30-May-09 8:59
Henry Minute30-May-09 8:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.