Send over Serial Port with a Script

I’m Building a Mini-Router with a CCR2004-2XS-1G-PCIe in a littel Case. Now I want to add a little OLED to Display some Inforamtions like CPU Load, Temeperature, WAN and LAN IP Adresses. My Script works so far.

Haw can I send the INformations out of the Serial Port serial0?

Is there a better Solution than described here: Sending text out over a serial port - MikroTik Wiki?

I run Router OS 7.20beta2, since it was written in the Release notes, that scripting with serial port is possible now.

:local temp [/system/health get [find where name=cpu-temperature] value]
:local load [/system/resource get cpu-load]
:local totalmem [/system/resource get total-memory]
:local freemem [/system/resource get free-memory]
:local wanip [/ip/dhcp-client get [find where interface="sfp28-1"] address ]
:local lanip [/ip/dhcp-client get [find where interface="bridge1"] address ]
#/system serial-terminal serial0
:put ("CPU: " .$load."% / " . $temp . "°C")
:put ("RAM: " . 10000000000 / $totalmem *  ($totalmem-$freemem) /100000000 ."%")
:put ("IP-WAN: " .$wanip)
:put ("IP-LAN: " .[:pick $lanip 0 [:find $lanip "/"]])

It is depending a bit on what hardware you are using to control the display.
I presume something like an Arduino. When you write the software for that as well (instead of just using some standard “terminal program” that you cannot modify) you could configure the serial port as a console port, then make the external device do a “login” to the router and start a script.
From then on, anything the script sends with :put as in your example will go out the serial port.

Well there is a at-chat option on /ppp connected to a serial port, so you don’t have to abuse the init-string. But your issue with scripting from MikroTik side is that the “at-chat” is looking for an “OK” back from the device to terminate at-chat. @Sertik has some scripts on the forum where he built something on top of “at-chat” I believe.

If it’s a newer MikroTik, you should be able to use a /container. I have a project that does that here:
https://github.com/tikoci/serial2http

Thanks @Amm0 and @pe1chl for your Replys.
I’ll use an Arduino (or an ATtiny2313). So it yould be possible, to send Answers, like the OK. I rather won’t login as Terminal, but maybee it’s the best solution, since I still can use the serialport as emergency console.

Since my Hardware is a CCR2004-1G-2XS-PCIe-Card, i first have to solder the UART Connections. That shouldn’t be a problem.
But since I installed Router OS 7.20beta2 and I red in the Releasenotes:
v7.20beta [testing] is released! - Announcements - MikroTik community forum

*) console - added non-interactive (scriptable) serial-terminal support;

I hope there’s easy solution. But I couldn’t find further Infos to this new Function.

Also because it’s an PCIe Card and doesn’t have any USB-Ports. I can’t really use the Containers. Or can I?

I think it’s this to send using the new operator:
/system/serial-terminal/write input="my command" port=serial1

But exactly how you’d read a response IDK, TAB do not show any parameter you’d expect like terminator= or as-value

It’s possible the as-vale inmplied … I just know the commands, But have not tested this one.

:global x [/system/serial-terminal/write input="my command" port=serial1]; :put $x might work IDK.

I could just be early in beta and they didn’t finish some “read” operation yet..

Since I want to drive an external Display is it possible to send Data directly to the SPI or I2C (and drive the Display)?

The connector is showed here: Mikrotik CCR2004-1G-2XS-PCIe Actual CPU Model and Teardown Analysis - WuSiYu Blog

I don’t know on CCR2004, but some devices do support GPIO.