Click here to Skip to main content
Click here to Skip to main content

Making a Remote PC Slave for Debugging or Fun

By , 10 Jul 2012
 

Summary

Remotely controlling a PC to turn it on and off from software control on another PC.

Introduction

I'm working heavily on C# Open Source Operating System (Cosmos). While VMware is great, its even better to test on real hardware. To do this I wanted to make the deployment automatic, but this required some custom work. Booting could be done using PXE, and debugging through a null modem serial cable. But who wants to manually turn on and off the PC for every build?

To solve this problem I needed to find a way to turn the slave PC on and off using software on my main PC. A few alternatives were first considered.

WOL (Wake On LAN)

A magic packet can be sent across the network containing the MAC address of the PC to turn it on. However WOL has no facility to turn off or reset.

USB Power Controllers

Amazon has a bunch of USB and Ethernet based power switches that turn on and off a power socket. PCs can be programmed to restore after a power loss. However these tended to be very expensive, and the lower end ones had horrible comments about quality. These would also not provide perfect control for what I needed.

Custom Solution

I found a blog that discussed several attempts by an end user to do exactly what I was doing by using the lines of a serial port. Its actually more complicated than one would think because isolation is required using at minimum opto couplers. The schematic required some very specific parts however that were hard to come by.

Relay Controllers

I found several ready to go relay controllers for purchase. I evaluated many of them and finally settled on the CanaKit UK1104. It costs $59.95 plus shipping. It supports 4 relays, and also has input sensors which I can use to detect the PC state. This is actually important, because toggling the power switch is used to both turn on and off the PC, so I needed to know if I was about to turn it on, or off.

To use the relays, you also need a +9V or +12V power supply. I have hooked many 12V applications to the computer PSU, but since the device is actually used to turn the PC on, and there are no always on +12V lines on an ATX computer PSU, the additional power supply is needed.

Choosing a Motherboard

Everything else in the PC is standard and many are used parts I got from a computer repair facility. In my case I needed a few specific items on the motherboard:

  1. On board Ethernet with PXE boot. Most boards have this.
  2. Serial port, or serial header.
  3. Inexpensive.
  4. On board video

After some evaluation I settled on the Intel D425KT. Its a Mini-ITX board with on board serial, and a second serial available via headers. It has an on board Atom CPU and costs about $50 wholesale, or $60-$75 retail.

Building the PC

First, I built the PC and made sure it worked normally. Next step was to add the relay board and make some custom changes to the wiring. Assembled the PC looks like this.



Now lets examine the front panel header of the motherboard. Every motherboard's pin locations are different so its important to look in your manual for proper connections. Wrong connections can short out your board.


The connections we are interested in are in green and red. To make the connections easy, find an old case and get the power light and power switch. They will have connectors that can be used to ensure shorts do not occur. Sometimes they wont match exactly but they can be separated and leads can be extracted with a small knife and rearranged.


(Note, this is not the D425KT in this photo)

First we need to connect up the power switch to 6 and 8. Cut the power cable and wire it to COM and NO on one of the relays. I used relay 4. COM is ground, and NO is shorted to COM when the relay is on. NC is shorted to COM when the relay is off, but we don't need that in our application. Since it is a switch, there is no polarity and it doesn't matter which one pins 6 and 8 are connected to, so long as one is connected to COM and one to NO. The relays are screw down terminals, so no soldering is required, just a small screwdriver. I also spliced back the original physical switch and connected it directly to the relay to form the splice so I can still manually operate the PC.

Next we need to connect the Power LED. This is a +5 line that is powered when the PC is on. Its used to connect the power light, but since its +5V we can use it to sense if the PC is on or off. To do this cut the power line and attach the +5 line to channel 1 on the controller card. Only the +5 line is used, the ground remains only to the motherboard. On the line, the coloured one is used for positive and the plastic connector is usually marked as well.



If  one is not coloured and the other white, they may be red and black instead. Red is always positive, and black is always ground. Less common in these wires is the stripe system because they are smaller.



