Serial Port Usage

From MikroTik Wiki
Jump to navigation Jump to search

Usage possibilities for serial ports in RouterOS

The most obvious (and default) usage is the serial console for RouterOS. On every RouterBoard or other system you will have a RouterOS login prompt on serial0 (the first serial interface). This is one of the ways to do the basic configuration on your RouterOS device after installation (i.e. at least give it an ip address or so).

There are other and more advanced uses for serial ports. I try to sum up those possibilities here.

You can use RouterOS to access another device (like a switch, for example) that is connected through a serial cable

  • directly from a RouterOS console or telnet/ssh login (i.e. "from within RouterOS")
  • by opening a telnet/ssh session that will get you directly on this device (without having to login to RouterOS first)
  • as if it were connected to a physical COM port of your PC

There's also a nasty workaround for sending text out over a serial port. Unfortunately RouterOS doesn't provide commands to write/read the serial port directly.

Preparations

Please note that there are some #Caveats you should be aware of! Take your time understanding those limits to avoid strange things to happen when connecting a device to a serial port on a RouterBoard.

First you have to have a free serial port to use for this. If you are on a device with only one serial port (like all RouterBoards, WRAP/ALIX boards etc.) you will have to disable the system console on this serial port:

/system console disable 0

Be sure to just disable the console rather than removing it, as RouterOS will recreate the console after the next reboot when you really remove it.

Next you will have to configure your serial port according to the serial port settings of the connected device. Using the following command you will set your serial port to 19200 Baud 8N1. What settings you need to use depends on the device you connect:

/port set serial0 baud-rate=19200 data-bits=8 parity=none stop-bits=1

You can also try to let RouterOS guess the needed baud rate by setting

/port set serial0 baud-rate=auto

Now's the time to connect your device if not already done. Usually you will have to use a null modem cable (the same thing as a cross-over-cable for Ethernet). Now we're ready to go!

Accessing a serial device from within RouterOS

The easiest way is the serial-terminal command:

/system serial-terminal serial0

This will give you access to the device you connected to port serial0. Ctrl-A is the prefix key, which means that you will enter a small "menu" by pressing Ctrl-A. The Ctrl-A character will not be sent to your device!

If you want to exit the connection to the serial device type Ctrl-A, then Q. This will return you to your RouterOS console.

If you really want to send a Ctrl-A character to your serial device you have to type Ctrl-A two times.

Accessing a serial device directly by SSH / telnet / mac-telnet

If you want to access the device directly without logging into RouterOS first, use the special-login command.

First create a new user for access to the serial device:

/user add name=serial password=secret group=read

Then connect this username to the desired serial port:

/special-login add user=serial port=serial0

Now when you connect to your RouterOS machine using SSH (or telnet/mac-telnet) and use the username/password of the user you just created, you will be connected to your serial device directly.

One nice side effect of this is that you could give this login to someone else who can access the serial device, but not RouterOS itself. (Or, if connecting by WinBox, couldn't change anything.)

Accessing a serial device as if it were physically connected to your PCs' COM port

Perhaps you are using a software to access your serial device that can only talk to COM ports like COM1?

Want to use this to access your device when it's located somewhere else?

The usual way would be to purchase something like a LanTronix or Moxa serial server, but you can do this for free with RouterOS, too!

RFC 2217 defines a protocol to transfer data from/to a serial device over TCP. Combine this with a software driver on your PC that presents this network connection as a "virtual COM port" and your done!

There are lots of software solutions for the PC side to do this, most of them commercial. One that works well with RouterOS in my experience is "Serial/IP COM Port Redirector" from Tactical Software (and no, I'm not affiliated with them anyhow ;-) ).

Serial to Ethernet Connector lets you share up to 255 serial port devices over network (Internet or LAN).

But there's also a free (like in beer) solution that you may even use commercially! It's named VSPE (virtual serial port emulator) and can be obtained from Eterlogic.

Also could be useful freeware RS232 Data Logger by Eltima

Configuring the RouterOS part

/port remote-access add port=serial0 protocol=rfc2217 tcp-port=10000

This will connect an incoming connection on TCP port 10000 to the port serial0.

You can restrict from what ip addresses you accept connections using the parameter address-range (as in "/ip service"). Using the "local-address" parameter you can specify on which local ip address you want to listen for incoming connections (if you have more than one ip address configured).

That's about everything we need to configure in RouterOS!

Configuring your PC

Now on to the PC part. Install the VPSE software from Eterlogic and start it.

You will see a screen like this:

File:Vpse-step1.jpg

Clicking on the "Create new device" button will get you here:

File:Vpse-step2.jpg

Make sure you selected device type "Connector" in the drop-down and press "Next":

File:Vpse-step3.jpg

Now select a (unused) COM port number which should be your new virtual COM port and press "Finish".

Make sure that the application you want to use to access this virtual port can open the COM port number you are about to create here. Older applications often only can be configured to use COM1-COM4, so creating COM9 could mean getting yourself into trouble.

Verify that the virtual COM port you created (COM3 in the screenshot) is shown as "Ready":

File:Vpse-step4.jpg

Next, press the "Create new device" button again and select "TcpClient" as device type:

File:Vpse-step5.jpg

Press "Next". In the next screen you will have to enter the ip address of your RouterOS machine (192.168.1.254 in the screenshot) and the port number you configured above (10000 in our example). Also make sure that you select the virtual COM port we created just before in the "Source serial port" drop-down:

File:Vpse-step6.jpg

Clicking on "Finish" will end our work and present you with a screen like this:

File:Vpse-step7.jpg

If the status of your TCPClient device doesn't show "OK", you either have an error in your RouterOS config, or you cannot reach the configured port on the RouterOS machine from your PC (network cabling? routing? firewall? ...).

If everything went well, you can now start any software and use our freshly created COM3 (in the example) to access your serial device connected to the RouterOS machine. Even if this would be located on another continent!

Caveats

There are some caveats with using the serial port on RouterBoards for the applications described above. Please read them carefully to understand how you might lock yourself out of your router!

Losing serial console access

By reconfiguring port serial0 on a RouterBoard as seen above, you will loose your serial console access to RouterOS. This means, that if you cannot access your RouterBoard over the network anymore, you might even have to reset the whole configuration of it to gain access again.

RouterBOOT entering the game

When rebooting a RouterBoard the bootloader (RouterBOOT) will always use the serial console (serial0 on RouterBoards) to send out some startup messages and offer access to the RouterBOOT menu.

Having text coming out of the serial port to the connected device might confuse your attached device. Furthermore in standard config you can enter RouterBOOT menu by pressing ANY key. So if your serial device sends any character to the serial port of your RouterBoard during boot time, the RouterBoard will enter the RouterBOOT menu and will NOT boot RouterOS unless you manually intervent!

You can reconfigure RouterBOOT to enter the RouterBOOT menu only when a DEL character is received - use this to reduce the chance to get a router that's stuck when rebooting!

Or if newer versions are used "Silent boot" feature can be used to suppress any output on the serial interface, including removal of booting sounds.