Click here to Skip to main content
15,911,762 members
Home / Discussions / C#
   

C#

 
GeneralRe: use DefWindowProc in c# Pin
DavidNohejl5-Jun-05 7:17
DavidNohejl5-Jun-05 7:17 
GeneralRe: use DefWindowProc in c# Pin
Sasuko5-Jun-05 6:39
Sasuko5-Jun-05 6:39 
GeneralRe: use DefWindowProc in c# Pin
Carsten Zeumer5-Jun-05 10:12
Carsten Zeumer5-Jun-05 10:12 
GeneralRe: use DefWindowProc in c# Pin
Sasuko5-Jun-05 11:21
Sasuko5-Jun-05 11:21 
GeneralRe: use DefWindowProc in c# Pin
Carsten Zeumer5-Jun-05 11:33
Carsten Zeumer5-Jun-05 11:33 
GeneralRe: use DefWindowProc in c# Pin
Sasuko5-Jun-05 12:53
Sasuko5-Jun-05 12:53 
GeneralAccomplishing Listview Column Reorder Programmatically Pin
Hashir Zuberi5-Jun-05 0:24
Hashir Zuberi5-Jun-05 0:24 
GeneralSerialPort/Stress problem Pin
Snowjim5-Jun-05 0:23
Snowjim5-Jun-05 0:23 
Hey!

Intruduction:
I am using Framework 2.0(beta 2) and the System.IO.Ports.SerialPort class to communicat with serialport/USB devices.

I have built my serialport communication with Events, like this:

<br />
#region Create Connection<br />
serialPort = new SerialPort(serialPortSettings.getPort,              serialPortSettings.getBoudRate, serialPortSettings.getParity, serialPortSettings.getDataBits, serialPortSettings.getStopBit);<br />
serialPort.DtrEnable = true;<br />
serialPort.RtsEnable = true;<br />
serialPort.ReceivedBytesThreshold = 4;<br />
serialPort.Open();<br />
#endregion<br />
<br />
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_ReceivedEvent);<br />


SerialPort Settings that I use
Baud rate: 115200
Data bits: 8
Parity: None
Stop bit(s): 1

This is done by a special serialport thred, this to make sure that the main thread(the one that redraws the GUI and so on) not will be involved in the event firing of this serialport.

In serialPort_ReceivedEvent you can finde the folowing code:

<br />
lock (this)<br />
{<br />
if (RUNNING)<br />
           {<br />
           ThreadPool.QueueUserWorkItem(new WaitCallback(onDataReceived));<br />
           currentRunningThreads++; // keep track of amount of thread currently working<br />
           }<br />
}<br />


This function will be fired as soon as 4 bytes(ReceivedBytesThreshold) have been written to the serialport. As you can see, it will place a work in the ThreadPool and then make the current thread available for next event.

The onDataReceived looks like this.