If they are both white, one will have a stripe. The one with the stripe is positive.

I also spliced the light back in this as well so I have a visible indicator as well. To attach to the board which has pins for sensors, I cut off the plastic connector from the hard drive light and used it. Not all the channels are equal and some use different voltage levels. For our use probably any would work (TTL or Schottky), but Schottky is a bit better for our use so I chose channel 1.



So after we are done it should look something like this.





Or the real life version.



Finally since we will boot the PC over the network and also use the serial for debugging, we have a few important connections on the back as well.



Software

OK, now we are ready to go! The relay controller installs as a USB to serial, which means it can be addressed with a terminal program or very easily with any programming language by using a serial port.



The relay controller operates on series of text commands listed in the manual. To test it we can now open a terminal program and issue some commands. This is TeraTerm, a free terminal program.





First we check the state of Channel 1. Its 0, this means the computer is off.

Then we have to toggle the switch since power switches are push and release. That is we simulate it being pushed and released by turning on the relay and turning it off. In software make sure you delay about 500ms between on and off for the motherboard to recognize the press. Remember, we are simulating a human finger.

Finally we check channel 1 again, and we can see the computer is now on.

One of the other relays could be hooked to the reset pins, but its not needed. To reset the PC, simply turn it off, then back on. To turn it off is the same as it is to turn it on, just "push" the button.

Video 

I have also posted a video of this system in action.

Source

