Annoying Problem

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Hi

Can someone please help me?

I'm trying to create a program to control an IP Wallboard (which is an LED screen, often used to scroll text 2 lines deep by about 30 characters long - traditionally red LED).

Firstly i tried connecting via WinSock which works perfectly! However I want it to work in the .NET way of doing things, which would mean using sockets. I've tried two ways from MSDN, and about another 3 ways from other examples i've found. I just can get it working! Could someone PLEASE PLEASE tell me why this isnt working? I've been trying to do it for 2 weeks now and its doing my head in.


Visual Basic:
    Private mobjClient As TcpClient

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mobjClient = New TcpClient("172.28.48.150", 3500)
    End Sub

    Private Sub Send(ByVal Data As String)
        Try
            MsgBox("connected = " & mobjClient.Connected.ToString)
            Dim w As New IO.StreamWriter(mobjClient.GetStream)
            w.Write(Data) ' & vbCr)
            w.Flush()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

    Private Sub btnMSDN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSDN.Click

        Send("Hello")

    End Sub

I know this is probably in the wrong place, but its the only section where there is even a remote chance of it getting answered....So dont move it please.

Thanks
 
Are you getting any errors or is it just now sending the string through to the device? What format does the device expect the data to be in (ascii, unicode, byte array etc.)?
 
I strongly believe its expecting it in a string format.

Here is a sample of the code that i'm using with Winsock which works fine.

Visual Basic:
'Other code
            With Me.Winsock1
                .Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol
                ' ip address of sign
                .RemoteHost = "172.28.48.150"
                ' port number sign listens on
                .RemotePort = CInt("3500")
                .Connect()
            End With
'Other code

    Private Sub Winsock1_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock1.ConnectEvent ' Fired once winsock connects
        Call writedebug("Connected")
        With Me.Winsock1
            ' send the message to the sign
            .SendData(sMessage) 'sMessage is a string
        End With
        Call writedebug("Data Sent")
    End Sub

I'm not actually sending the text "Hello". I'm actually sending a long list of escape characters and some text, i removed this as i thought it would add complexity to read and wasnt very helpful. But i'll include it below, just incase.

Visual Basic:
    Public sHeader As String 'Holds Message Header
    Public sNumMessage As String 'Holds No. of Messages
    Public sNumDestination As String 'Holds No. of Destinations
    Public sNumSigns As String 'Holds Sign Numbers
    Public sMessHigh As String 'Holds Message High
    Public sMessLow As String 'Holds Message Low
    Public sMessText1 As String 'Holds Message Text for line 1
    Public sMessText2 As String 'Holds message text for line 2
    Public sMessRolling As String 'Holds rolling string
    Public sMessLargeRolling As String
    Public bytChecksum As Byte 'Holds Message Checksum
    Public Shared sMessage As String 'String to hold all of the above
    Public sEsc As String 'Holds Escape ASCII Character
    Public sForeColorStr As String 'holds foreground colour of text

    Private Sub btnMSDN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSDN.Click


        'set escape string
        sEsc = Chr(&H1BS)

        bSocketInUse = False
        ' initialise to red text on black background
        sForeColorStr = Chr(27) & "[31;40m"

        Dim strMessage As String = ""
        Dim strEsc As String = Chr(&H1BS) 'Holds Escape ASCII Character
        sHeader = Chr(&H99S)
        sNumMessage = Chr(&H1S)
        sNumDestination = Chr(&H1S)
        sNumSigns = Chr(&H1S)
        sMessHigh = Chr(0)

        sMessText1 = strEsc & "[2J" & strEsc & "[1;1H" & sForeColorStr & "0937"

        sMessLow = Chr(Len(Trim(sMessText1)) + Len(Trim(sMessText2)))

        'build message for transmission
        sMessage = sHeader & sNumMessage & sNumDestination & sNumSigns & sMessLow & sMessHigh ' this is the message header stuff
        sMessage = sMessage & sMessText1  'add text for top line ' & sEsc & "[4x"

        bytChecksum = 0
        ' update checksum using each byte of the message
        For i As Integer = 1 To Len(sMessage)
            bytChecksum = RotateLeft(bytChecksum)
            bytChecksum = bytChecksum Xor Asc(Mid(sMessage, i, 1))
        Next
        ' do one further rotation
        bytChecksum = RotateLeft(bytChecksum)
        ' and append the checksum to the message
        sMessage = sMessage & Chr(bytChecksum)

        Send(sMessage)

    End Sub

Thanks
 
Not tested it but try changing your send method to something like
Visual Basic:
    Private Sub Send(ByVal Data As String)
        Try
            MsgBox("connected = " & mobjClient.Connected.ToString)

            Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(Data)
            Dim s As IO.Stream = mobjClient.GetStream
            s.Write(b, 0, b.Length)
            s.Flush()

         
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
 
Just a failure to display anything - no error message. Previously the winsock version was on a different application, this time i've added it into the same application. Then alternate which piece of code is called. I done this just to make sure that the string being passed is exactly identical and to ensure that i havent got a missing escape character or something. The WinSock still works and the sockets one still doesnt.

Its really annoying as i've tried 2 examples from MSDN, one from a Wrox book and another 2/3 from various google search results.

Thanks for your help btw, i really do appreciate it.
 
I forget the exact namespace, and code to do this; however, I had trouble sending data the serial port (vb6 and msComm control worked) didn't work in .NET by default, I had to change the System.Text.Encoding. Somehow through the magaic of COM the vb6 version knew what to send, I ended up needing to send a UTF8 string.

HTH
 
Well i'm still stuck on this :(

I created another program to listen to what is being sent through the port and output the characters in hex format. I then found out, that my sockets were working fine, but they are sending different escape characters to the port then the winsock one is. So as mentioned above i've tried using different encoding methods. This is what is returned under various encoding (i've left out the ones which were vastly different).

winsock -
991111E01B5B324A1B5B313B31481B5B33313B34306D4A6F696E74205465737420332A

System.Text.Encoding.UTF8.GetBytes(sMessage) -
E284A21111E01B5B324A1B5B313B31481B5B33313B34306D4A6F696E74205465737420332A

System.Text.Encoding.ASCII.GetBytes(sMessage) -
3F1111E01B5B324A1B5B313B31481B5B33313B34306D4A6F696E74205465737420332A

With the obvious exception of the winsock one, they both use the below code.

Visual Basic:
        mobjClient = New TcpClient("172.28.46.27", 3500)
        Try
            Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(sMessage)
            Dim s As IO.Stream = mobjClient.GetStream
            s.Write(b, 0, b.Length)
            s.Flush()
            s.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Can anybody see why this is happening please? If its unavoidable, can you think of a workaround? (Short of using Winsock).

Thanks
 
I've found a work around, but i dont know why the work around actually works. But as its different i'll post that in a new thread.
 
Back
Top