Similar to the last post I have also been asked to listen to a serial port and import the data into NAV. Â I even used the same VB.NET service. Â On the NAV side I added two functions to my web service.
Where RMSerial is a table with the following code.
Here is the VB.NET code for the service.
[code lang=”vb”]Imports System
Imports System.Timers
Imports System.Net
Imports System.IO
Imports System.IO.Ports
Public Class FileImportService
Dim Salvor1 As Salvor.SalvorWebService
Dim Timer2 As System.Timers.Timer
Dim User As New System.Net.NetworkCredential
Dim Serial1 As New System.IO.Ports.SerialPort
Protected Overrides Sub OnStart(ByVal args() As String)
‘ Add code here to start your service. This method should set things
‘ in motion so your service can do its work.
Salvor1 = New Salvor.SalvorWebService
User.Domain = "<Domain>"
User.UserName = "<User>"
User.Password = "<Password>"
Salvor1.Credentials = User
Timer2 = New System.Timers.Timer(30000)
AddHandler Timer2.Elapsed, AddressOf ProcessSerialData
Timer2.Interval = 30000
Timer2.Enabled = True
Timer2.Stop()
If My.Settings.COMPort <> "" Then
If Serial1.IsOpen Then
Serial1.Close()
End If
WriteDebug("COM Port Selected: " & My.Settings.COMPort)
Serial1.PortName = My.Settings.COMPort
Serial1.BaudRate = 9600
Serial1.Parity = Parity.Even
Serial1.DataBits = 8
Serial1.StopBits = 1
Serial1.Open()
If Serial1.IsOpen Then
WriteDebug("Serial Port Opened")
Else
WriteDebug("Failed to open Serial Port")
End If
AddHandler Serial1.DataReceived, AddressOf Serial1_DataReceived
End If
End Sub
Protected Overrides Sub OnStop()
‘ Add code here to perform any tear-down necessary to stop your service.
If My.Settings.COMPort <> "" Then
If Serial1.IsOpen Then
Serial1.Close()
End If
End If
End Sub
Protected Sub Serial1_DataReceived()
Timer2.Stop()
WriteDebug("Serial Event Started, buffer size: " & Serial1.ReadBufferSize)
Dim LineRead As String
Dim LineResponse As String = ""
Dim WaitLoop As Integer = 0
LineRead = Serial1.ReadExisting
If LineRead = "!" Then
LineResponse = "$"
ElseIf LineRead = "*" Then
LineResponse = "&"
ElseIf Left(LineRead, 1) = "[" Then
While Right(LineRead, 1) <> "]" And WaitLoop < 10
Threading.Thread.Sleep(100)
LineRead = LineRead & Serial1.ReadExisting
WaitLoop = WaitLoop + 1
End While
If Right(LineRead, 1) = "]" Then
Try
If Salvor1.AboutCompany = "SAM" Then
LineResponse = Salvor1.COMPort(LineRead)
End If
Catch ex As Exception
LineResponse = "%"
WriteToEventLog("Web Service unavailable:" & ex.ToString, EventLogEntryType.Error)
End Try
Else
LineResponse = "%"
End If
End If
Serial1.Write(LineResponse)
WriteDebug("Serial read: " & LineRead)
WriteDebug("Serial response: " & LineResponse)
Timer2.Interval = 30000
Timer2.Enabled = True
Timer2.Start()
End Sub
Protected Sub ProcessSerialData()
Timer2.Enabled = False
Timer2.Stop()
Dim Success As Boolean
Try
Success = Salvor1.ProcessSerialData
If Not Success Then
WriteToEventLog("Failed to process serial data", EventLogEntryType.Error)
End If
Catch ex As Exception
WriteToEventLog("Web Service unavailable:" & ex.ToString, EventLogEntryType.Error)
End Try
End Sub
Protected Sub WriteToEventLog(ByVal Message As String, ByVal EntryType As EventLogEntryType)
Dim MyLog As New EventLog()
‘ Check if the the Event Log Exists
If Not Diagnostics.EventLog.SourceExists(Me.ServiceName) Then
Diagnostics.EventLog.CreateEventSource(Me.ServiceName, Me.ServiceName & " Log")
‘ Create Log
End If
MyLog.Source = Me.ServiceName
‘ Write to the Log
Diagnostics.EventLog.WriteEntry(MyLog.Source, Message, EntryType)
End Sub
Protected Sub WriteDebug(ByVal Message As String)
If My.Settings.Debug Then
WriteToEventLog(Message, EventLogEntryType.Information)
End If
End Sub
End Class
[/code]