The source code used in this video can be extracted easily from the Cosmos project source. Here is the relevant code as used in Cosmos (C# Open Source Operating System)

<pre wrap="true">using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
using Cosmos.Build.Common;

namespace Cosmos.Debug.VSDebugEngine.Host {
  public class Slave : Base {
    string mPortName;
    SerialPort mPort;
    Thread mPowerStateThread;

    public Slave(NameValueCollection aParams, bool aUseGDB)
      : base(aParams, aUseGDB) {
      var xPort = mParams[BuildProperties.SlavePortString];
      if (xPort == "None") {
        throw new Exception("No slave port is set.");
      }

      var xParts = xPort.Split(' ');
      mPortName = xParts[1];
    }

    string WaitForPrompt() {
      var xSB = new StringBuilder();
      char xLastChar = ' ';
      char xChar = ' ';
      while (true) {
        xLastChar = xChar;
        xChar = (char)mPort.ReadChar();
        xSB.Append(xChar);
        if (xChar == ':' && xLastChar == ':') {
          break;
        }
      }
      // Remove ::
      xSB.Length = xSB.Length - 2;
      return xSB.ToString();
    }

    void TogglePowerSwitch() {
      Send("REL4.ON");
      Thread.Sleep(500);
      Send("REL4.OFF");
    }

    bool IsOn() {
      var xResult = Send("CH1.GET").Split('\n');
      return xResult[1][0] == '1';
    }

    string Send(string aData) {
      // Dont use writeline, it only sends /n or /r (didnt bother to find out which, we need both)
      mPort.Write(aData + "\r\n");
      return WaitForPrompt();
    }

    void WaitPowerState(bool aOn) {
      int xCount = 0;
      while (IsOn() == !aOn) {
        Thread.Sleep(250);
        xCount++;
        // 5 seconds
        if (xCount == 20) {
          throw new Exception("Slave did not respond to power command.");
        }
      }
    }

    public override void Start() {
      mPort = new SerialPort(mPortName);
      mPort.Open();

      Send("");
      // Set to digital input
      Send("CH1.SETMODE(2)");

      if (IsOn()) {
        TogglePowerSwitch();
        WaitPowerState(false);
        // Small pause for discharge
        Thread.Sleep(1000);
      }

      TogglePowerSwitch();
      // Give PC some time to turn on, else we will detect it as off right away.
      WaitPowerState(true);

      if (OnShutDown != null) {
        mPowerStateThread = new Thread(delegate() {
          while (true) {
            Thread.Sleep(1000);
            if (!IsOn()) {
              mPort.Close();
              OnShutDown(this, EventArgs.Empty);
              break;
            }
          }
        });
        mPowerStateThread.Start();
      }
    }

    public override void Stop() {
      if (mPowerStateThread != null) {
        mPowerStateThread.Abort();
        mPowerStateThread.Join();
      }

      if (IsOn()) {
        TogglePowerSwitch();
        WaitPowerState(false);
      }
      mPort.Close();
    }
  }
} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chad Z. Hower aka Kudzu
Other
Cyprus Cyprus
Member
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
www.KudzuWorld.com
 
Formerly the Regional Developer Adviser (DPE) for Microsoft Middle East and Africa, he was responsible for 85 countries spanning 4 continents and 10 time zones. Now Chad is a Microsoft MVP.
 
Chad is the chair of several popular open source projects including Indy and Cosmos (C# Open Source Managed Operating System).
 
Chad is the author of the book Indy in Depth and has contributed to several other books on network communications and general programming.
 
Chad has lived in Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, and the United States. Chad has visited more than 60 countries, visiting most of them several times.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionLooks good ... but as for USB Power Controllers... [modified]professional i0022 May '13 - 1:23 
Netio USB Power Controllers are of good quality and I picked mine up for <$100 USD. for the 230CS[^]. They also have good programmability...
 
Hate to advertise on here ... but it seems a little easier than wiring up things in the way listed in this article, and not that much more expensive.
 
Kris


modified 2 days ago.

AnswerRe: Looks good ... but as for USB Power Controllers...memberChad Z. Hower aka Kudzu22 May '13 - 2:32 
I dont remember if it was in another comment thread or in the article but I looked at such devices. This device you mention is not widely available. Googling it turns up only a handful of sellers mostly in Czech Republic and not including shipping or customs the prices are $150 and up. Add shipping to a foreign country, taxes etc and I'd be lucky to get it for $250.
 
Furthermore one of the problems with all these devices is that you cant get the feedback from the power light, nor can you guarantee the device to come on easily. To get them to turn on you have to set the BIOS to "power on on power loss" and then toggle power, but if the device didnt lose power, then it wont power on when current is applied and the device becomes useless. So then it has to be combined with wake on LAN. So it becomes more complicated, is more limited, and far more expensive.
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
 
My Technical Stuff:
http://www.KudzuWorld.com
 
My Blogs:
http://www.KudzuWorld.com/blogs/

GeneralRe: Looks good ... but as for USB Power Controllers...professional i0022 May '13 - 12:44 
Chad Z. Hower aka Kudzu wrote:
I'd be lucky to get it for $250
Wow! Frown | :(
 
Chad Z. Hower aka Kudzu wrote:
if the device didnt lose power, then it wont power on
Most computers you can set to Auto power on: on power loss, always, or none.
 
Chad Z. Hower aka Kudzu wrote:
Furthermore one of the problems with all these devices is that you cant get the feedback from the power light
Fair point.
 


BTW I voted you up Thumbs Up | :thumbsup:
 
Kris

GeneralRe: Looks good ... but as for USB Power Controllers...memberChad Z. Hower aka Kudzu22 May '13 - 12:46 
i00 wrote:
Most computers you can set to Auto power on: on power loss, always, or none.

 
For power loss yes. But not if the computer was off before or was shut down normally. So in OS development, if the OS powers down, then these devices cannot power them back on. Generally these devices are used for powering off, but then wake on LAN is used to wake them back up.
 
i00 wrote:
BTW I voted you up

 
Thanks!
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
 
My Technical Stuff:
http://www.KudzuWorld.com
 
My Blogs:
http://www.KudzuWorld.com/blogs/

GeneralRe: Looks good ... but as for USB Power Controllers... [modified]professional i0022 May '13 - 15:07 
Chad Z. Hower aka Kudzu wrote:
For power loss yes. But not if the computer was off before or was shut down
normally

That's the always option in my computer BIOS... I know that I can shutdown my computer properly, then cycle the power on my Netio device and it will come back on (even if it was off prior to doing this).
 
EDIT: on the computer I am on now these are the options:
http://sarahagedcare.net/130523111716.png
*damn ... cant use img Poke tongue | ;-P
 
Power Off = always off when power is restored
Power On = always turn on when power is restored (regardless if the computer was on or off when the power was lost)
Last State = if it was running when power went out, restore power, else leave off
 
Kris


modified yesterday.

GeneralMy vote of 5memberp_a_k29 Jan '13 - 22:51 
Nice idea and very detailed article.
GeneralMy vote of 5memberYusuf Uzun16 Aug '12 - 0:16 
I like the idea, its very interesting and it should be life saver who work on remote pc.
GeneralMy vote of 5memberMihai MOGA8 Aug '12 - 5:45 
This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up once again.
Generalthats something amazingmemberXmen W.K.14 Jul '12 - 18:29 
loved it how you handled it, but is it possible with bluetooth ? I'm no expect in hardware thing, maybe there is way ?
 
+5 btw and b'marked

TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can

GeneralRe: thats something amazingmemberChad Z. Hower aka Kudzu15 Jul '12 - 2:33 
Why would you want to use Bluetooth? Sure its possible, but it would be very complicated and increase the cost a LOT.
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
 
My Technical Stuff:
http://www.KudzuWorld.com
 
My Blogs:
http://www.KudzuWorld.com/blogs/

GeneralRe: thats something amazingmemberXmen W.K.15 Jul '12 - 2:38 
Chad Z. Hower aka Kudzu wrote:
Why would you want to use Bluetooth?

to go wireless

TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can

GeneralMy vote of 4memberGeekForChrist11 Jul '12 - 4:40 
Pretty Cool!
GeneralA socket application can achieve this [modified]memberYajnesh Narayan Behera2 Jul '12 - 20:50 
Such a nicely explained article, going to help a lot. In past, I have developed such an utility, & was awarded few bucks.
 
The aim of the application was to remotely power on & off systems (Windows & Linux). My feedback is, this can be achieved through a socket application, no hardware is required.

modified 3 Jul '12 - 3:03.

GeneralRe: A socket application can achieve thismemberChad Z. Hower aka Kudzu3 Jul '12 - 4:03 
You didn't read the article very carefully. I assume you are speaking of WOL (wake on LAN) which I discussed in the article and why it won't work.
 
This technique is used mostly for debugging booting systems. When the system boots up and locks up, crashes or otherwise becomes unresponsive, how are you going to power it off or reset it? WOL has no provisions for this.
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
 
My Technical Stuff:
http://www.KudzuWorld.com
 
My Blogs:
http://www.KudzuWorld.com/blogs/

GeneralRe: A socket application can achieve thismemberYajnesh Narayan Behera3 Jul '12 - 7:40 
I missed the benefits this application is going to provide in terms of debugging. Yes, that's right, WOL was used to power-on & client-server application was there to power-off system. That app saved millions for my organization.
 
I landed here, designing a project to remotely(from my office) control system, tv, fridge etc.
GeneralMy vote of 5memberDr Bob2 Jul '12 - 14:10 
This totally awesome! I shall be singing your praises for years to come! The simple solution is often the most difficult to invent.
GeneralMy vote of 5membernewton.saber2 Jul '12 - 4:07 
This is a great article. Well written, great illustrations and a fantastic topic. I am sure this took you quite some time to write and I appreciate all the time you put into it to make it so great. Thanks for sharing.
Keep on learning, keep on coding.
~Newton
QuestionThat's pretty wild stuffmvpSacha Barber1 Jul '12 - 22:28 
Cosmos is a bit nuts (in a good way)
Sacha Barber
  • Microsoft Visual C# MVP 2008-2012
  • Codeproject MVP 2008-2012
Open Source Projects
Cinch SL/WPF MVVM

Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 10 Jul 2012
Article Copyright 2012 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid