Page 1 of 1

Socket Commication Help

Posted: Saturday 1st May 2021 7:56pm
by AndyGable
Hi Everyone

I am trying to talk to another program of mine via Socket

I have I think the server working correctly but when I run the code I get status number of 5

This is the code
ClientConnection = New Socket

    With ClientConnection
        .Host = "127.0.0.1"
        .Port = 9001
        .Connect
    End With

    If ClientConnection.Status = Net.Inactive Then
        Print "No Connection"
        Wait 1

        If ClientConnection.Status = Net.Connected Then
            Write #ClientConnection, DataToSend, Len(DataToSend)
            Print "Sending to server : " & DataToSend
        Else
            Close #ClientConnection
            Print "Sorry Time out please try again"
        Endif
    Else
        Print "Error"
    Endif
Where am I going wrong (this is the client code) I can post the Server code if needed

Re: Socket Commication Help

Posted: Sunday 2nd May 2021 6:46am
by stevedee
AndyGable wrote: Saturday 1st May 2021 7:56pm ...I am trying to talk to another program of mine via Socket...
Take a look at my post: http://captainbodgit.blogspot.com/2016/ ... ambas.html which will walk you through the process.

Your logic is wrong with this line:-
    If ClientConnection.Status = Net.Inactive Then
...as clearly the rest of the If...Then... will not execute if you have an active connection.

Re: Socket Commication Help

Posted: Friday 7th May 2021 10:53pm
by AndyGable
stevedee wrote: Sunday 2nd May 2021 6:46am
AndyGable wrote: Saturday 1st May 2021 7:56pm ...I am trying to talk to another program of mine via Socket...
Take a look at my post: http://captainbodgit.blogspot.com/2016/ ... ambas.html which will walk you through the process.

Your logic is wrong with this line:-
    If ClientConnection.Status = Net.Inactive Then
...as clearly the rest of the If...Then... will not execute if you have an active connection.
Hi Stevedee,

Do i need to change my code to
If ClientConnection.Status <> Net.Inactive Then
I did follow your guide but I think my issue is I am using a CLI application with out any GUI interface so I could not fully follow your guide.

