[S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Dinosaur
Posts: 1368
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Dinosaur » Jan 07, 2021 9:28

Hi All

Just bought a new printer Epson XP-4100.
Unbelievable how difficult it is to get the printer you want in Australia due to so many people working from home.

My requirement was for ESC/P support.
Yes, many of the latest Inkjets support ESC/P.
Using it on a Beaglebone Black and didn't want to install CUPS , plus I need to send one line to the printer
and may not send another line for hours untill the page is full.

Previously (Parallel port ) it was just a case of sending the ESC codes and all was sweet.
I thought that using sncNetworkPrinter I could do the same.
However, it simply prints garbage.

Epson advises that unless you load a different driver, the printer defaults to ESC/P.
So I sent chr(27) + chr(64) which is ESC @ to initialize the printer.
Then a Sleep to see what happens, nothing, so press Enter to send next line.
A test line "Hello World"It then loads a new page and prints a few characters of garbage.

Am I being over optimistic to expect this to work this way ?
Or more likely I am completely missing something blatant.
Does anyone have any experience doing this either with joshy's snc or other way.

Regards

EDIT:
echo "Hello World" | nc 192.168.8.107 9100 works ??
But this doesn't

Code: Select all

' [S]imple [N]etwork [C]onnection
#include once "snc.bi"
' test of a printer client connection
const as string ServerIP   = "192.168.8.107"
const as ushort ServerPort = 9100
dim as Integer ErrCode
' connect to printer server
var client = NetworkClient(ServerIP,ServerPort)
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

' get a connection from ConnectionFactory
var connection = client.GetConnection()
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

' ready to send ?
while connection->CanPut()<>1: sleep 100 : wend
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

' put data on the connection
Dim as String TxData
Dim as Integer TxLen
TxData = chr(&h1b) + chr(&h40)
TxLen  = Len(TxData)
connection->PutData(@TxData,TxLen)
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

sleep 100

TxData = "Hello World"
TxLen  = Len(TxData)
connection->PutData(@TxData,TxLen)
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)
end
badidea
Posts: 2281
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby badidea » Jan 07, 2021 14:50

Can you try with zstring ptr? Or use strPtr/sadd. I think @TxData is not what you want. Also 0 string termination may be needed.
Dinosaur
Posts: 1368
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Dinosaur » Jan 07, 2021 20:35

Hi All

Thanks for the reply badidea.
Or more likely I am completely missing something blatant.

You were right (AGAIN) all it needed was StrPtr instead of @.

One of these days I am going to get over my phobia about pointers.

Regards
NorbyDroid
Posts: 19
Joined: May 21, 2016 22:55

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby NorbyDroid » Jan 15, 2021 1:19

I was wondering if someone could check this over and see what I am doing wrong.

The Client is sending four chars in a string to the Server but when the server gets the dats and checks the info, it isn't always showing the proper data.

Here is the server code:

Code: Select all

#Include "snc.bi"
#Include "sendreceive.bi"

ScreenRes 640,176,32
Width 80,11

Dim Shared As NetworkConnection Ptr Connection

Const As UShort PORT=80
Var Server=NetworkServer(PORT)
Var ErrCode=Server.GetLastError()

If ErrCode<>0 Then
  Print "Error: Can't create server! " & GetNetworkErrorText(ErrCode)
  Beep:Sleep:End 1
End If

Print "Server created.  Waiting for Client...."

While Connection=NULL
  Connection=Server.GetConnection()
Wend

Dim As Integer yPos
Dim As String GetData,SetData

Print "Client connected " & Server.ClientIP

yPos=1
While Asc(InKey())<>27
  GetData=""
  ErrCode=ReceiveData(Connection,GetData,4)
  SetData=Left(GetData,4)

  If ErrCode=-1 Then
    Print "Error receiving data disconnect client!"
    Connection=NULL
    Beep:Sleep:End 1
  Else
    'Print "[" & GetData & "] " & Len(GetData),
    Print "[" & SetData & "] " & Len(SetData),
  End If

  Sleep 10
Wend

Beep:Print "Server shutting down...."
If Connection<>NULL Then Connection=NULL
Sleep 2000


Here is the Client Code:

Code: Select all

#Include "snc.bi"
#Include "sendreceive.bi"

ScreenRes 640,224,32
Width 80,14

Dim As Integer tFrame,tPos,xPos
Dim As String TrainArt(10,6),strData,tData

Declare Sub NetSend(strText As String)

Const As String IP="192.168.1.101"
Const As UShort PORT=80

Dim Shared As NetworkConnection Ptr Connection

Var Client=NetworkClient(IP,PORT)
Var ErrCode=Client.GetLAstError()

If ErrCode<>0 Then
  Print "Error: can't create client! " & GetNetworkErrorText(ErrCode)
  Beep:Sleep:End 1
End If

Print "Client connected with server " & IP
Connection=Client.GetConnection()
ErrCode=Client.GetLAstError()

If ErrCode<>0 Then
  Print "Error: Can't get a conection! " & GetNetworkErrorText(ErrCode)
  Beep:Sleep:End 1
End If

xPos=1
tFrame=1

While Asc(InKey())<>27
  For tLoop As Integer=1 To 10
    tData=Right(" "+str(tLoop),2)+Right(" "+str(tFrame),2)
    Print "{" & tData & "} " & Len(tData),
    NetSend tData

    Sleep 50
  Next

  tPos+=1
  If tPos Mod 6=0 Then
    tFrame+=1:If tFrame>6 Then tFrame=1
     xPos+=1:If xPos>160 Then tPos=1:xPos=1
  EndIf
Wend

Beep:Print "Client shutting down...."

If Connection<>NULL Then Connection=NULL
Sleep 2000

Function NetReceive(strLength As Integer) As String
Dim As Integer tCode
Dim As String strText

tCode=ReceiveData(Connection,strText,strLength)

If tCode=-1 Then
  Print "Error receiving data disconnect client!"
  Connection=NULL
  Beep:Sleep:End 1
Else
  NetReceive=strText
End If
End Function

Sub NetSend(strText As String)
Dim As Integer tCode

tCode=SendData(Connection,strText)

If tCode=-1 Then
  Print "Error sending data disconnect client!"
  Connection=NULL
  Beep:Sleep:End 1
End If
End Sub


What I was trying to accomplish is for the Client to send a string of ascii art to the Server and the Server displays it on the screen. Thank you for your help.
badidea
Posts: 2281
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby badidea » Jan 16, 2021 2:09

@NorbyDroid, If you look at the implementation of SendData() in sendreceive.bi, then you can see that 1 additional byte is transmitted:

ErrCode = connection->putData(strptr(strData),nChars+1)

This is a 0 character to mark the end of text.

In your code, add 1 byte in receive:

ErrCode=ReceiveData(ConnectionX,GetData,5)

I think a more robust solution can be made, but this should work.

BTW, if you are transmissing binary data, it is more efficient to use putData() directly.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: Baidu [Spider] and 5 guests