Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi to all
I want to connect with a barcode printer and send him some specific characters and take back anything they send to me

In VB6 i have use the winsock and (for me) the things is simple....

VB
Winsock1(totlistsite_w).RemoteHost = ListView1.ListItems(totlistsite_w).SubItems(1)
Winsock1(totlistsite_w).RemotePort = ListView1.ListItems(totlistsite_w).SubItems(2)
Winsock1(totlistsite_w).Connect
Do
    txtCounter = ""
    If Return_Status.Enabled = False Then Exit Do
    If ListView1.ListItems(totlistsite_w).ListSubItems(6) = "" Then Exit Do
    DoEvents
    If Winsock1(totlistsite_w).State = 7 Then
        Winsock1(totlistsite_w).SendData "!V32 1" & vbCrLf
        txtCounter = ReadString1(Winsock1(totlistsite_w))
    End If
Loop Until Winsock1(totlistsite_w).State = 7 Or Winsock1(totlistsite_w).State = 9
Winsock1(totlistsite_w).Close



Please could you help me to make something similar with vb.net.

Thanks and regards.
Posted
Updated 22-Feb-11 12:41pm
v3
Comments
DaveAuld 21-Feb-11 13:58pm    
Edit: set lang in pre block

This[^] thread explains how you can use the VB6.0 Winsock Control in .Net.

If you want to go totally .NET though, this[^] will get you started.
 
Share this answer
 
And the code in VB.net

Cheked.................

VB
Dim i, j As Integer
Dim FindBlank As String
Dim txtonlyCounter As Integer = 0
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(PrinterIP, PrinterPort)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
    Dim sendBytes As [Byte]() = System.Text.ASCIIEncoding.ASCII.GetBytes("!V32 1" & vbCrLf)
    networkStream.Write(sendBytes, 0, sendBytes.Length)
    Dim bytes1(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes1, 0, CInt(tcpClient.ReceiveBufferSize))
    Dim returndata As String = ""
    returndata = Encoding.ASCII.GetString(bytes1)
    tcpClient.Close()
    Dim txtCounterLength As Integer = Len(returndata)
    For i = 1 To txtCounterLength
        FindBlank = Mid(returndata, i, 1)
        If FindBlank = " " Then
            j = i - 1
            txtonlyCounter = Mid(returndata, 1, j)
            Exit For
        End If
    Next i
    Try
        ListView1.Items(PrinterIndex).SubItems(12).Text = txtonlyCounter
    Catch
        ListView1.Items.Item(PrinterIndex).SubItems.Add(12).Text = txtonlyCounter
    End Try
Else
    If Not networkStream.CanRead Then
        'MsgBox("Could not write data to data stream")
        tcpClient.Close()
    Else
        If Not networkStream.CanWrite Then
            'MsgBox("Could not read data from data stream")
            tcpClient.Close()
        End If
    End If
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900