Communicating with UCI engines

Discussion of chess software programming and technical issues.

Moderator: Ras

James Constance
Posts: 358
Joined: Wed Mar 08, 2006 8:36 pm
Location: UK

Communicating with UCI engines

Post by James Constance »

I'm developing a PGN reader & engine GUI using c# and wpf. So far I have a program that can more or less read the PGN files and display the games on a board. The next thing I'd like to do is pass positions to a UCI engine and display their analysis. However, I am still very much a beginner with the programming language and I'm picking things up as I go along.

So I was wondering if anyone could point me in the direction of any resources that explain how I'd go about getting my program to communicate with a UCI engine. As I said, I'm learning from scratch, and I have no knowledge how to do this.

Thanks!

James
User avatar
emadsen
Posts: 441
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Communicating with UCI engines

Post by emadsen »

The C# source code for my engine demonstrates how to do the opposite- respond to commands from GUI. Perhaps it could be of help. It demonstrates how to listen on standard input stream and respond on standard output stream, use threads, delegates, etc. Examine Interface\StreamListener.cs
Erik Madsen | My C# chess engine: https://www.madchess.net
James Constance
Posts: 358
Joined: Wed Mar 08, 2006 8:36 pm
Location: UK

Re: Communicating with UCI engines

Post by James Constance »

emadsen wrote:The C# source code for my engine demonstrates how to do the opposite- respond to commands from GUI. Perhaps it could be of help. It demonstrates how to listen on standard input stream and respond on standard output stream, use threads, delegates, etc. Examine Interface\StreamListener.cs
Thanks, Erik - that should be really helpful!
User avatar
hgm
Posts: 28452
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Communicating with UCI engines

Post by hgm »

Well, in a GUI it is a lot more involved, because the GUI is responsible for spawning the engine processes, and setting up pipes between the GUI and the engine.

I have no idea what C# is, however. The UCI2WB adapter is one of the simpler programs that launches engines that way, and is open source. But it is written in plain C, so I have no idea if that has any relevance for C#. In addition, these tasks could be platform dependent; on Linux you ned completely different code than in Windows API.

Anyway, should you be interested, its sources can be downloaded from my on-line source repository ( http://hgm.nubati.net/cgi-bin/gitweb.cgi ).
Joerg Oster
Posts: 991
Joined: Fri Mar 10, 2006 4:29 pm
Location: Germany
Full name: Jörg Oster

Re: Communicating with UCI engines

Post by Joerg Oster »

On the Shredder Homepage you can still download a full description of the UCI protocol.
http://download.shredderchess.com/div/uci.zip
Jörg Oster
lucasart
Posts: 3243
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Communicating with UCI engines

Post by lucasart »

James Constance wrote:I'm developing a PGN reader & engine GUI using c# and wpf. So far I have a program that can more or less read the PGN files and display the games on a board. The next thing I'd like to do is pass positions to a UCI engine and display their analysis. However, I am still very much a beginner with the programming language and I'm picking things up as I go along.

So I was wondering if anyone could point me in the direction of any resources that explain how I'd go about getting my program to communicate with a UCI engine. As I said, I'm learning from scratch, and I have no knowledge how to do this.

Thanks!

James
Understanding the UCI protocol is very simple, and so is implementing it from the point of view of the UCI engine. What is not trivial is the GUI side of things (whether UCI or Xboard).

You basically want to spawn processes (UCI engines) and "talk to their stdin / listen to their stdout". It is not as simple as people may imagine. Conceptually, here's what you need to do:
- spawn processes asynchronously: the children processes (UCI engines)
- in the parent you need to create 2 pipes for each child process. The first pipe is the one that the child process uses as stdin and the second one stdout. The parent must connect itself to one end of each of these pipes, and the child process must connect itself to the other end (both for stdin and stdout).
- then you communcate normally via stdin and stdout. But there are some unusual subtilties: 1/ read/writes are typically buffered, so you need to manually flush after write 2/ the problem of blocking reads in the parent process (what if an engine is irresponsive?)

Two possibilities:
1/ you are very lucky and C# has all this already managed for you, and you can use some .net classes or whatever these things are to do the job
2/ you are out of luck and you need to do everything yourself, just like in C, with system calls. On Unix like systems, you should look for (variants of) dup(), fork(), exec(). On Windows, I don't know.

I just googled for "spawning processes C#", and maybe that can help you get started:
http://stackoverflow.com/questions/2857 ... out-in-net
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
James Constance
Posts: 358
Joined: Wed Mar 08, 2006 8:36 pm
Location: UK

Re: Communicating with UCI engines

Post by James Constance »

Thanks HGM, Joerg, and Lucas! I dare say I'll have some more questions when I get my head round the basics!

James