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!
Thanks a lot for your Help, the connection to the device is running now !!! Now i want to transmit a message of 13 Bytes with the function : LDClient.SendMessage() But i am not sure what the correct format of the message is ? I tryed LDClient.SendMessage("0030500000000") and LDClient.SendMessage(0030500000000) , but i am even not sure , wether this is the right way of doing it anyway ....... once more i need a little help from you .....
07-27-2025, 10:50 AM (This post was last modified: 07-27-2025, 11:00 AM by litdev.)
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.
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.
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.
07-27-2025, 12:21 PM (This post was last modified: 07-27-2025, 01:01 PM by Juergen.)
you are right with the conversion to ascii when the LDClient.SendMessage converts all to a string. so when i want to transmit a pure
00 (HEX) the LDClient.SendMessage send 30(Hex ) , that is the wrong value in the telegram for the device .
The Functions : Text.GetCharacter() or Text.GetCharacterCode(0) will not help me with that...... i guess . I think i have a problem now ;-(
07-28-2025, 08:32 AM (This post was last modified: 07-28-2025, 08:34 AM by litdev.)
Hi, not sure which bit you don't follow.
A string is in fact just an array of characters, so we can create a string by appending characters to the end of a string, this is what ' bytes = Text.Append(bytes,char) ' is doing.
So char is a single character.
A character is internally stored as a byte (00 to FF in hex), so a string as an array of bytes. When we print a string, the char (byte) value corresponds to the character defined by the ascii code for the given byte value, so a string with byte value of 0x20 (or 32 decimal) will print a space. Not all of the 256 ascii values are characters, some are control characters (originally derived from typewriters and mechanical input, like Line Feed, Cariage Return, Backspace, Beep etc.
So the key is to get the character (char) that corresponds to a given ascii code number, that is simply what ' Text.GetCharacter(7) ' does (7 is a machine beep).
So in the end we form a string (array of bytes) with each byte being the byte value corresponding to a character obtained from a byte value (in decimal, so we use the hex to decimal conversion ' LDMath.Base2Decimal ' for convenience).
I don't know in detail exactly what this does at a low level.
Maybe this helps (a bit of googling) - looks like maybe the device was sent something it didn't understand (line5?), but I'm not an expert at all on TCP:
It is hard to say what Protocol means. Or what is "application" protocol in this case? Pure TCP/IP is only a stream of data like a file read/write. "Application" protocol is a logic how applications (devices) talk to each other.