Hello,
i investigated the code (sample project) you send to me (and connect TX and RX of COM3 together):
- Imports System.IO.Ports
- Dim Rs232 As New SerialPort("COM3", 38400)
- Sub Main()
- Dim x(1) As Byte
- Rs232.Open()
- While (True)
- x(0) = 42
- Rs232.Write(x, 0, 1)
- If (Rs232.BytesToRead > 0) Then Rs232.Read(x, 0, 1)
- Rs232.DiscardInBuffer()
- End While
- End Sub
Display More
and you are right that the classes for controlling serial ports are memory intensive. But i can not detect a memory leak. You may use the library from http://www.opennetcf.org/CategoryView.aspx?category=Home for higher performance.
I modified the code as fellowing:
- Imports System.Threading
- Dim Rs232 As New SerialPort("COM3", 38400)
- Sub Main()
- Dim t As String = ""
- Dim x(1) As Byte
- Dim y(1) As Byte
- Dim sw As Boolean = True
- Rs232.Open()
- While (True)
- x(0) = 42
- y(0) = 43
- If (sw = True) Then
- sw = False
- Rs232.Write(x, 0, 1)
- t = "42"
- Else
- Rs232.Write(y, 0, 1)
- sw = True
- t = "43"
- End If
- Thread.Sleep(10)
- If (Rs232.BytesToRead > 0) Then
- Rs232.Read(x, 0, 1)
- Console.WriteLine(t)
- Else
- Console.WriteLine("no data available")
- End If
- Rs232.DiscardInBuffer()
- End While
- End Sub
Display More
without the "sleep" i never (or rare) get data to read, because Read() is called before the data is written (38400 Baud !) and then data is discarded. I print out the data read in a console. The program works fine for hours! And the memory does not increase!
Do i overlook something?