Click here to Skip to main content
16,005,121 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: how to resize the richtextbox as well as the font size Pin
The ANZAC2-Aug-07 22:09
The ANZAC2-Aug-07 22:09 
GeneralRe: how to resize the richtextbox as well as the font size Pin
eyes20072-Aug-07 22:16
eyes20072-Aug-07 22:16 
GeneralRe: how to resize the richtextbox as well as the font size Pin
The ANZAC2-Aug-07 23:19
The ANZAC2-Aug-07 23:19 
Questionprint preview Pin
md_refay2-Aug-07 21:54
md_refay2-Aug-07 21:54 
AnswerRe: print preview Pin
eyes20072-Aug-07 22:09
eyes20072-Aug-07 22:09 
QuestionMemory Leak Pin
Budi_Sanuri2-Aug-07 21:07
Budi_Sanuri2-Aug-07 21:07 
AnswerRe: Memory Leak Pin
Christian Graus2-Aug-07 21:22
protectorChristian Graus2-Aug-07 21:22 
GeneralRe: Memory Leak Pin
Budi_Sanuri3-Aug-07 19:34
Budi_Sanuri3-Aug-07 19:34 
Hi ...
Thanks to give me some comment.
I was call Dispose function to release any object that i created... but it my code still increase usage memory. in 8 hour, my memory increase 100 KBytes.
Actually i created some service application to accessing a serial port.

Here some code that i used to read a serial port continously:

'------------------------------------------------------------------------
Public Class PLC_Com

Dim MySerial As New System.IO.Ports.SerialPort
Dim MyTimer As System.Threading.Timer

Dim DataByte As Byte
Dim DataResult As String
Dim ReadStatus, WriteStatus As Integer

' Base Setting
Const def_ProtocolName As String = "Dedicated"
Const def_PortName As String = "COM1"
Const def_BaudRate As Integer = 57600
Const def_DataBits As Integer = 8
Const def_StopBits As Integer = 1 ' System.IO.Ports.StopBits.One
Const def_Parity As String = "None" ' System.IO.Ports.Parity.None
Const def_Handshake As String = "None" ' System.IO.Ports.Handshake.None
Const def_TimerInterval As Integer = 1000
Const def_WriteReadInterval As Integer = 100
Const def_RegCapacity As Integer = 1000
Const ACK As Char = Chr(6)
Const NAK As Char = Chr(21)

' Loading User Setting
Dim usr_ProtocolName As String = Nothing
Dim usr_PortName As String = Nothing
Dim usr_BaudRate As Integer = Nothing
Dim usr_DataBits As Integer = Nothing
Dim usr_StopBits As Integer = Nothing
Dim usr_Parity As String = Nothing
Dim usr_Handshake As String = Nothing
Dim usr_TimerInterval As Integer = Nothing
Dim usr_WriteReadInterval As Integer = Nothing
Dim usr_RegCapacity As Integer = Nothing

' System address
Dim ComStatus As String = Nothing
Dim RegIOCount As Integer = 0
Dim RegIOCountBuff As Integer = 0
Dim RegIOArray(3, 1) As String 'RegIOArray format (Command, Type, Value)
Const iRegCmd As Integer = 0
Const iRegMode As Integer = 1
Const iRegVal As Integer = 2

Dim i As Integer = 0
Dim BuffStr As String = Nothing

Public Sub New()
MyBase.New()
' This call is required by the Windows Form Designer.
InitializeComponent()
End Sub

'------------------------------------------------------------------------
' My Service start
'------------------------------------------------------------------------

Protected Overrides Sub OnStart(ByVal args() As String)
' Initilize Service Parameter, Reinitialize PLC IO Register buffer
Call Load_ServiceSetting()
ReDim RegIOArray(3, usr_RegCapacity)
Call Load_IO_Configuration()
' Delegating Timer event
System.IO.File.AppendAllText("C:\ServiceLog.txt", "MyService was started at : " & Now.ToString() & vbCrLf)
Dim tDelegate As Threading.TimerCallback = AddressOf EventAction
' Activation a timer
MyTimer = New System.Threading.Timer(tDelegate, Me, 0, usr_TimerInterval)
End Sub

