We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP/IP Socket Programming
#12
Hi,

Glad the basic connection is working.

LDClient.SendMessage can take either a number or a string as input.  The input is then converted into a string, then array of bytes (each byte is the ascii character code value) with trailing null terminator before sending.

I would recommend sending a string so it is clear what you are sending, e.g.  LDClient.SendMessage("0030500000000").  If you send just a number LDClient.SendMessage(0030500000000) it would be interpreted as "3050000000" - conversion from number to string would loose the leading zeros.  This is the extension code behind SendMessage.

Code:
                if (!tcpClient.Connected) return "NOT_CONNECTED";
                Byte[] bytes = Encoding.UTF8.GetBytes(message + '\0');
                NetworkStream networkStream = tcpClient.GetStream();
                networkStream.Write(bytes, 0, bytes.Length);
                return "SUCCESS";

Secondly, you say you want to send bytes - there is a distiction between byte values and their ascii representation.  A byte is a number between 0 and 255 (in decimal and FF in hex), and each value represnets a character, so the character "0" is 48 (dec) or 30 (hex), see https://www.ascii-code.com.  If you want to send pure byte numbers then you will have to form the string using Text.GetCharacter(code), where code is the byte value.

Code:
char = Text.GetCharacter(48)
TextWindow.WriteLine(char)
code = Text.GetCharacterCode("0")
TextWindow.WriteLine(code)

One suggestion is to try to read from the server first (if you know how to get it to send a message), and see what it looks like.  

Code:
result = LDClient.Connect("192.168.2.20:15730", "False")
If (result = "SUCCESS") Then
  msg = ""
  LDClient.ServerMessage = OnServerMessage
  While("True") 'Keep the program running - we would do more here later
    If (msg <> "") Then 'We have a message
      TextWindow.WriteLine("Client message received : "+msg)
      msg = "" 'Message handled
    EndIf
   
    Program.Delay(10)' Don't mash cpu
  EndWhile
EndIf
Sub OnServerMessage
  msg = LDClient.LastServerMessage
EndSub


If you have a link to some documentation for the server device that you can share, there may be more detailed info on the data format required and some way to check what was received by the server.
[-] The following 1 user Likes litdev's post:
  • AbsoluteBeginner
Reply


Messages In This Thread
TCP/IP Socket Programming - by Juergen - 07-14-2025, 08:35 AM
RE: TCP/IP Socket Programming - by litdev - 07-14-2025, 02:49 PM
RE: TCP/IP Socket Programming - by Juergen - 07-16-2025, 09:40 AM
RE: TCP/IP Socket Programming - by Juergen - 07-25-2025, 10:55 AM
RE: TCP/IP Socket Programming - by litdev - 07-25-2025, 11:23 AM
RE: TCP/IP Socket Programming - by Juergen - 07-25-2025, 11:48 AM
RE: TCP/IP Socket Programming - by litdev - 07-25-2025, 01:12 PM
RE: TCP/IP Socket Programming - by Juergen - 07-25-2025, 06:17 PM
RE: TCP/IP Socket Programming - by litdev - 07-25-2025, 06:33 PM
RE: TCP/IP Socket Programming - by Juergen - 07-27-2025, 08:49 AM
RE: TCP/IP Socket Programming - by litdev - 07-27-2025, 10:50 AM
RE: TCP/IP Socket Programming - by Juergen - 07-27-2025, 12:21 PM
RE: TCP/IP Socket Programming - by litdev - 07-27-2025, 01:42 PM
RE: TCP/IP Socket Programming - by Juergen - 07-28-2025, 08:04 AM
RE: TCP/IP Socket Programming - by litdev - 07-28-2025, 08:32 AM
RE: TCP/IP Socket Programming - by WhTurner - 07-28-2025, 12:37 PM
RE: TCP/IP Socket Programming - by Juergen - 08-23-2025, 06:39 AM
RE: TCP/IP Socket Programming - by litdev - 08-23-2025, 12:33 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)