Socket Commication Help

Post

Posted
Rating:
#1 (In Topic #652)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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

Code (gambas)

  1. ClientConnection = New Socket
  2.  
  3.     With ClientConnection
  4.         .Host = "127.0.0.1"
  5.         .Port = 9001
  6.         .Connect
  7.     End With
  8.  
  9.     If ClientConnection.Status = Net.Inactive Then
  10.         Print "No Connection"
  11.         Wait 1
  12.  
  13.         If ClientConnection.Status = Net.Connected Then
  14.             Write #ClientConnection, DataToSend, Len(DataToSend)
  15.             Print "Sending to server : " & DataToSend
  16.         Else
  17.             Close #ClientConnection
  18.             Print "Sorry Time out please try again"
  19.         Endif
  20.     Else
  21.         Print "Error"
  22.     Endif
  23.  

Where am I going wrong (this is the client code) I can post the Server code if needed
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

AndyGable said

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

Take a look at my post: Captain Bodgit: Using Sockets with Gambas which will walk you through the process.

Your logic is wrong with this line:-

Code (gambas)

  1.     If ClientConnection.Status = Net.Inactive Then
…as clearly the rest of the If…Then… will not execute if you have an active connection.
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

stevedee said

AndyGable said

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

Take a look at my post: Captain Bodgit: Using Sockets with Gambas which will walk you through the process.

Your logic is wrong with this line:-

Code (gambas)

  1.     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

Code (gambas)

  1. 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)
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public PoSConnection As ServerSocket
  4.  
  5. Public SerialPort1 As SerialPort
  6. Public SettingsFileLocation As String = Application.Path &/ "PrinterSettings.conf"
  7.  
  8. 'Printer Only Fucntions
  9. Public PrinterNewLine As String = Chr$(10) & Chr$(13)
  10.  
  11. ' Printer Fucntions Declares
  12. Public PrinterInitialize As String = Chr(&H1B) & "@"
  13. Public CancelFontMode As String = Chr(&H1B) & "!" & Chr(0)
  14. Public NormalFont As String = Chr(&H1B) & "!" & Chr(1)
  15. Public BoldFont As String = Chr(&H1B) & "!" & Chr$(9)
  16. Public DoubleWidthFont As String = Chr(&H1B) & "!" & Chr$(33)
  17. Public DoubleHeightFont As String = Chr(&H1B) & "!" & Chr$(17)
  18. Public DoubleWidthHeightFont As String = Chr(&H1B) & "!" & Chr(49)
  19. Public LeftAlignText As String = Chr(&H1B) & "a" & Chr(48)
  20. Public CentreAlignText As String = Chr(&H1B) & "a" & Chr(49)
  21. Public RightAlignText As String = Chr(&H1B) & "a" & Chr(50)
  22.  
  23. 'Public PrintStoreLogo1 As String = Chr$(&H1B) & "a" & Chr$(48)
  24. 'Public PrintStoreLogo2 As String = Chr$(&H1B) & "a" & Chr$(48)
  25. 'Public PrintStoreLogo3 As String = Chr$(&H1B) & "a" & Chr$(48)
  26. 'Public PrintStoreLogo4 As String = Chr$(&H1B) & "a" & Chr$(48)
  27.  
  28. Public OpenCashDrawer As String = Chr$(&H1B) & Chr$(&H70) & Chr$(&H0) & Chr$(60) & Chr$(120)
  29.  
  30. Public CutPaperFull As String = Chr$(&H1D) & "V" & Chr(0)
  31. Public CutPaperPartial As String = Chr$(&H1D) & "V" & Chr(1)
  32.  
  33. ' Printer Serial Settings
  34. Public LinesToFeed As Integer = 0
  35. Public UsePlanPoundSymbol As String
  36. Public PrintTestSlip As String
  37. Public PrintGraphicalLogo As String
  38.  
  39. Public PrinterPortName As String
  40. Public PrinterBandRate As Integer
  41. Public PrinterFlowControl As String
  42. Public PrinterDataBits As String
  43. Public PrinterStopBits As String
  44. Public PrinterParity As String
  45.  