Protected Overrides Sub OnStop()
System.IO.File.AppendAllText("C:\ServiceLog.txt", "MyService was stoped at : " & Now.ToString() & vbCrLf)
End Sub


'----------------------------------------------------------------
' FUNCTION SECTION
'----------------------------------------------------------------
Dim j As Integer = 0

Public Sub EventAction(ByVal sender As Object)
For RegIOCountBuff = 0 To RegIOCount - 1
If RegIOArray(iRegMode, RegIOCountBuff) = "R" Then
ComStatus = Read_PLC_Reg(RegIOArray(iRegCmd, RegIOCountBuff))
RegIOArray(iRegVal, RegIOCountBuff) = ComStatus
End If
If RegIOArray(iRegMode, RegIOCountBuff) = "W" Then
If j = 1000 Then j = 0 Else j += 1
RegIOArray(iRegVal, RegIOCountBuff) = CStr(j)
ComStatus = Write_PLC_Reg(RegIOArray(iRegCmd, RegIOCountBuff), j)
End If
Select Case ComStatus
Case "Error0"
System.IO.File.AppendAllText("C:\ServiceLog.txt", "NAK detected in PLC response with command " & RegIOArray(iRegCmd, RegIOCountBuff) & " at : " & Now.ToString() & vbCrLf)
Case "Error1"
System.IO.File.AppendAllText("C:\ServiceLog.txt", "Length of PLC response Not Valid with command " & RegIOArray(iRegCmd, RegIOCountBuff) & " at : " & Now.ToString() & vbCrLf)
Case "Error2"
System.IO.File.AppendAllText("C:\ServiceLog.txt", "No response from PLC with command " & RegIOArray(iRegCmd, RegIOCountBuff) & " at : " & Now.ToString() & vbCrLf)
Me.Stop()
Case "Error3"
System.IO.File.AppendAllText("C:\ServiceLog.txt", "Error while reading SerialPort at : " & Now.ToString() & vbCrLf)
Me.Stop()
End Select
System.Threading.Thread.Sleep(50)
Next
End Sub

Private Function Read_PLC_Reg(ByVal CmdString As String) As String
Try
DataResult = ""
If MySerial.IsOpen = False Then MySerial.Open()
MySerial.Write(Chr(5) & CmdString & Chr(4))
System.Threading.Thread.Sleep(usr_WriteReadInterval)
If MySerial.BytesToRead > 0 Then
For i = 0 To MySerial.BytesToRead - 1
DataByte = MySerial.ReadByte
DataResult += Chr(DataByte)
Next
If DataResult.Length > 14 Then
If Microsoft.VisualBasic.Left(DataResult, 1) <> NAK Then
Return Microsoft.VisualBasic.Mid(DataResult, 11, 4)
Else
Return "Error0"
Exit Try
End If
Else
Return "Error1"
Exit Try
End If
Else
Return "Error2"
Exit Try
End If
Catch ex As Exception
If MySerial.IsOpen Then MySerial.Close()
Return "Error3"
Exit Try
End Try
End Function

Private Function Write_PLC_Reg(ByVal CmdString As String, ByVal RegValue As String) As String
Try
DataResult = ""
If MySerial.IsOpen = False Then MySerial.Open()
MySerial.Write(Chr(5) & CmdString & StrTo4Str(Hex(RegValue)) & Chr(4))
System.Threading.Thread.Sleep(usr_WriteReadInterval)
If MySerial.BytesToRead > 0 Then
For i = 0 To MySerial.BytesToRead - 1
DataByte = MySerial.ReadByte
DataResult += Chr(DataByte)
Next
If DataResult.Length > 6 Then
If Microsoft.VisualBasic.Left(DataResult, 1) <> NAK Then
Return "OK"
Else
Return "Error0"
Exit Try
End If
Else
Return "Error1"
End If
Else
Return "Error2"
Exit Try
End If
Catch ex As Exception
If MySerial.IsOpen Then MySerial.Close()
Return "Error3"
Exit Try
End Try
End Function

