Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

 

   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

Posted

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.

 

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

 

   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

  • Administrators
Posted

Not tested it but try changing your send method to something like

   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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

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

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 2 weeks later...
Posted

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.

 

       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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...