Beispiel #1
0
        protected override bool SetupDevice()
        {
            m_timer.SetInterval(base.Interval);

            if (!OpenSerialPort())
            {
                return(false);
            }

            if (m_delayafteropen > 0)
            {
                m_stop.WaitOne(m_delayafteropen / 1000);
            }

            //bytes per channel
            m_bytes = 1;
            while (Math.Round(Math.Pow(256, m_bytes)) <= m_max)
            {
                m_bytes++;
            }

            //allocate a buffer, that can hold the prefix,the number of bytes per channel and the postfix
            m_buffsize = m_prefix.Count + m_channels.Count * m_bytes + m_postfix.Count;
            m_buff     = new byte[m_buffsize];

            //copy in the prefix
            if (m_prefix.Count > 0)
            {
                Array.Copy(m_prefix.ToArray(), m_buff, m_prefix.Count);
            }

            //copy in the postfix
            if (m_postfix.Count > 0)
            {
                Array.Copy(m_postfix.ToArray(), 0, m_buff, m_prefix.Count + m_channels.Count * m_bytes, m_postfix.Count);
            }
            //memcpy(m_buff + m_prefix.Count + m_channels.Count * m_bytes, &m_postfix[0], m_postfix.Count);

            //set channel bytes to 0, write it twice to make sure the controller is in sync
            //memset(m_buff + m_prefix.Count, 0, m_channels.Count * m_bytes);
            for (int i = 0; i < 2; i++)
            {
                if (m_serialport.Write(m_buff, m_buffsize) == -1)
                {
                    Util.LogError($"{Name}: {m_serialport.GetError()}");
                    return(false);
                }
            }

            return(true);
        }