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
LDCommPort
#1
Hallo zusammen, ich versuche gerade, mit SB eine Steuerung für meine Modelleisenbahn zu schreiben. Dabei bin ich auf ein Problem gestoßen, das ich nicht lösen kann. Ich habe folgenden Code zur Steuerung der Lokomotiven erstellt.

lokAdresseTB = Controls.AddTextBox(20,930)
Controls.SetTextBoxText(lokAdresseTB, "3")

lokSlider = LDControls.AddSlider(150.930.200)
LDControls.SetSliderRange(lokSlider,0,126)
LDControls.SetSliderValue(lokSlider,0)

LokVorBTN = Controls.AddButton("▶️ Vorwärts",380,930)
LokZurBTN = Controls.AddButton("◀️ Rückwärts",450,930)
LokStopBTN = Controls.AddButton("⏹️ Halt",520,930)

Sub-LokSteuerung
  adresse = Controls.GetTextBoxText(lokAdresseTB)
  geschw = LDControls.GetSliderValue(lokSlider)

  Wenn Controls.LastClickedButton = LokVorBTN, dann
    LDCommPort.TXString("<t " + adresse + geschw +"1 ">")
  EndIf

  Wenn Controls.LastClickedButton = LokZurBTN, dann
    LDCommPort.TXString("<t " + adresse + geschw + "0 ">")
  EndIf

  Wenn Controls.LastClickedButton = LokStopBTN, dann
    LDCommPort.TXString("<t " + adresse + " 1 0>")
  EndIf
EndSub

Das Problem liegt in dieser Zeile: LDCommPort.TXString("<t " + address + speed +"1 ">"). Hier behauptet SB, ich hätte drei Argumente angegeben, obwohl nur eines benötigt wird. Hat jemand eine Idee, wie man das anders schreiben könnte?
Der Befehl, der an die DCC-EX-Zentrale gesendet werden muss, sieht folgendermaßen aus: <t 3 50 1> Das t steht für die anzusprechende Lokomotive; die 3 ist die DCC-Adresse, die 50 die Geschwindigkeit und die 1 die Richtung.
Viele Grüße, Eddie
Reply
#2
Hi Eddie,

Looking just at the command  LDCommPort.TXString("<t " + address + speed +"1 ">") as the rest appears to be using language aliases (share using Publish converts to standard SB without aliases).

TXString requires a single string, the following compiles with the string output for testing:

Code:
adresse = 3
geschw = 50
LDCommPort.TXString("<t " + adresse + " " + geschw +" 1>")
TextWindow.WriteLine("<t " + adresse + " " + geschw +" 1>")


Maybe it is simply the extra " you have.
Reply
#3
Es liegt wohl nicht am " . Wenn ich nur " an den Anfang und das Ende des Strings setze, wie es sein sollte, dann werden die Variablen 'adress' und 'geschw' wie alles andere in " " gelb und haben dann keine Wirkung mehr.
Reply
#4
Did you try it the way I showed, which creates a string concatenation of constants (in " ") and variables.

If you still have issues, then please post the full code using Publish so we can try compiling it without manually changing keywords.  For example try importing the following LDCommPort test sample program JTCF968.000
Reply
#5
Ich habe den Code unter FXDZ308.000 veröffentlicht. Das Programm startet nun, aber der String wird nicht wie vorgesehen gesendet.
Reply
#6
OK, so it compiles, next is to do the simplest test (probably would have done that first).  I don't have your hardware obviously, but I would start with something as simple as possible that should test the connection.

Code:
result = LDCommPort.AvailablePorts()
TextWindow.WriteLine(result)

result = LDCommPort.OpenPort("COM5",115200)
TextWindow.WriteLine(result)

result = LDCommPort.SetEncoding("ASCII")
TextWindow.WriteLine(result)

result = LDCommPort.TXString("<t 3 50 1>")
TextWindow.WriteLine(result)

If this does work, then put TextWindow.WriteLine debug statements in your code to debug further.

If this doesn't work, then check the port, baudrate command and documentation (parity etc) for the hardware, and any return error messages.
Reply
#7
Hi Eddie,
I don't know if your DCC-EX central unit is an Arduino application or some other hardware.
The following COM checker should be able to find the Arduino.
If COM5 won't open, another program might have reserved it, so close it first.
I don't know if CR and LF are necessary, so maybe comment them out.


Code:
ArduinoCommand = "<s>"
ports = LDCommPort.AvailablePorts()
TextWindow.WriteLine(ports)
numberPorts = Array.GetItemCount(ports)
TextWindow.WriteLine(numberPorts)
For n = 1 to numberPorts
  TextWindow.WriteLine("Open " + Ports[n])
  result = LDCommPort.OpenPort(ports[n],115200)
  TextWindow.WriteLine(result)
  Program.Delay(100)
  TextWindow.WriteLine("Send " + ArduinoCommand)
  result = LDCommPort.TXString(ArduinoCommand)
  result = LDCommPort.TXByte(13) 'Send carriage return'
  result = LDCommPort.TXByte(10) 'Send linefeed'
  TextWindow.WriteLine(result) 
  Program.Delay(100)
  TextWindow.WriteLine("Read  " + ports[n])
  result = LDCommPort.RXAll()
  TextWindow.WriteLine(result)
EndFor
Reply
#8
(07-13-2025, 12:21 PM)Eddie Wrote: Ich habe den Code unter FXDZ308.000 veröffentlicht. Das Programm startet nun, aber der String wird nicht wie vorgesehen gesendet.

(translated by Google translator)

Hi, Eddie  Shy

You write: LDCommPort.TXString("<t "+ adresse + geschw +" 1 >")

If the program executes this code of yours, it will create a message like this:  <t adressegeschw 1 >

Is this the message you want to send? If I understand correctly, each command in the message should be separated by a space. For example like this:  <t adresse geschw 1>

But, for this, your line of code should look like this: LDCommPort.TXString("<t "+ adresse + " " + geschw +" 1>")

Blush
Reply
#9
Hello,
I found the error. It was due to the slider output, e.g., 30.12345667. These many decimal places prevented the string from being sent or further processed in the DCC-EX central unit. I've rounded it now, and it works. Thanks for your answers.
Regards, Eddie
[-] The following 2 users Like Eddie's post:
  • AbsoluteBeginner, litdev
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)