Writing your own software



ZMax exposes a TCP/IP data socket. You can connect to this socket using any programming language (MATLAB, Python, PHP, C++, C#, Java, Visual Basic etc).

All you need to do is open a TCP socket connection to the Hypnodyne Server and parse data bytes.

MATLAB INTEGRATION

Sample code is provided for MATLAB which clearly demonstrates the meaning of the bytes and how to scale data to obtain decimal values such as microvolts for the EEG channels and so forth.

 Download HDConnect.m

C# INTEGRATION

Sample functions are included for C# (.NET), please use in conjunction with Microsoft's asynchronous client socket example. A full project will be available soon.

 Download HDConnect.cs


		  			
private void AsyncClient_MessageReceived(AsyncClient a, string msg)
{
    string[] stringSeparators = new string[] { "\r\n", "\r", "\n", "\n\r" };
    string[] lines = msg.Split(stringSeparators, StringSplitOptions.None);
    for (int linen = 0; linen < lines.Length; linen++)
    {
        String line = lines[linen];
        bool isDataPacket = false;
        if (line != "")
        {
            if (line.StartsWith("_DONGLE_INSERTED"))
            {
                if (DongleInserted != null)
                    DongleInserted(null);
                if (Alert != null)
                    Alert(alert.Dongle, false);
            }
            if (line.StartsWith("ACKBYTES"))
            {
                if (AckReceived != null)
                {
                    AckReceived(null);
                }
            }
            if (line.StartsWith("NACKBYTES"))
            {
                if (NackReceived != null)
                {
                    NackReceived(null);
                }
            }
            if (line.StartsWith("_DONGLE_REMOVED"))
            {
                if (DongleRemoved != null)
                    DongleRemoved(null);
                if (Alert != null)
                    Alert(alert.Dongle, true);
            }
            if (line.StartsWith("RECEIVER_GONE"))
            {
                if (ReceiverGone != null)
                    ReceiverGone(null);
                if (Alert != null)
                    Alert(alert.FileCompromised, true);
            }
            if (line.StartsWith("DEBUG:"))
            {

            }
            else if (line[0] == 'D')
            {
                String dataline = line.Substring(1);
                string[] parts = dataline.Split('.');
                byte[] buf = HexToBytes(parts[1]);
                int packet_type = buf[0];
                if ((packet_type >= 1) && (packet_type <= 11))
                {
                    if (buf.Length == 40)
                    {
                        isDataPacket = true;
                        ProcessDataPacket(buf, true);
                        int eegrv = buf[1] * 256 + buf[2];
                        int eeglv = buf[3] * 256 + buf[4];
                        int dx = buf[5] * 256 + buf[6];
                        int dy = buf[7] * 256 + buf[8];
                        int dz = buf[9] * 256 + buf[10];
                        double scaled_eegr = ScaleEEG(eegrv);
                        double scaled_eegl = ScaleEEG(eeglv);
                        double scaled_dx = ScaleAccel(dx);
                        double scaled_dy = ScaleAccel(dy);
                        double scaled_dz = ScaleAccel(dz);
                    }
                }
            }
        }
        if (!isDataPacket)
        {
            if (DebugInfo != null)
                DebugInfo(GetDateTime() + " > " + line);
        }
    }
    a.Receive();
}

private double ScaleEEG(int x)
{
    double y = x;
    y = y - 32768;
    y = y * (3952);
    y = y / 65536;
    return y;
}


private double ScaleAccel(int x)
{
    double y = x;
    y = y * 4 / 4096 - 2;
    return y;
}