Python scripts for sensors

Post Reply
philw3
Posts: 3
Joined: 04.02.2019, 16:36

Python scripts for sensors

Post by philw3 » 04.02.2019, 16:54

Hi All,

Firstly it should be pointed out this is a very new thing to me, so its likely I'm doing something silly and you'll be able to correct me in 2 seconds, great! Please just bear with me and assume I'm an idiot!

Starting with the bit that works as background information:
I have a raspberry pi set up which is successfully detecting hall sensor feedback. This works fine, so its just the bit of code of what to do when a train is detected I'm having problems with.

For the sake of testing I've removed the below from the working sensor code, so I'm able to run it separately on demand, so its not a sensor fault. I have also added a new sensor and left its name alone for now, as I at one point thought my naming could be at fault.

Using the starting point of https://wiki.rocrail.net/doku.php?id=ro ... ripting-en I have successfully got a code that starts track power, what I then did was tried to modify that to change sensor status instead. I have ended up with the following code:

Code: Select all

#!/usr/bin/python

from socket import *
 
# Subroutine for adding the XML-Header and send it to the server
def sendMsg( s, xmlType, xmlMsg ):
  s.send("<xmlh><xml size=\"%d\" name=\"%s\"/></xmlh>%s" %(len(xmlMsg), xmlType, xmlMsg))
 
# Create the server connection
s = socket(AF_INET, SOCK_STREAM)
s.connect(('localhost', 8051))
 
# Compose the power on command and send it
rrMsg = "<fb id='fb1' state=\"true\"/>"
sendMsg( s, "fb", rrMsg )
 
# Close server connection
s.close()
This appears to match the commands here: http://rocrail.net/software/rocrail-sna ... en.html#fb but I can not for the life of me get it to work.

Am I missing something?
Any help much appreciated, and apologies is its a very stupid mistake

Thanks in advance,
Phil

rjversluis
Site Admin
Posts: 41142
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: XML scripts for sensors

Post by rjversluis » 04.02.2019, 16:58

Hi Phil,

try this:

Code: Select all

rrMsg = "<fb id=\"fb1\" state=\"true\"/>"

philw3
Posts: 3
Joined: 04.02.2019, 16:36

Re: XML scripts for sensors

Post by philw3 » 04.02.2019, 17:21

Hi,

Thanks for spotting that error! Unfortunately I'm still having no luck.

I can run the power on script with success still so its getting messages okay. So is not a simple connection problem either.

For clarity for people joining this thread, this is the code now after corrections from last reply:

Code: Select all

#!/usr/bin/python

from socket import *
 
# Subroutine for adding the XML-Header and send it to the server
def sendMsg( s, xmlType, xmlMsg ):
  s.send("<xmlh><xml size=\"%d\" name=\"%s\"/></xmlh>%s" %(len(xmlMsg), xmlType, xmlMsg))
 
# Create the server connection
s = socket(AF_INET, SOCK_STREAM)
s.connect(('localhost', 8051))
 
# Compose the power on command and send it
rrMsg = "<fb id=\"fb1\" state=\"true\"/>"
sendMsg( s, "fb", rrMsg )
 
# Close server connection
s.close()
Thanks

rjversluis
Site Admin
Posts: 41142
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Python scripts for sensors

Post by rjversluis » 04.02.2019, 17:53

Hi,

just check what the Server did receive.
Or post the error of Python... -o

philw3
Posts: 3
Joined: 04.02.2019, 16:36

Re: Python scripts for sensors

Post by philw3 » 05.02.2019, 14:22

Hi,

Sorry for taking up so much of your time being clearly stupid!

No error is shown in python

Attached is the output of the rocview trace (Hopefully this is helpful for you, apologies if not)

I assume the "no statistics for fb1" visible below from the trace is an indication something, or the sensor, isn't set up properly?

Code: Select all

20190205.130708.226 g9999I main     app      2114 sendToRocrail( <model cmd="modify" controlcode="" slavecode="">
  <fb id="fb1" x="16" y="13" z="0" state="false" identifier="" gpssid=" )
20190205.130708.227 g9999I main     app      0343 InitIndex
20190205.130708.242 g9999I main     app      1596 no statistics for fb1
<fb id="fb1" x="16" y="13" z="0" state="false" identifier="" gpssid="0" shortcut="false" counter="0" wheelcount="0" carcount="0" countedcars="0" bididir="0" val="0" load="0" regval="0" maxload="4" ori="north" baseaddr="0" prev_id="fb1" desc="" decid="" show="true" showid="true" road="false" curve="false" ignoresamestate="false" blockid="HL3" routeids="[EH+]-[HL3-],[ES+]-[HL3-],[HLA+]-[HL3-],[HL3-]-[EH+],[HL3-]-[ES+]" accnr="0" timer="0" zerocodedelay="0" operable="true" iid="" uidname="fb1" bus="0" addr="1" reg0="0" reg1="0" reg2="0" reg3="0" reg4="0" reg5="0" reg6="0" reg7="0" threshold="1" offset="" cutoutbus="0" cutoutaddr="0" fbtype="6" activelow="false" resetwc="false" ctciid="" ctcaddr="0" ctcport="0" ctcgate="0" ctcasswitch="false" gpsx="0" gpsy="0" gpsz="0" gpstolx="0" gpstoly="0" gpstolz="0"/>
Again apologies if this is a really newbie issue!

Thanks,
Phil
Attachments
rocview.002.trc
Rocview trace
(29 KiB) Downloaded 4 times

rjversluis
Site Admin
Posts: 41142
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Python scripts for sensors

Post by rjversluis » 05.02.2019, 14:30

Hi Phil,

the Rocview is of no interest.
Attach the Rocrail Server trace.

Post Reply

Return to “DIY Hardware”