Jump to content

Recommended Posts

Posted (edited)

Hi Forum,

I am working on a project where I need to retrieve names from an Excel sheet and send them as XML data to a printer.

The problem is that if the names in the Excel sheet contain the Danish special characters like æ, ø, and å, no XML file is sent to the printer. 

I have tested the system by creating a log file, and here the XML data is correctly inserted. 

If I use the data from the log file and send it to the printer via a terminal program, it works.

This means that the problem occurs when I use the Send function in AU3.

 

Can anyone try to help me with this issue

#include <Array.au3>
#include <Excel.au3>
#include <File.au3>

;Global $sIPAddress = "192.168.0.102" ; Set the Variable $sIPAddress.
;Global $iPort = "9100" ;9100 ; Port used for the connection.
; Inifil FilePath = Arbejds Mappen + ZEBRA-Utillity.ini
Global Const $IniFilePath = (@ScriptDir & '\STG_XML.ini') ; File Navn på INI Filen "ZEBRA-Utillity.ini"

; Create application object
Local $oExcel = _Excel_Open()
Local $bReadOnly = False
Local $bVisible = True

Local Const $sFilePath = @WorkingDir & "/test1.txt"    ; Vælg Filnavn og Path hvor Fil skal gemmes.

; Register OnAutoItExit to be called when the script is closed.
OnAutoItExitRegister("OnAutoItExit")

$iIPAddress = IniRead($IniFilePath, "HM_Application", "IP_Address", "127.0.0.1")        ; Indlæs IP Address fra Ini Fil
$iSUBNet = IniRead($IniFilePath, "HM_Application", "SubNet-Mask", "255.255.255.0")        ; Indlæs IP Address fra Ini Fil
$iPort = IniRead($IniFilePath, "HM_Application", "PORT", "9100")                        ; Indlæs Port No. fra Ini Fil
$iColumn = IniRead($IniFilePath, "Exel_Ark", "Column_Read", "A")                        ; Indlæs Colonne Ident. fra Ini Fil
; ****************************************************************************
; Open an existing workbook and return its object identifier.
; *****************************************************************************
Local $sWorkbook = @ScriptDir & "\Temp.xlsx"          ; Selekt hvor Exel Fil er gemt
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook, $bReadOnly, $bVisible)
;Local $aResult = _Excel_RangeRead($oWorkbook, Default, "A1:B5", 1)
Local $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns($iColumn), 1)
_Excel_Close($oExcel, False, True) ; Does not save any open workbooks and closes the Excel instance
_ArrayDisplay($aResult)

#comments-start
    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
            Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
            If $hFileOpen = -1 Then
                    MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    ;                Return False
            EndIf
    ;_FileWriteFromArray($sFilePath, $aResult, 0)
    For $i = 0 To UBound($aResult) - 1
    FileWrite($hFileOpen, '<?xml version="1.0" encoding="utf-8" standalone="no"?>' & @CRLF)
    FileWrite($hFileOpen, '<labels _FORMAT="E:STG_XML.ZPL" _QUANTITY="1" _PRINTERNAME="P1" _JOBNAME="J1">' & @CRLF)
    FileWrite($hFileOpen, '  <label>' & @CRLF)
    FileWrite($hFileOpen, '    <variable name="XML-Navn">')
    FileWrite($hFileOpen, $aResult[$i])
    FileWrite($hFileOpen, '</variable>' & @CRLF)
    FileWrite($hFileOpen, '  </label>' & @CRLF)
    FileWrite($hFileOpen, '</labels>' & @CRLF)
    Next

#comments-end

Send_Data()

_Exit()

