f7c7790d90
- aggiunta gestione eccezione per stampante non presente.
207 lines
6.9 KiB
VB.net
207 lines
6.9 KiB
VB.net
Imports System.IO
|
|
Imports System.Text
|
|
Imports System.Threading.Tasks
|
|
Imports Zebra.Sdk.Comm
|
|
Imports Zebra.Sdk.Printer
|
|
Imports Zebra.Sdk.Printer.Discovery
|
|
|
|
Class ZebraPrinter
|
|
|
|
Private m_FileLog As String
|
|
Private m_Values As New Dictionary(Of String, String)
|
|
Private m_LabelBytes As Byte()
|
|
Private m_printerConnection As Connection = Nothing
|
|
Private m_sDataRoot As String
|
|
Private m_Waiting As Boolean = True
|
|
|
|
' dichiarazione funzione per lettura file ini
|
|
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (
|
|
ByVal lpAppName As String,
|
|
ByVal lpKeyName As String,
|
|
ByVal lpDefault As String,
|
|
ByVal lpReturnedString As StringBuilder,
|
|
ByVal nSize As Integer,
|
|
ByVal lpFileName As String) As Integer
|
|
|
|
|
|
' inizilizzo il file di log inserendo la data dell'esecuzione
|
|
Sub InitLog()
|
|
' Impostazione path radice per i dati
|
|
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
|
|
Dim sb = New StringBuilder(500)
|
|
If GetPrivateProfileString("Data", "DataRoot", "", sb, sb.Capacity, m_sDataRoot & "DataRoot.ini") = 0 Then
|
|
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
|
|
Else
|
|
m_sDataRoot = sb.ToString
|
|
End If
|
|
m_FileLog = m_sDataRoot & "\Temp\ZebraPrinterUtilityLog.txt"
|
|
If Not File.Exists(m_FileLog) Then
|
|
Try
|
|
File.Create(m_FileLog).Close()
|
|
Catch ex As Exception
|
|
End Try
|
|
End If
|
|
Try
|
|
Dim sMessage As StreamWriter = File.AppendText(m_FileLog)
|
|
sMessage.WriteLine(DateTime.Now.ToString)
|
|
sMessage.Close()
|
|
Catch ex As Exception
|
|
End Try
|
|
|
|
End Sub
|
|
|
|
' Funzione per scrivere nel file di Log
|
|
Sub EmitLog(sLine As String)
|
|
Dim sMessage As StreamWriter = File.AppendText(m_FileLog)
|
|
sMessage.WriteLine(sLine)
|
|
sMessage.Close()
|
|
End Sub
|
|
|
|
' collego la stampante
|
|
Friend Function ConnectPrinter() As Boolean
|
|
Try
|
|
Dim discoPrinters As List(Of DiscoveredPrinterDriver) = UsbDiscoverer.GetZebraDriverPrinters()
|
|
m_printerConnection = New DriverPrinterConnection(discoPrinters(0).PrinterName)
|
|
Return True
|
|
Catch ex As ConnectionException
|
|
EmitLog("Printer connection error: " & ex.Message)
|
|
Return False
|
|
Catch
|
|
EmitLog("Printer not found error")
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
' creo e stampo l'etichetta (ricevo il nome del template da usare)
|
|
Public Async Sub Print(FilePath As String)
|
|
Await Task.Run(Async Function()
|
|
|
|
Try
|
|
' Connecting...
|
|
m_printerConnection.Open()
|
|
EmitLog("Connessione stampante: APERTA")
|
|
' Sending Data...
|
|
Dim LabelFile As String = File.ReadAllText(FilePath)
|
|
For Each Variable In m_Values
|
|
LabelFile = LabelFile.Replace(Variable.Key, Variable.Value)
|
|
Next
|
|
Dim LabelBytes As Byte() = Encoding.UTF8.GetBytes(LabelFile)
|
|
Await Task.Delay(200)
|
|
' stampo etichetta
|
|
m_printerConnection.Write(LabelBytes)
|
|
m_Waiting = False
|
|
EmitLog("Etichetta pronta")
|
|
Catch __unusedConnectionException1__ As ConnectionException
|
|
EmitLog(__unusedConnectionException1__.ToString)
|
|
' Communications Error
|
|
Catch __unusedZebraPrinterLanguageUnknownException2__ As ZebraPrinterLanguageUnknownException
|
|
EmitLog(__unusedZebraPrinterLanguageUnknownException2__.ToString)
|
|
End Try
|
|
|
|
' Chiudo la connessione
|
|
Try
|
|
'Threading.Thread.Sleep(500)
|
|
Threading.Thread.Sleep(200)
|
|
' Disconnecting...
|
|
If m_printerConnection IsNot Nothing Then
|
|
m_printerConnection.Close()
|
|
EmitLog("Connessione stampante: CHIUSA")
|
|
Threading.Thread.Sleep(1000)
|
|
End If
|
|
' Not Connected
|
|
Catch __unusedConnectionException1__ As ConnectionException
|
|
EmitLog(__unusedConnectionException1__.ToString)
|
|
End Try
|
|
|
|
End Function)
|
|
End Sub
|
|
|
|
|
|
' Lettura file ini
|
|
Private Function ReadValue(FilePath As String) As Boolean
|
|
' verifico che il file esiste
|
|
If Not File.Exists(FilePath) Then
|
|
EmitLog("File '" & FilePath & "' non esiste.")
|
|
Return False
|
|
End If
|
|
|
|
Dim sb = New StringBuilder(500)
|
|
Dim Index As Integer = 1
|
|
Dim nResult As Integer = -1
|
|
' leggo l'elenco delle variabili
|
|
While nResult <> 0
|
|
Try
|
|
nResult = GetPrivateProfileString("Main", "Var" & Index.ToString, "", sb, sb.Capacity, FilePath)
|
|
Catch ex As Exception
|
|
EmitLog(ex.ToString)
|
|
Index = Index + 1
|
|
Continue While
|
|
End Try
|
|
Dim sItems As String() = sb.ToString.Split(","c)
|
|
If sItems.Count = 2 Then
|
|
m_Values.Add(sItems(0).Trim, sItems(1).Trim)
|
|
End If
|
|
Index = Index + 1
|
|
End While
|
|
|
|
EmitLog("Dati caricati")
|
|
Return True
|
|
End Function
|
|
|
|
Sub New(args As String())
|
|
|
|
InitLog()
|
|
ConnectPrinter()
|
|
|
|
' verifico che il numero di argomenti sia superiore a 2 (migliorare l'interpretazione dell'errore)
|
|
If args.Length < 2 Then
|
|
EmitLog("Non sono stati esplicictati tutti i file")
|
|
Return
|
|
End If
|
|
|
|
EmitLog("File template: " & args(0))
|
|
' verifico che l'estensione del file sia corretta
|
|
If Path.GetExtension(args(0)) <> ".prn" Then
|
|
EmitLog("Estensione file template non corretta, diversa da .prn")
|
|
Return
|
|
End If
|
|
' verifico che il file esiste
|
|
If Not File.Exists(args(0)) Then
|
|
EmitLog("File '" & args(0) & "' non esiste.")
|
|
End If
|
|
|
|
EmitLog("File dati: " & args(1))
|
|
' verifico che l'estensione del file sia corretta
|
|
If Path.GetExtension(args(1)) <> ".ini" Then
|
|
EmitLog("Estensione file dati non corretta, diversa da .ini")
|
|
Return
|
|
End If
|
|
|
|
' leggo l'elenco dei dati da stampare
|
|
If Not ReadValue(args(1)) Then Return
|
|
' stampo etichetta
|
|
Print(args(0))
|
|
|
|
' metto il programmma in attesa
|
|
Dim nCiclo As Integer = 0
|
|
While m_Waiting And nCiclo < 6
|
|
Threading.Thread.Sleep(1000)
|
|
nCiclo += 1
|
|
End While
|
|
' elimino il file dei dati
|
|
Try
|
|
File.Delete(args(1))
|
|
EmitLog("Eliminato file " & args(1))
|
|
Catch ex As Exception
|
|
End Try
|
|
End Sub
|
|
|
|
|
|
End Class
|
|
|
|
|
|
|
|
|
|
|
|
|