Sub Load_ServiceSetting()
BuffStr = ""
Try
' Reading User setting from file
Dim FileSetting As New System.IO.StreamReader("C:\ServiceSetting.txt")
BuffStr = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
usr_ProtocolName = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
usr_PortName = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
usr_BaudRate = CInt(FileSetting.ReadLine)
BuffStr = FileSetting.ReadLine
usr_DataBits = CInt(FileSetting.ReadLine)
BuffStr = FileSetting.ReadLine
usr_StopBits = CInt(FileSetting.ReadLine)
BuffStr = FileSetting.ReadLine
usr_Parity = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
usr_Handshake = FileSetting.ReadLine
BuffStr = FileSetting.ReadLine
usr_TimerInterval = CInt(FileSetting.ReadLine)
BuffStr = FileSetting.ReadLine
usr_WriteReadInterval = CInt(FileSetting.ReadLine)
BuffStr = FileSetting.ReadLine
usr_RegCapacity = CInt(FileSetting.ReadLine)
FileSetting.Close()
FileSetting.Dispose()
Catch ex As Exception
usr_ProtocolName = def_ProtocolName
usr_PortName = def_PortName
usr_BaudRate = def_BaudRate
usr_DataBits = def_DataBits
usr_StopBits = def_StopBits
usr_Parity = def_Parity
usr_Handshake = def_Handshake
usr_TimerInterval = def_TimerInterval
usr_WriteReadInterval = def_WriteReadInterval
usr_RegCapacity = def_RegCapacity
System.IO.File.AppendAllText("C:\ServiceLog.txt", "Loading SerialPort deafault configuration at : " & Now.ToString() & ". Error : " & ex.ToString & vbCrLf)
Finally
' Seting up Serial Port parameter
MySerial.PortName = usr_PortName
MySerial.BaudRate = usr_BaudRate
MySerial.DataBits = usr_DataBits
Select Case usr_StopBits
Case 0 : MySerial.StopBits = System.IO.Ports.StopBits.None
Case 1 : MySerial.StopBits = System.IO.Ports.StopBits.One
Case 2 : MySerial.StopBits = System.IO.Ports.StopBits.Two
Case Else
MySerial.StopBits = System.IO.Ports.StopBits.None
End Select
Select Case usr_Parity
Case "None" : MySerial.Parity = System.IO.Ports.Parity.None
Case "Odd" : MySerial.Parity = System.IO.Ports.Parity.Odd
Case "Even" : MySerial.Parity = System.IO.Ports.Parity.Even
Case Else
MySerial.Parity = System.IO.Ports.Parity.None
End Select
Select Case usr_Handshake
Case "None" : MySerial.Handshake = System.IO.Ports.Handshake.None
Case "RequestToSend" : MySerial.Handshake = System.IO.Ports.Handshake.RequestToSend
Case "RequestToSendXOnXOff" : MySerial.Handshake = System.IO.Ports.Handshake.RequestToSendXOnXOff
Case Else
MySerial.Handshake = System.IO.Ports.Handshake.None
End Select
MySerial.Encoding = System.Text.Encoding.Default
System.IO.File.AppendAllText("C:\ServiceLog.txt", "SerialPort was initialized at : " & Now.ToString() & vbCrLf)
End Try
End Sub