Code (gambas)

  1. Public Sub Main()
  2.  
  3.     Print "Loading Epson Printer Module..."
  4.  
  5.     Print "     Loading " & SettingsFileLocation & " file"
  6.     LoadPrinterSettings
  7.  
  8.     Print "         Data Loaded from Settings file"
  9.     Print "                 Number of Lines to Feed before cutting paper : " & LinesToFeed
  10.  
  11.     Select Case UsePlanPoundSymbol
  12.         Case "yes", "Yes"
  13.             Print "                 Print The Plain Pound Symbol"
  14.  
  15.         Case "no", "No"
  16.             Print "                 Print The Pound Symbol by using chr$(156)"
  17.     End Select
  18.  
  19.     Select Case PrintTestSlip
  20.         Case "yes", "Yes"
  21.             Print "                 One Module has loaded Print a test page"
  22.  
  23.         Case "no", "No"
  24.             Print "                 No test page set to print once module has loaded"
  25.     End Select
  26.  
  27.     Select Case PrintGraphicalLogo
  28.         Case "yes", "Yes"
  29.             Print "                 Print The First stored logo as the Store logo"
  30.  
  31.         Case "no", "No"
  32.             Print "                 No Graphical Logo set to print"
  33.     End Select
  34.  
  35.     Print "                        Printer Port set to : " & PrinterPortName
  36.     Print "                   Printer Band rate set to : " & PrinterBandRate
  37.     Print "                 Printer Flowcontrol set to : " & PrinterFlowControl
  38.     Print "                   Printer Data Bits set to : " & PrinterDataBits
  39.     Print "                   Printer Stop Bits set to : " & PrinterStopBits
  40.     Print "                      Printer Parity Set to : " & PrinterParity
  41.  
  42.     Print "     Opening Commication to the Printer please wait..."
  43.     ConnectToPrinter("Open")
  44.  
  45.     Wait 1 ' Seep for 2 seconds
  46.  
  47.     If PrintTestSlip = "Yes" Then
  48.         Print "Sending Test Print to the Printer"
  49.         SendTestPrint
  50.     End If
  51.  
  52.     'Open Port ready for the PoS to connect to the module
  53.     StartConnection
  54.  
  55.     ConnectToPrinter("Close")
  56.  
  57.  

Code (gambas)

  1. Private Sub LoadPrinterSettings()
  2.  
  3.     Dim SettingsLocalFile As Settings
  4.  
  5.     SettingsLocalFile = New Settings(SettingsFileLocation)
  6.  
  7.     'This will Load the PoSSettings.conf file
  8.  
  9.     '*******    PoS Settings Section    *******
  10.     LinesToFeed = SettingsLocalFile["Printer Settings/LinesToFeed"]
  11.     UsePlanPoundSymbol = SettingsLocalFile["Printer Settings/UsePlanPoundSymbol"]
  12.     PrintTestSlip = SettingsLocalFile["Printer Settings/PrintTestSlip"]
  13.     PrintGraphicalLogo = SettingsLocalFile["Printer Settings/PrintGraphicalLogo"]
  14.  
  15.     PrinterPortName = SettingsLocalFile["Printer Settings/PortName"]
  16.     PrinterBandRate = SettingsLocalFile["Printer Settings/BandRate"]
  17.     PrinterFlowControl = SettingsLocalFile["Printer Settings/FlowControl"]
  18.     PrinterDataBits = SettingsLocalFile["Printer Settings/DataBits"]
  19.     PrinterStopBits = SettingsLocalFile["Printer Settings/StopBits"]
  20.     PrinterParity = SettingsLocalFile["Printer Settings/Parity"]
  21.  
  22.  

Code (gambas)

  1. Private Sub ConnectToPrinter(ConnectionType As String)
  2.  
  3.     SerialPort1 = New SerialPort
  4.  
  5.     With SerialPort1
  6.  
  7.         .Close
  8.  
  9.         .PortName = PrinterPortName
  10.         .Speed = PrinterBandRate
  11.  
  12.         Select Case PrinterFlowControl
  13.             Case "Hardware"
  14.                 .FlowControl = SerialPort1.Hardware
  15.  
  16.             Case "Software"
  17.                 .FlowControl = SerialPort1.Software
  18.  
  19.             Case "None"
  20.                 .FlowControl = SerialPort1.None
  21.         End Select
  22.  
  23.         .DataBits = PrinterDataBits
  24.         .StopBits = PrinterStopBits
  25.  
  26.         Select Case PrinterParity
  27.             Case "None"
  28.                 .Parity = SerialPort1.None
  29.  
  30.             Case "Odd"
  31.                 .Parity = SerialPort1.Odd
  32.  
  33.             Case "Even"
  34.                 .Parity = SerialPort1.Even
  35.         End Select
  36.  
  37.         Select Case ConnectionType
  38.             Case "Open"
  39.                 .Open
  40.  
  41.             Case "Close"
  42.                 .Close
  43.         End Select
  44.     End With
  45.  
  46.  

