by

Serial Port Communication with the Alpha Clock Five

If you’ve read any documentation on the Alpha Clock Five you may have noticed that the engineer who originally developed the clock have provided you with some basic commands to facilitate serial port communication.

In this blog post I would like to cover the ST (or Set Time) command and write a basic Windows Forms application that will allow you to set the time on your clock to the time on your computer.

According to the docs:

ST: Set Time

The set time function is used to set the time displayed on Alpha Clock Five.

The full command consists of 13 bytes, as follows:

  • The header byte, 0xFF
  • The command ST
  • A 10-digit string giving the number of seconds since Jan 1 1970

The number of seconds since Jan 1 1970 is an implementation of a unix time string. As an example, the 10-digit string 1324758327 represents the time 20:25:27, Saturday December 24.

The Alpha Clock Five library (v. 2.0 and above) includes a set of Processing sketches, including an example program (AlphaClock_SetTime) that can sync the time on your Alpha Clock Five to that on your PC. (Sample applications to communicate with Alpha Clock Five are provided as .pde files, for use with the Processing development environment. Processing is a free, cross-platform programming environment, and you can download your copy at processing.org.)

 

So how do we turn that into a Windows Forms application? Easy.

Using the System.IO.Ports library all we need to do is create a SerialPort object with the proper COM port and baud rate set. Then write the proper bytes to the output stream and viola, we’ll be able to set the time from our computer. You can find the COM port under the Device Manager in Windows. Your FTDI Friend or other compatible Serial to TTL device should be listed under Ports (COM & LPT). My FTDI Friend is currently on COM3. The Alpha Clock documentation states that the baud rate for serial communication for both firmware 1.0 and 2.0 is 19200 baud.

It is useful to know a byte of hexadecimal for this next bit xD

What’s the header byte 0xFF?

0xFF is 255 in hex.

This means that the header byte is equivalent to a byte array with one byte set to the value 255.

How do we get the Unix Timestamp?

It is possible to do a few different ways. My solution is to create a DateTime equal to January 1st, 1970 0:00:00:000 (that’s 12AM) and then subtract it from DateTime.Now to get a TimeSpan object. Then you can get the total seconds from the TotalSeconds field of the TimeSpan. Cast to a long datatype and you’re golden!

Here is the codebehind for the Windows Forms application that I wrote today after work:

using System;
using System.Windows.Forms;
using System.IO.Ports;

namespace SyncTime
{
    public partial class Form1 : Form
    {

        SerialPort serialPort;
        private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort = new SerialPort("COM3",19200);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            long timestamp = ConvertToTimestamp(DateTime.Now);
            serialPort.Open();
            serialPort.Write(new byte[] { 255 }, 0, 1);
            serialPort.Write($"ST{timestamp}");
            serialPort.Close();
        }

        private static long ConvertToTimestamp(DateTime value)
        {
            TimeSpan elapsedTime = value - Epoch;
            return (long)elapsedTime.TotalSeconds;
        }

    }
}

Write a Comment

Comment