It can be done easily on a Linux machine, you can use a pty ("pseudo-teletype", where a serial port is a "real teletype"). From one end, open /dev/ptyp5, and then attach your program to /dev/ttyp5; ttyp5 act as a serial port, but will send/receive everything it does via /dev/ptyp5.
If you want to access and talk to file /dev/ttys2, then move your old /dev/ttys2 out of the way and make a symlink from ptyp5 to ttys2.
Yes, you can use some other number rather than ptyp5. You can prefer to pick one with a high number to avoid duplication because all of your login terminals will also be using ptys.
If you want to read more about ptys, visit this link.