Code (gambas)

  1. Private Sub SendTestPrint()
  2.  
  3.     Write #SerialPort1, PrinterInitialize
  4.     Write #SerialPort1, CentreAlignText & DoubleWidthHeightFont & "THIS IS A TEST PRINT" & CancelFontMode & PrinterNewLine
  5.     Write #SerialPort1, CentreAlignText & DoubleWidthFont & "Printed from Linux" & CancelFontMode & PrinterNewLine
  6.  
  7.     Write #SerialPort1, LeftAlignText & DoubleWidthHeightFont & BoldFont & "On : " & Format(Now, "dd/mm/yyyy") & CancelFontMode & PrinterNewLine
  8.     Write #SerialPort1, LeftAlignText & DoubleWidthHeightFont & BoldFont & "AT : " & Format(Now, "hh:nn:ss") & CancelFontMode & PrinterNewLine
  9.  
  10.     Write #SerialPort1, LeftAlignText & NormalFont & PrinterNewLine
  11.  
  12.     Write #SerialPort1, LeftAlignText & NormalFont & "Epson ESC/PoS Control module for algPoS Point of sale" & PrinterNewLine
  13.     Write #SerialPort1, LeftAlignText & NormalFont & "Software" & PrinterNewLine
  14.     Write #SerialPort1, LeftAlignText & NormalFont & PrinterNewLine
  15.     Write #SerialPort1, LeftAlignText & NormalFont & "Version : " & Application.Version & PrinterNewLine
  16.     Write #SerialPort1, LeftAlignText & NormalFont & "App Name : " & Application.Name & PrinterNewLine
  17.  
  18.     Feed_CutPaper(LinesToFeed)
  19.  
  20.     SerialPort1.Send
  21.  
  22.  


Code (gambas)

  1. Private Sub Feed_CutPaper(Numberoflines As Integer)
  2.  
  3.     Dim i As Integer = 0
  4.  
  5.     For i = 1 To Numberoflines
  6.         Write #SerialPort1, PrinterNewLine
  7.     Next
  8.  
  9.     Write #SerialPort1, CutPaperFull
  10.  
  11.  


Code (gambas)

  1. Private Sub StartConnection()
  2.  
  3.     PoSConnection = New ServerSocket
  4.  
  5.     With PoSConnection
  6.         .Port = 9001
  7.         .Type = Net.Internet
  8.         .Listen(1)
  9.     End With
  10.  
  11.     If PoSConnection.Status = Net.Active Then
  12.         Print "     Module ready for Communcation using port 9001/9101"
  13.     End If
  14.  
  15.     PoSConnection_Connection("127.0.0.1")
  16.  
  17.  

Code (gambas)

  1. Private Sub PoSConnection_Connection(strHostIP As String)
  2.  
  3.     Dim ThisSocket As Socket
  4.  
  5.     Do
  6.         If PoSConnection.Status = Net.Pending Then
  7.             ThisSocket = PoSConnection.Accept()
  8.             ThisSocket.Blocking = False
  9.  
  10.             If ThisSocket.Status = Net.Connected Then
  11.                 Print "          Connection Establed with PoS Application"
  12.                 Exit
  13.             Else
  14.  
  15.             End If
  16.         End If
  17.     Loop
  18.  
  19.     Socket_read
  20.  
  21.  

Code (gambas)

  1. Private Sub Socket_read()
  2.  
  3.     Dim StrMsg As String
  4.  
  5.     If Last.Status <> Net.Connected Then Return
  6.  
  7.     Read #Last, StrMsg, Lof(Last)
  8.  
  9.     Print "Recevied data : " & StrMsg
  10.     PrintData(StrMsg)
  11.  
  12.  

Code (gambas)

  1. Private Sub PrintData(PrintData As String)
  2.  'This will format the data from the PoS terminal and print it
  3.  


This is the code I am currenly using to send the data to the Printer add on module

Code (gambas)

  1. Private Sub SendToModule(DataToSend As String)
  2.  
  3.     ClientConnection = New Socket
  4.  
  5.     With ClientConnection
  6.         .Host = "127.0.0.1"
  7.         .Port = 9001
  8.         '.Connect
  9.     End With
  10.  
  11.     ClientConnection.Connect
  12.  
  13.     PrintErrorMessages(ClientConnection.Status)
  14.  
  15.     If ClientConnection.Status <> Net.Inactive Then
  16.         If ClientConnection.Status = Net.Connected Then
  17.             Write #ClientConnection, DataToSend, Len(DataToSend)
  18.             Print "Sending to server : " & DataToSend
  19.         Else
  20.             Close #ClientConnection
  21.             Print "Sorry Time out please try again"
  22.         Endif
  23.     Else
  24.         Print "Error"
  25.     Endif
  26.  
  27.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’

AndyGable said

…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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.