do you have a guide that would work on a CLI application? (the reason why I have selected CLI is this app is to run in the background and it only handles data that has to be sent to the printer it does not need to have a graphical interface. (unless you know a way to load a GUI application with out it showing a GUI interface.

I aI am doing it this way so I do not have to hard code support for every printer I support with in my NPoS application (also allows me to add support for newer printers when they come along)

This is what I had in mind at the moment (this all runs so far from the main command line) If someone know how to start programs from another program when the desktop has loaded I would like to see a example (also can you shut down the desktop once it has loaded and only have say my boot up program running (in window I could terminal the Explorer.exe and then my startup app would take over and load the
rest of the system)

I am loading all my add on modules before starting XServer (I do this by using for example ./NPoS/AddOn/Printer-Epson.gambas &) the & at the end will load the program and return control right back to the Opearting system)

once every add on is loaded I would then use startx ./NPoS/NPoS.gambas to start x server and boot into my NPoS application (that then uses the sockets to talk to the Printer add on module)

Re: Socket Commication Help

Posted: Friday 7th May 2021 11:01pm
by AndyGable
Not sure if this will help but this is the code so far from the CLI printer module

I know for a fact the Printer code works perfectly as I can run the same code in another application that talks directly to the printer
' Gambas module file

Public PoSConnection As ServerSocket

Public SerialPort1 As SerialPort
Public SettingsFileLocation As String = Application.Path &/ "PrinterSettings.conf"

'Printer Only Fucntions
Public PrinterNewLine As String = Chr$(10) & Chr$(13)

' Printer Fucntions Declares
Public PrinterInitialize As String = Chr(&H1B) & "@"
Public CancelFontMode As String = Chr(&H1B) & "!" & Chr(0)
Public NormalFont As String = Chr(&H1B) & "!" & Chr(1)
Public BoldFont As String = Chr(&H1B) & "!" & Chr$(9)
Public DoubleWidthFont As String = Chr(&H1B) & "!" & Chr$(33)
Public DoubleHeightFont As String = Chr(&H1B) & "!" & Chr$(17)
Public DoubleWidthHeightFont As String = Chr(&H1B) & "!" & Chr(49)
Public LeftAlignText As String = Chr(&H1B) & "a" & Chr(48)
Public CentreAlignText As String = Chr(&H1B) & "a" & Chr(49)
Public RightAlignText As String = Chr(&H1B) & "a" & Chr(50)

'Public PrintStoreLogo1 As String = Chr$(&H1B) & "a" & Chr$(48)
'Public PrintStoreLogo2 As String = Chr$(&H1B) & "a" & Chr$(48)
'Public PrintStoreLogo3 As String = Chr$(&H1B) & "a" & Chr$(48)
'Public PrintStoreLogo4 As String = Chr$(&H1B) & "a" & Chr$(48)

Public OpenCashDrawer As String = Chr$(&H1B) & Chr$(&H70) & Chr$(&H0) & Chr$(60) & Chr$(120)

Public CutPaperFull As String = Chr$(&H1D) & "V" & Chr(0)
Public CutPaperPartial As String = Chr$(&H1D) & "V" & Chr(1)

' Printer Serial Settings
Public LinesToFeed As Integer = 0
Public UsePlanPoundSymbol As String
Public PrintTestSlip As String
Public PrintGraphicalLogo As String

Public PrinterPortName As String
Public PrinterBandRate As Integer
Public PrinterFlowControl As String
Public PrinterDataBits As String
Public PrinterStopBits As String
Public PrinterParity As String
Public Sub Main()

    Print "Loading Epson Printer Module..."

    Print "     Loading " & SettingsFileLocation & " file"
    LoadPrinterSettings

    Print "         Data Loaded from Settings file"
    Print "                 Number of Lines to Feed before cutting paper : " & LinesToFeed

    Select Case UsePlanPoundSymbol
        Case "yes", "Yes"
            Print "                 Print The Plain Pound Symbol"

        Case "no", "No"
            Print "                 Print The Pound Symbol by using chr$(156)"
    End Select

    Select Case PrintTestSlip
        Case "yes", "Yes"
            Print "                 One Module has loaded Print a test page"

        Case "no", "No"
            Print "                 No test page set to print once module has loaded"
    End Select

    Select Case PrintGraphicalLogo
        Case "yes", "Yes"
            Print "                 Print The First stored logo as the Store logo"

        Case "no", "No"
            Print "                 No Graphical Logo set to print"
    End Select

    Print "                        Printer Port set to : " & PrinterPortName
    Print "                   Printer Band rate set to : " & PrinterBandRate
    Print "                 Printer Flowcontrol set to : " & PrinterFlowControl
    Print "                   Printer Data Bits set to : " & PrinterDataBits
    Print "                   Printer Stop Bits set to : " & PrinterStopBits
    Print "                      Printer Parity Set to : " & PrinterParity

    Print "     Opening Commication to the Printer please wait..."
    ConnectToPrinter("Open")

    Wait 1 ' Seep for 2 seconds

    If PrintTestSlip = "Yes" Then
        Print "Sending Test Print to the Printer"
        SendTestPrint
    End If

    'Open Port ready for the PoS to connect to the module
    StartConnection

    ConnectToPrinter("Close")

End
Private Sub LoadPrinterSettings()

    Dim SettingsLocalFile As Settings

    SettingsLocalFile = New Settings(SettingsFileLocation)

    'This will Load the PoSSettings.conf file

    '*******    PoS Settings Section    *******
    LinesToFeed = SettingsLocalFile["Printer Settings/LinesToFeed"]
    UsePlanPoundSymbol = SettingsLocalFile["Printer Settings/UsePlanPoundSymbol"]
    PrintTestSlip = SettingsLocalFile["Printer Settings/PrintTestSlip"]
    PrintGraphicalLogo = SettingsLocalFile["Printer Settings/PrintGraphicalLogo"]

    PrinterPortName = SettingsLocalFile["Printer Settings/PortName"]
    PrinterBandRate = SettingsLocalFile["Printer Settings/BandRate"]
    PrinterFlowControl = SettingsLocalFile["Printer Settings/FlowControl"]
    PrinterDataBits = SettingsLocalFile["Printer Settings/DataBits"]
    PrinterStopBits = SettingsLocalFile["Printer Settings/StopBits"]
    PrinterParity = SettingsLocalFile["Printer Settings/Parity"]

End
Private Sub ConnectToPrinter(ConnectionType As String)

    SerialPort1 = New SerialPort

    With SerialPort1

        .Close

        .PortName = PrinterPortName
        .Speed = PrinterBandRate

        Select Case PrinterFlowControl
            Case "Hardware"
                .FlowControl = SerialPort1.Hardware

            Case "Software"
                .FlowControl = SerialPort1.Software

            Case "None"
                .FlowControl = SerialPort1.None
        End Select

        .DataBits = PrinterDataBits
        .StopBits = PrinterStopBits

        Select Case PrinterParity
            Case "None"
                .Parity = SerialPort1.None

            Case "Odd"
                .Parity = SerialPort1.Odd

            Case "Even"
                .Parity = SerialPort1.Even
        End Select

        Select Case ConnectionType
            Case "Open"
                .Open

            Case "Close"
                .Close
        End Select
    End With

End
Private Sub SendTestPrint()

    Write #SerialPort1, PrinterInitialize
    Write #SerialPort1, CentreAlignText & DoubleWidthHeightFont & "THIS IS A TEST PRINT" & CancelFontMode & PrinterNewLine
    Write #SerialPort1, CentreAlignText & DoubleWidthFont & "Printed from Linux" & CancelFontMode & PrinterNewLine

    Write #SerialPort1, LeftAlignText & DoubleWidthHeightFont & BoldFont & "On : " & Format(Now, "dd/mm/yyyy") & CancelFontMode & PrinterNewLine
    Write #SerialPort1, LeftAlignText & DoubleWidthHeightFont & BoldFont & "AT : " & Format(Now, "hh:nn:ss") & CancelFontMode & PrinterNewLine

    Write #SerialPort1, LeftAlignText & NormalFont & PrinterNewLine

    Write #SerialPort1, LeftAlignText & NormalFont & "Epson ESC/PoS Control module for algPoS Point of sale" & PrinterNewLine
    Write #SerialPort1, LeftAlignText & NormalFont & "Software" & PrinterNewLine
    Write #SerialPort1, LeftAlignText & NormalFont & PrinterNewLine
    Write #SerialPort1, LeftAlignText & NormalFont & "Version : " & Application.Version & PrinterNewLine
    Write #SerialPort1, LeftAlignText & NormalFont & "App Name : " & Application.Name & PrinterNewLine

    Feed_CutPaper(LinesToFeed)

    SerialPort1.Send

End
Private Sub Feed_CutPaper(Numberoflines As Integer)

    Dim i As Integer = 0

    For i = 1 To Numberoflines
        Write #SerialPort1, PrinterNewLine
    Next

    Write #SerialPort1, CutPaperFull

End
Private Sub StartConnection()

    PoSConnection = New ServerSocket

    With PoSConnection
        .Port = 9001
        .Type = Net.Internet
        .Listen(1)
    End With

    If PoSConnection.Status = Net.Active Then
        Print "     Module ready for Communcation using port 9001/9101"
    End If

    PoSConnection_Connection("127.0.0.1")

End
Private Sub PoSConnection_Connection(strHostIP As String)

    Dim ThisSocket As Socket

    Do
        If PoSConnection.Status = Net.Pending Then
            ThisSocket = PoSConnection.Accept()
            ThisSocket.Blocking = False

            If ThisSocket.Status = Net.Connected Then
                Print "          Connection Establed with PoS Application"
                Exit
            Else

            End If
        End If
    Loop

    Socket_read

End
Private Sub Socket_read()

    Dim StrMsg As String

    If Last.Status <> Net.Connected Then Return

    Read #Last, StrMsg, Lof(Last)

    Print "Recevied data : " & StrMsg
    PrintData(StrMsg)

End
Private Sub PrintData(PrintData As String)
 'This will format the data from the PoS terminal and print it
End

This is the code I am currenly using to send the data to the Printer add on module
Private Sub SendToModule(DataToSend As String)

    ClientConnection = New Socket

    With ClientConnection
        .Host = "127.0.0.1"
        .Port = 9001
        '.Connect
    End With

    ClientConnection.Connect

    PrintErrorMessages(ClientConnection.Status)

    If ClientConnection.Status <> Net.Inactive Then
        If ClientConnection.Status = Net.Connected Then
            Write #ClientConnection, DataToSend, Len(DataToSend)
            Print "Sending to server : " & DataToSend
        Else
            Close #ClientConnection
            Print "Sorry Time out please try again"
        Endif
    Else
        Print "Error"
    Endif

End

Re: Socket Commication Help

Posted: Sunday 9th May 2021 7:45am
by stevedee
AndyGable wrote: Friday 7th May 2021 10:53pm ...I did follow your guide but I think my issue is I am using a CLI application with out any GUI interface so I could not fully follow your guide...
It doesn't matter whether you are using cli or gui. When I suggested that you follow my guide, I meant open a new Gambas Project and start from there. Gain an understanding of how it works and then create your own implementation in your final code/project. You won't be able to copy 'n paste my code into your project.

I'm sorry if I've misunderstood your question or your code, but I'm too pushed for time at the moment to invest a lot of time on this.

Maybe someone else can pick this one up.