Not sending enough data

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I'm trying to send data to a label printer. Everything is fine, until I try and send it more than about 3000 bytes - it's as though either the extra data is not sent, or it is rejected.

This is the first time I've used .NET for socket programming - I always used to do it in VB6.

VB.NET:
    Private Sub TestPrintTCP()
        Dim tcpSender As TcpClient = New TcpClient()
        tcpSender.Connect("172.17.20.82", 8980)
        If tcpSender.Connected = False Then
            MessageBox.Show("Not connected")
            tcpSender.Close()
            Exit Sub
        End If
        'MessageBox.Show("Connected!")

        Dim sTest As String = "WS;C;XS;" 'all my printer characters goes in here

        Dim SendBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(sTest)
        Dim nStream As NetworkStream = tcpSender.GetStream()
        If nStream.CanWrite = True Then
            MessageBox.Show(SendBytes.Length.ToString)
            nStream.Write(SendBytes, 0, SendBytes.Length)
            nStream.Flush()
        End If

        tcpSender.Close()

    End Sub
 
Not closing the socket could be a problem for the other end. Do nStream.Close() before you close the TcpClient.
 
VB.NET:
        Dim nStream As NetworkStream = tcpSender.GetStream()
        If nStream.CanWrite = True Then
            'MessageBox.Show(SendBytes.Length.ToString)
            nStream.Write(SendBytes, 0, SendBytes.Length)
            nStream.Flush()
        End If
        nStream.Close()
        tcpSender.Close()

Nope, that didnt fix it :confused: I know it's definitely receiving something, as the printer has an LED that flashes when receiving. When I send it a small amount of data, the LED flashes and prints. When I send a large quantity, it flashes but doesnt print :confused:

For reference, I know that the "printer data" I am sending is valid data and would print - I checked that part first :D
 
Still no joy, but some more information.

I tried setting the SendBufferSize to 8192, and this didnt help.

If, however, I write a short amount (less than 2048 bytes) and flush, write and flush, it works OK :confused:
 
Back
Top