Results 1 to 14 of 14

Thread: Need to open a network cash drawer

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Need to open a network cash drawer

    Not a programmer but need to create a utility to open a network cash drawer.
    We are switching our Point of Sale app and require new cash drawers.
    New drawers will work fine with new app but not the old POS application.

    Each drawer will have an assigned IP address. I need to create a separate utility to open the drawer at each workstation while we are using both applications.

    APG, the drawer manufacturer provided me with the following


    1. Connect to the Cash Drawer
    Private Sub CashDrawerConnect_Click()
    'Close prior to connect (OK)
    Winsock1.Close
    'Establish connection to device
    'ipaddr = "192.168.2.5" 'APGDefaultStaticIPAddress
    Winsock1.RemoteHost = ipaddr
    Winsock1.RemotePort = 30998 'RequiredPort
    Winsock1.Connect

    Sleep 250
    TxtOpStatus = "Connection to the cash drawer at " &ipaddr& " is established..."
    TxtOpStatus.Refresh
    End Sub


    2. Open the Cash Drawer
    Private Sub CashDrawerOpen_Click()
    ' DrawerOpen (Kick)
    If Winsock1.State = sckConnected Then

    Winsock1.SendData "opendrawer\0a"
    Else
    TxtOpStatus = "Not connected to the device"
    TxtOpStatus.Refresh
    End If
    End Sub




    I have Visual Studio 2019 but I have never created a Visual Basic program.

    Anyone care to take a shot at teaching me the basics?

    Utility needs to run under Windows 7 32 bit

    Bob

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Need to open a network cash drawer

    This question should probably really have been in the VB.Net forum. You would no doubt have several responses by now.

    I don't have the time to get specific right now, but Assuming you just want a form where you can press a button to open the drawer, it should be quite easy. The code above looks mostly like it is is legacy VB (VB 6), so you would not use it with VB.Net (VS 2019).
    This particular line: Winsock1.SendData "opendrawer\0a" : looks a bit suspect. I don't think you can insert a linefeed character at the end of the string that way in VB6. That would be something you can do in another language. If you can do it in VB6, I will have learned something.

    In any case, something that should work in VS 2019 using VB, would be the following. You can add more to it, i.e. they have a textbox for status output, but I didn't do that in this example. this just does the socket setup and send.

    I just create an instance of a UDPClient, and use it.

    I did add two buttons, but since the socket is opened at creation and UDP is not a connection type interface, the first button shouldn't even be necessary.

    The above code is set up for two buttons, one to connect and one to open the drawer. Once connected you shouldn't need the connect button unless you have to reconnect for some reason. I guess that is why they close the socket, in case you press the button while you're already connected. I'm following that method in this example, although I probably wouldn't bother with the first button myself.

    Just add two buttons to a form and paste in the following code (replacing the existing generated code for the form) to try it out.
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    
    Public Class Form1
        Dim udpSender As New UdpClient(0)
        Dim destIPnt As New IPEndPoint(IPAddress.Parse("192.168.2.5"), 30998) 'Send to UDP port 30998 
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Connect
            udpSender.Close()
            udpSender.Dispose()
            udpSender = New UdpClient(0)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'OpenDrawer
            Dim openDrawer As Byte() = System.Text.Encoding.ASCII.GetBytes("opendrawer" & vbLf)
            udpSender.Send(openDrawer, openDrawer.Length, destIPnt)
        End Sub
    End Class
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Need to open a network cash drawer

    It doesn't really look like .NET, to me. Which tool are you wanting to write this in?
    My usual boring signature: Nothing

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Need to open a network cash drawer

    For the record, the sample code posted by the OP is VB6.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: Need to open a network cash drawer

    Passel

    Thank you for the response.

    Is it possible to build it simply as an executable without any buttons?

    Bob

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Need to open a network cash drawer

    Quote Originally Posted by Shaggy Hiker View Post
    It doesn't really look like .NET, to me. Which tool are you wanting to write this in?
    I have Visual Studio 2019 but I have never created a Visual Basic program.
    I agree the example given to the OP is VB6, except as I said, I'm not aware of escaping a hex value inside a string as the example did, so I think it might the person giving the example may be using another language as their primary (as I do too, technically).
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Need to open a network cash drawer

    Yes. But how do you want it to know when to open the cash drawer?
    Do you just want to execute it to open the drawer, and it just exits after sending the string?

    Assuming that is true, then just choose to create a console application.
    That code could look like this:
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    
    Module Module1
    
        Sub Main()
            Dim udpSender As New UdpClient(0)
            Dim destIPnt As New IPEndPoint(IPAddress.Parse("192.168.2.5"), 30998) 'Send to UDP port 30998 
            Dim openDrawer As Byte() = System.Text.Encoding.ASCII.GetBytes("opendrawer" & vbLf)
            udpSender.Send(openDrawer, openDrawer.Length, destIPnt)
            Threading.Thread.Sleep(250)
            udpSender.Close()
            udpSender.Dispose()
        End Sub
    
    End Module
    Last edited by passel; Dec 22nd, 2020 at 03:03 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Need to open a network cash drawer

    Since you mentioned a number of drawers with different IP addresses, then perhaps if a non-gui (i.e. console) application is what you want to be kicked off, then perhaps rather than create one for each drawer, you would like to pass the IP address to the application.
    For instance, from a command line I could do the following.

    .\SendUDPmsg 192.168.2.5

    The code would have to parse the IP address from the command line arguments array, minimally like this.
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    
    Module Module1
    
        Sub Main()
            Dim ipAdx As IPAddress
    
            If My.Application.CommandLineArgs().Count = 1 Then
                ipAdx = IPAddress.Parse(My.Application.CommandLineArgs(0))
                Dim udpSender As New UdpClient(0)
                Dim destIPnt As New IPEndPoint(ipAdx, 30998) 'Send to UDP port 30998 
                Dim openDrawer As Byte() = System.Text.Encoding.ASCII.GetBytes("opendrawer" & vbLf)
                udpSender.Send(openDrawer, openDrawer.Length, destIPnt)
                Threading.Thread.Sleep(250)
                udpSender.Close()
                udpSender.Dispose()
            End If
        End Sub
    
    End Module
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: Need to open a network cash drawer

    Passel

    Passing the IP address with the command is a cleaner option than creating 7 different files.
    However I haven't gotten your code to work yet so still some work for me to do.

    Perhaps it does need to initial Connect before opening.
    I will let you know what I come up with

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Need to open a network cash drawer

    I guess that may be my mistake. It is doing a connect so is using TCP (which is the default for the vb6 winsock control) not UDP. I do a lot of UDP, and a few cases of TCP (since most of my network stuff is local, not Internet), so I assumed UDP for this simple application, without looking closely at the code.

    So, instead of UDPClient, we need to switch to TCPClient.

    That won't be as easy for me to test, as I would need to write a TCP server to represent the drawer to accept the connection. For now, I'll just give you an example of the Client Side and hope it will connect and communicate correctly.

    I'm on a different machine now, but I think I want to work on the same machine I was on before, so I'll post this now, so I don't have to retype or send it to the other machine by another method. Then I'll check out a TCPClient example, and post it here (in this post) shortly.

    I'm back... on the other machine.
    It isn't as easy to setup the wireshark filter to see the packet, because of the connection and handshake that has to take place before the data is sent.

    But I did have an example TCP Server, so I just changed the port it was listening to for connections, and started it up, and then ran an executable of the below code, and it connected and the server printed out the received data, so the code below works, i.e. it connects and transfers the data, and then exits.

    Code:
    Imports System.Net
    Imports System.Net.Sockets
    
    Module Module1
    
        Sub Main()
    
            Dim ipAdx As IPAddress
    
            If My.Application.CommandLineArgs().Count = 1 Then
                ipAdx = IPAddress.Parse(My.Application.CommandLineArgs(0))
                Dim tcpNetStream As NetworkStream
                Dim client As New TcpClient
    
                client.Connect(ipAdx, 30998)
                Threading.Thread.Sleep(250)
    
                tcpNetStream = client.GetStream
    
                Dim openDrawer As Byte() = System.Text.Encoding.ASCII.GetBytes("opendrawer" & vbLf)
                tcpNetStream.Write(openDrawer, 0, openDrawer.Length)
                Threading.Thread.Sleep(250)
    
                tcpNetStream.Close()
                tcpNetStream.Dispose()
                client.Close()
                client.Dispose()
            End If
    
        End Sub
    
    End Module
    Last edited by passel; Dec 22nd, 2020 at 07:18 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: Need to open a network cash drawer

    Passel

    Sorry about the delay in responding.
    Got shutdown and kicked out until this morning.

    Still having issues and will continue to show my ignorance.
    Created a new Console App (.NET Core)
    Called it Cashdraw
    Pasted your code
    Build and Run

    Following error received
    Build started...
    1>------ Build started: Project: Cashdraw, Configuration: Debug Any CPU ------
    1>C:\Users\MCounter3\source\repos\cashdraw\Cashdraw\Program.vb(10,12): error BC30456: 'Application' is not a member of 'Cashdraw.My'.
    1>C:\Users\MCounter3\source\repos\cashdraw\Cashdraw\Program.vb(11,37): error BC30456: 'Application' is not a member of 'Cashdraw.My'.
    1>Done building project "Cashdraw.vbproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Sorry this has turned into such a PITA.

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Need to open a network cash drawer

    Yeah, well I haven't worked with .Net Core, so I don't recognize those errors.
    The code I wrote and tested was for a .Net framework Console App, not .Net Core.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  13. #13

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: Need to open a network cash drawer

    Passel

    Thank you

    Put your code in to .Net Framework
    Got error on client.Dispose()
    Deleted that line figuring that it was just good housekeeping on your behalf.

    Built file and "IT WORKS"

    Don't know how I can thank you.
    I have taken a new interest in VB and will force myself to learn as much as I can.
    Probably won't use it at my late age but I will learn.

    Thank you, Thank you, Thank you

  14. #14
    Banned
    Join Date
    Jan 2021
    Location
    USA
    Posts
    25

    Re: Need to open a network cash drawer

    Hi
    Thanks for sharing quality information, really appreciating.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width