Private Sub Load_IO_Configuration()
BuffStr = ""
RegIOCount = 0
Try
' Reading PLC IO Register from file
Using FileIOReg As System.IO.StreamReader = New System.IO.StreamReader("C:\ServiceIO.txt")
BuffStr = FileIOReg.ReadLine()
Do
BuffStr = FileIOReg.ReadLine()
If (BuffStr <> Nothing) Or (BuffStr <> "") Then
RegIOArray(iRegCmd, RegIOCount) = BuffStr
RegIOCount += 1
End If
Loop Until BuffStr Is Nothing
FileIOReg.Close()
FileIOReg.Dispose()
For i = 0 To RegIOCount - 1
RegIOArray(iRegMode, i) = Microsoft.VisualBasic.Mid(RegIOArray(0, i), 3, 1)
Next
End Using
System.IO.File.AppendAllText("C:\ServiceLog.txt", "PLC IO Register was initialized at : " & Now.ToString() & vbCrLf)
Catch ex As Exception
System.IO.File.AppendAllText("C:\ServiceLog.txt", ex.ToString & vbCrLf) '"Error while reading PLC IO Register "
End Try
End Sub

Private Function StrTo4Str(ByVal Data As String) As String
Dim Hasil As String = ""
Select Case Len(Data)
Case 1 : Hasil = "000" & Data
Case 2 : Hasil = "00" & Data
Case 3 : Hasil = "0" & Data
Case 4 : Hasil = Data
End Select
Return Hasil
End Function


End Class


the procedure :
Call Load_ServiceSetting()
ReDim RegIOArray(3, usr_RegCapacity)
Call Load_IO_Configuration()
is call when my service run fist time.
AnswerRe: Memory Leak Pin
Luc Pattyn2-Aug-07 23:35
sitebuilderLuc Pattyn2-Aug-07 23:35 
AnswerRe: Memory Leak Pin
originSH3-Aug-07 0:25
originSH3-Aug-07 0:25 
GeneralRe: Memory Leak Pin
Luc Pattyn3-Aug-07 1:10
sitebuilderLuc Pattyn3-Aug-07 1:10 
GeneralRe: Memory Leak Pin
originSH3-Aug-07 1:44
originSH3-Aug-07 1:44 
GeneralRe: Memory Leak Pin
Budi_Sanuri3-Aug-07 19:36
Budi_Sanuri3-Aug-07 19:36 
AnswerRe: Memory Leak Pin
Rizwan Bashir3-Aug-07 21:54
Rizwan Bashir3-Aug-07 21:54 
GeneralRe: Memory Leak Pin
Budi_Sanuri4-Aug-07 4:15
Budi_Sanuri4-Aug-07 4:15 
QuestionVB.Net, Pin
BalRaj Thoma2-Aug-07 21:02
BalRaj Thoma2-Aug-07 21:02 
AnswerRe: VB.Net, Pin
Christian Graus2-Aug-07 21:23
protectorChristian Graus2-Aug-07 21:23 
Question.net framework downloading Pin
Sonia Gupta2-Aug-07 20:40
Sonia Gupta2-Aug-07 20:40 
AnswerRe: .net framework downloading Pin
ne0h2-Aug-07 20:49
ne0h2-Aug-07 20:49 
GeneralRe: .net framework downloading Pin
Sonia Gupta2-Aug-07 21:03
Sonia Gupta2-Aug-07 21:03 
GeneralRe: .net framework downloading Pin
ne0h2-Aug-07 21:18
ne0h2-Aug-07 21:18 
GeneralRe: .net framework downloading Pin
Sonia Gupta2-Aug-07 21:35
Sonia Gupta2-Aug-07 21:35 
GeneralRe: .net framework downloading Pin
ne0h3-Aug-07 0:01
ne0h3-Aug-07 0:01 
QuestionHow do i creat the look and feel similar to the forms in windows vista. Pin
ozzyrocsdbn2-Aug-07 20:40
ozzyrocsdbn2-Aug-07 20:40 
QuestionRegarding hiding datagrid columns in vb.net Pin
choorakkuttyil2-Aug-07 19:46
choorakkuttyil2-Aug-07 19:46 

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.