Problem med variable i VB.NET
Hej,Jeg er ved at lave et program til der skal læse fra en TcpStream og skrive dette til en SerialPort. Dette virker som sådan udemærket den er bare lang tid om at skrive til SerialPorten hvis det er en variable der bliver sat.
Jeg har et script som jeg har indsat nedenunder.
Jeg erklærer variablen TcpReceived. og laver samtidig en Ny Thread_Tcp til selve Tcp løsningen.
Denne modtager fint kommandoøen fra mit program. Og sender også et svar tilbage med OK. Den skal så skrive til variablen TcpReceived og det gør den også men det er den meget lang tid om 3-4 sec umiddelbart. Fordi hvis jeg skriver til en system variable og læser dette så læser den med et samme når jeg senere læser fra en timer. Hvad gør jeg galt? hvorfor er den så lang tid om at skrive til variablen i programmet og ikke system variablen. Er det den Thread der har problemer med at skrive til variablen eller ihvertfald tager lang tid?
<code>
Public Class SerialCom_Service
Dim Thread_Tcp As New Thread(AddressOf Tcp)
Dim TcpListen As Boolean = False
Dim TcpReceived As String
Dim TcpSent As String
Public Sub New()
InitializeComponent()
If Not System.Diagnostics.EventLog.SourceExists("SerialCom") Then
System.Diagnostics.EventLog.CreateEventSource("SerialCom", "Application")
End If
EventLog1.Source = "SerialCom"
EventLog1.Log = "Application"
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
Timer1.Enabled = True
Timer1.Interval = My.Settings.strTimerInterval
TcpListen = True
Thread_Tcp.IsBackground = True
Thread_Tcp.Start()
With SerialPort1
.PortName = My.Settings.strPortName
.BaudRate = My.Settings.strBaudRate
.DataBits = My.Settings.strDataBits
.StopBits = My.Settings.strStopBits
.Parity = My.Settings.strParity
.ReceivedBytesThreshold = My.Settings.strRBThreshold
.RtsEnable = False
.DtrEnable = False
End With
Try
SerialPort1.Open()
Catch ex As Exception
EventLog1.WriteEntry("Error: " & ex.Message, EventLogEntryType.Error)
Throw
End Try
EventLog1.WriteEntry("SerialCom_Service started successfully." & vbCr & _
"PortName: " & My.Settings.strPortName & vbCr & _
"BaudRate: " & My.Settings.strBaudRate & vbCr & _
"DataBits: " & My.Settings.strDataBits & vbCr & _
"StopBits: " & My.Settings.strStopBits & vbCr & _
"Parity: " & My.Settings.strParity & vbCr & _
"IP: " & My.Settings.strIPAddress & vbCr & _
"Port: " & My.Settings.strPortNumber, EventLogEntryType.Information)
End Sub
Protected Overrides Sub OnStop()
Timer1.Enabled = False
Timer1.Interval = My.Settings.strTimerInterval
TcpListen = False
Try
SerialPort1.Close()
Catch ex As Exception
EventLog1.WriteEntry("Error: " & ex.Message, EventLogEntryType.Error)
Throw
End Try
EventLog1.WriteEntry("SerialCom_Service stopped successfully.", EventLogEntryType.Information)
End Sub
Private Sub Tcp()
Dim strIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(My.Settings.strIPAddress)
Dim strPortNumber As String = My.Settings.strPortNumber
Dim TcpListener As New TcpListener(strIPAddress, strPortNumber)
Try
TcpListener.Start()
Catch ex As Exception
EventLog1.WriteEntry("Error: " & ex.Message, EventLogEntryType.Error)
End Try
Do Until TcpListen = False
If TcpListener.Pending() Then
Try
Dim TcpClient As TcpClient = TcpListener.AcceptTcpClient()
Dim TcpStream As NetworkStream = TcpClient.GetStream()
Dim TcpReceiveBytes(TcpClient.ReceiveBufferSize) As Byte
TcpStream.Read(TcpReceiveBytes, 0, CInt(TcpClient.ReceiveBufferSize))
Dim TcpReceive As String = Encoding.ASCII.GetString(TcpReceiveBytes)
TcpReceived = TcpReceive
Environment.SetEnvironmentVariable("LINTRONIC", TcpReceive, EnvironmentVariableTarget.Machine)
Dim TcpSend As String = "OK"
Dim TcpSendBytes As [Byte]() = Encoding.ASCII.GetBytes(TcpSend)
TcpStream.Write(TcpSendBytes, 0, TcpSendBytes.Length)
TcpSent = TcpSend
TcpClient.Close()
Catch ex As Exception
EventLog1.WriteEntry("Error: " & ex.Message(), EventLogEntryType.Error)
End Try
End If
Thread.Sleep(100)
Loop
Try
TcpListener.Stop()
Catch ex As Exception
EventLog1.WriteEntry("Error: " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
EventLog1.WriteEntry("Received: " & SerialPort1.ReadExisting(), EventLogEntryType.Information)
End Sub
Private Sub Timer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Dim EnvReceived As String = Environment.GetEnvironmentVariable("LINTRONIC", EnvironmentVariableTarget.Machine)
If EnvReceived <> "" Then
' Code for Environment Variable commands.
SerialPort1.Write(EnvReceived)
EventLog1.WriteEntry("EnvTransmit: " & EnvReceived, EventLogEntryType.Information)
Environment.SetEnvironmentVariable("LINTRONIC", "", EnvironmentVariableTarget.Machine)
End If
If TcpReceived <> "" Then
' Code for TCP commands.
SerialPort1.Write(TcpReceived)
EventLog1.WriteEntry("TcpTransmit: " & TcpReceived, EventLogEntryType.Information)
TcpReceived = ""
End If
SerialPort1.DiscardInBuffer()
SerialPort1.DiscardOutBuffer()
End Sub
End Class
</code>