Func Send_Data()
    TCPStartup() ; Start the TCP service.

    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")

    ; Assign Local variables the loopback IP Address and the Port.
    ;    Local $sIPAddress = "192.168.0.102" ; 127.0.0.0 This IP Address only works for testing on your own computer.
    ;    Local $iPort = 9100 ; Port used for the connection.

    Opt("TCPTimeout", 1000)
    Local $nMaxTimeout = 10 ; script will abort if no server available after 10 secondes

    Local $iSocket, $iError

    While 1
        ; Assign a Local variable the socket and connect to a Listening socket with the IP Address and Port specified.
        $iSocket = TCPConnect($iIPAddress, $iPort)

        ; If an error occurred display the error code and return False.
        If @error = 10060 Then
            ; Timeout occurs try again
            $nMaxTimeout -= 1
            If $nMaxTimeout < 0 Then
                MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not connect, after " & 10 - $nMaxTimeout & " Sec. TimeOut")
                Return False
            EndIf
            ContinueLoop
        ElseIf @error Then
            $iError = @error
            ; The server is probably offline/port is not opened on the server.
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not connect, Error code: " & $iError)
            Return False
        Else
            ;            MsgBox($MB_SYSTEMMODAL, "", "Connection successful after " & 10 - $nMaxTimeout & " Sec.")
            ;_FileWriteFromArray($sFilePath, $aResult, 0)
            For $i = 0 To UBound($aResult) - 1
                TCPSend($iSocket, '<?xml version="1.0" encoding="utf-8" standalone="no"?>' & @CRLF)
                TCPSend($iSocket, '<labels _FORMAT="E:STG_NAME.ZPL" _QUANTITY="1" _PRINTERNAME="P1" _JOBNAME="J1">' & @CRLF)
                TCPSend($iSocket, '  <label>' & @CRLF)
                TCPSend($iSocket, '    <variable name="First_Name">')
                TCPSend($iSocket, $aResult[$i][0])
                TCPSend($iSocket, '</variable>' & @CRLF)
                TCPSend($iSocket, '    <variable name="Sec_Name">')
                TCPSend($iSocket, $aResult[$i][1])
                TCPSend($iSocket, '</variable>' & @CRLF)
                TCPSend($iSocket, '  </label>' & @CRLF)
                TCPSend($iSocket, '</labels>' & @CRLF)
            Next
            ; Sleep for 5 seconds.
            Sleep(1000)
            ExitLoop
        EndIf

    WEnd

    ; Close the socket.
    TCPCloseSocket($iSocket)
EndFunc   ;==>Send_Data

;Func Open_TCP()

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

Func _Exit()
    Exit 0
EndFunc   ;==>_Exit

#comments-start

    #include <GUIConstants.au3>
    TCPStartUp()
    Dim $szServerPC = @ComputerName
    Dim $szIPADDRESS = TCPNameToIP($szServerPC)
    Dim $nPORT = 85

    Dim $ConnectedSocket = -1

    $ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT & "/processXML.slap")

    MsgBox(0,"Test", $szIPADDRESS & ":" & $nPORT)
    Dim $szData

    $FileName = FileOpen("C:\request_1.xml",0)
    $XMLData = FileRead ($FileName)

    If @error Then
        MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
    Else
        MsgBox(0, "XML Data", $XMLData)
        TCPSend($ConnectedSocket,$XMLData)
        Sleep(100)
        $Response = TCPRecv($ConnectedSocket,1024)
        MsgBox(0, "XML Response", $Response);
    EndIf
    TCPCloseSocket($ConnectedSocket)

#comments-end

Read_EXEL-Write-TCP_test-2D-Array.au3

Edited by Jos
Added source in Codebox for easy viewing
  • Developers
Posted

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted

Hello,

you could use wireshark to dig into the communication with the printer (analyse packet content)

 

I suspect, that the encoding might be an issue: The printer is expecting UTF8.

 

So you might save the content, that has to be send to the printer to a file explicitely opened as UTF8 file, see help for FileOpen() the mode values.

 

Then try how to read that file's content into a variable, and transfer the content of this variable to your printer.

 

 

 

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

  • 2 weeks later...
Posted
Quote

It is correct that the printers use UTF8. However, my program generates an XML file which is sent to the printer, where the variables may contain special Danish characters. If I create an XML file and send it to the printer via a terminal program, the printer prints correctly. The problem is that AutoIT cannot send the Danish characters correctly. I have found a solution where I get AutoIT to generate a Temp.XML file, and then I call an FTP command that sends the file to the printer, allowing the printer to print the special characters. So, this shows that AutoIT can handle the special Danish characters, but it just cannot send them directly using the Send command.

 

Posted
3 minutes ago, LDK said:

The problem is that AutoIT cannot send the Danish characters correctly.

Untrue!

AutoIt can natively "send" (whatever that means) the UCS2 range of Unicode.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...