ultrasonic sensors for railway bars

As well as what @dougp has said it would help if you explain what you mean by "railway latch" and "portal".

I am reasonably familiar with railway modelling and I have not heard those terms used.

A diagram of what you are working with would be a big help.

...R