<br />
                if (RUNNING)<br />
                {<br />
                    lock (this)<br />
                    {<br />
                        if (serialPort.BytesToRead > 0)<br />
                        {<br />
                            string data = serialPort.ReadExisting();<br />
<br />
<br />
                            if (data != null && data.Length > 0)<br />
                            {<br />
                                RemovePortHandlerRAW.Write(data); //writes data to file, just to have something to compare with<br />
                                RemovePortHandlerRAW.Flush();<br />
<br />
                                data = toolHandler.costmizeString(data, timeStamp, showCR_LFOnIncomingData);<br />
<br />
                                streamHandler.writeData(data); //ourData<br />
<br />
                                if(SAVEDATATOFILE)<br />
                                    rows = toolHandler.stringToArray(data);<br />
<br />
                                if (rows != null)<br />
                                {<br />
                                    if (SAVEDATATOFILE)<br />
                                        dataCollector.writeArrayToFile(rows);<br />
                                }<br />
                            }<br />
                        }<br />
                    }<br />


As you can see, am doing different things in this onDataReceived function, but the main part is: ReadExisting on the serialport and writes it to file to be abled to compare it with the file I am realy sending to my program by using CompareIt(http://www.grigsoft.com/)

TEST:
I have created a program that sends a 2 MB file with a serten data structure where every row ends with CRLF. This program sends one char at a time and this as fast as posible over a nullmodem cable (from laptop to my computer)

When the file is transfared to my computer from the laptop, it will be compared with the file I sent from my laptop, to ensure that no data is lost.

Test Result:
I have manage to find a good ReceivedBytesThreshold(decides how many byte that have to be written to the serialport before the event is fired) on the serialport that I don’t lose any data, but have a good CPU usage. I first tried 512, 256, 32. But all this value will result in some Dataloses in high speed. I then tested 4, and that works good(no data loses).

Why not choose 1? By choosing 1 in ReceivedBytesThreshold will result in a higher CPU usage.

Stress Test:
To ensure that no data will be lost I hade to made some stress tests.

Test 1:
I tried to move around a firefow window(web browser) with an andvance page loaded(like www.gamespot.com). By doing this I get 100% CPU usage, and if I do this as long as posible during my program is receiving data over the nullmodem cabel, this test showed that data are lost during stress test like this!!

Sometimes it could also result in an array that says that a Thread have been interupted, but I don’t realy know why?

Test 2:
By open a window in WindowsXp with some files in and the hold F5 pressed it will update the window so fast that the CPU usage will be 100%. But this will strangly not effect the data flow? No data is lost?

Problems:
As you can see, I cant get a sequre data transfer! I have tried other program with the same stress tests like the one here http://www.aggsoft.com/, but this works fine!

Maby this is I Microsoft problem, maby the serilaport in framework 2.0 is not reay finished yet?

Maby I shouldt buther with the outcome on Test1?

I have also tried to have a Thread loop instead of the events, but that dident help(same problem).

I realy need help here, It would be great if anyone else have the posibility to try the exact same thing.

Best Regards
SnowJim
GeneralRe: SerialPort/Stress problem Pin
S. Senthil Kumar5-Jun-05 4:01
S. Senthil Kumar5-Jun-05 4:01 
GeneralRe: SerialPort/Stress problem Pin
Snowjim5-Jun-05 4:14
Snowjim5-Jun-05 4:14 
GeneralRe: SerialPort/Stress problem Pin
S. Senthil Kumar5-Jun-05 7:01
S. Senthil Kumar5-Jun-05 7:01 
GeneralRe: SerialPort/Stress problem Pin
Snowjim5-Jun-05 7:49
Snowjim5-Jun-05 7:49 
GeneralRe: SerialPort/Stress problem Pin
Sebastian Schneider5-Jun-05 20:07
Sebastian Schneider5-Jun-05 20:07 
GeneralRe: SerialPort/Stress problem Pin
Snowjim6-Jun-05 0:59
Snowjim6-Jun-05 0:59 
GeneralC# directory file-structure reading Pin
Steepy_dipper4-Jun-05 22:51
sussSteepy_dipper4-Jun-05 22:51 
GeneralRe: C# directory file-structure reading Pin
Carsten Zeumer4-Jun-05 23:33
Carsten Zeumer4-Jun-05 23:33 
GeneralRe: C# directory file-structure reading Pin
GaMBiT_KC4-Jun-05 23:34
GaMBiT_KC4-Jun-05 23:34 
GeneralSql Query Builder from an app Pin
Leyu4-Jun-05 20:43
Leyu4-Jun-05 20:43 
GeneralShort cut icons on a windows form Pin
Rinventive4-Jun-05 20:42
Rinventive4-Jun-05 20:42 
GeneralRe: Short cut icons on a windows form Pin
Carsten Zeumer4-Jun-05 23:46
Carsten Zeumer4-Jun-05 23:46 
GeneralRe: Short cut icons on a windows form Pin
Anonymous5-Jun-05 19:29
Anonymous5-Jun-05 19:29 
GeneralExchange the Language on a localized Windows Forms form Pin
Uwe Keim4-Jun-05 20:30
sitebuilderUwe Keim4-Jun-05 20:30 
Generalpls Provide C# code for the Visual Basic code Below Pin
monica2k4-Jun-05 18:00
monica2k4-Jun-05 18:00 
GeneralRe: pls Provide C# code for the Visual Basic code Below Pin
Chris Austin4-Jun-05 19:33
Chris Austin4-Jun-05 19:33 
GeneralRe: pls Provide C# code for the Visual Basic code Below Pin
WillemM5-Jun-05 0:58
WillemM5-Jun-05 0:58 

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.