コード例 #1
0
ファイル: FrameManager.cs プロジェクト: jionfull/SensorTst
 public static void DeleteFrame(FrameUnit frameUnit)
 {
     lock (listFrame)
     {
         listFrame.Remove(frameUnit);
     }
 }
コード例 #2
0
 public static void DeleteFrame(FrameUnit frameUnit)
 {
     lock (listFrame)
     {
         listFrame.Remove(frameUnit);
     }
 }
コード例 #3
0
        internal static bool WrFactoryMessage(byte cardAddr, byte[] message)
        {
            byte[]    dataBuffer = new byte[4000];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0xE2);

            byte[] buffer = new byte[1 + 16];
            buffer[0] = 0x50;
            Array.Copy(message, 0, buffer, 1, 16);

            Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE2, buffer, buffer.Length);

            try
            {
                if (frameUnit.WaitData(3000) == false)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            catch (ThreadAbortException)
            {
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(false);
        }
コード例 #4
0
ファイル: FrameManager.cs プロジェクト: jionfull/SensorTst
        public static FrameUnit CreateFrame(byte[] buffer,int cmd)
        {
            FrameUnit frameUnit = new FrameUnit(buffer, cmd);

            lock (listFrame)
            {
                listFrame.Add(frameUnit);
            }
            return frameUnit;
        }
コード例 #5
0
        public static FrameUnit CreateFrame(byte[] buffer, int cmd)
        {
            FrameUnit frameUnit = new FrameUnit(buffer, cmd);

            lock (listFrame)
            {
                listFrame.Add(frameUnit);
            }
            return(frameUnit);
        }
コード例 #6
0
        internal static bool RdTsterMessage(byte cardAddr, out string tster)
        {
            tster = String.Empty;

            byte[]    dataBuffer = new byte[4000];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0xE4);

            byte[] buffer = new byte[1];
            buffer[0] = 0x51;
            Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE4, buffer, 1);

            try
            {
                if (frameUnit.WaitData(3000) == false)
                {
                    return(false);
                }
                else
                {
                    if (frameUnit.FrameLength < 8 + 16 + 4)
                    {
                        return(false);
                    }
                    int charCount = 16;
                    for (int i = 8; i < frameUnit.FrameLength - 4 && i < (8 + 16); i++)
                    {
                        if (dataBuffer[i] == 0x00 || dataBuffer[i] == 0xff)
                        {
                            charCount = i - 8;
                            break;
                        }
                    }
                    try
                    {
                        tster = Encoding.Default.GetString(dataBuffer, 8, charCount);
                    }
                    catch
                    {
                    }

                    return(true);
                }
            }
            catch (ThreadAbortException)
            {
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(false);
        }
コード例 #7
0
        static public bool ReadAnalogCh(byte boardAddr, byte ch, out Int16 val, out float freq, out float lowFreq)
        {
            byte[]    dataBuffer = new byte[1000];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0x30); //接收任何数据帧
            bool      ret;

            try
            {
                byte[] buffer = new byte[1];
                buffer[0] = ch;


                Protocol.FrameTransmit.Send(1, boardAddr, 0x30, buffer, 1);
                if (frameUnit.WaitData(3000) == false)
                {
                    val     = 0;
                    ret     = false;
                    freq    = 0;
                    lowFreq = 0;
                }
                else
                {
                    val      = dataBuffer[9];
                    val     *= 256;
                    val     += dataBuffer[8];
                    ret      = true;
                    freq     = dataBuffer[11];
                    freq    *= 256;
                    freq    += dataBuffer[10];
                    freq    *= 0.1f;
                    lowFreq  = dataBuffer[13];
                    lowFreq *= 256;
                    lowFreq += dataBuffer[12];
                    lowFreq *= 0.1f;
                }
            }
            catch (ThreadAbortException)
            {
                ret     = false;
                val     = 0;
                freq    = 0;
                lowFreq = 0;
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(ret);
        }
コード例 #8
0
        static bool WriteDebugReg(byte boardAddr, UInt16 addr, UInt16 val)
        {
            bool ret;

            byte[]    rxBuf     = new byte[1000];
            FrameUnit frameUnit = FrameManager.CreateFrame(rxBuf, 0xf1);

            try
            {
                byte[] buffer = new byte[4];



                buffer[0] = (byte)(addr & 0xff);
                buffer[1] = (byte)(addr >> 8);
                buffer[2] = (byte)(val & 0xff);
                buffer[3] = (byte)(val >> 8);
                Protocol.FrameTransmit.Send(1, boardAddr, 0xf1, buffer, 4);

                if (frameUnit.WaitData(3000) == false)
                {
                    ret = false;
                }
                else
                {
                    UInt16 temp;
                    temp = (UInt16)((UInt16)rxBuf[9] + (UInt16)(rxBuf[10] * 256));
                    if (temp == val)
                    {
                        ret = true;
                    }
                    else
                    {
                        ret = false;
                    }
                }
            }
            catch
            {
                ret = false;
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(ret);
        }
コード例 #9
0
        internal static bool RdVerMessage(byte cardAddr, out string verMessage)
        {
            byte[] buffer = new byte[1];
            buffer[0] = (byte)0x01;
            Protocol.FrameTransmit.Send(1, cardAddr, 7, buffer, 1);
            StringBuilder sb = new StringBuilder();

            byte[]    dataBuffer = new byte[40];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0x07);

            if (frameUnit.WaitData(2000) == false)
            {
                verMessage = "查询失败";
            }
            else
            {
                verMessage = ("版本号:" + dataBuffer[8].ToString() + "." + dataBuffer[9].ToString() + "\r\n"
                              + "版本日期:" + dataBuffer[10].ToString() + "年" + dataBuffer[11].ToString() + "月" + dataBuffer[12].ToString());
            }
            return(true);
        }
コード例 #10
0
        internal static bool WriteCoeff(byte cardAddr, short[] CoeffValues)
        {
            byte[]    dataBuffer = new byte[4000];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0xE2);

            byte[] buffer = new byte[1 + CoeffValues.Length * 2];
            buffer[0] = 0x52;
            for (int i = 0; i < CoeffValues.Length; i++)
            {
                Int16 val;

                val = CoeffValues[i];


                buffer[i * 2 + 1] = (byte)(val & 0xff);
                buffer[i * 2 + 2] = (byte)((val >> 8) & 0xff);
            }
            Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE2, buffer, buffer.Length);

            try
            {
                if (frameUnit.WaitData(3000) == false)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            catch (ThreadAbortException)
            {
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(false);
        }
コード例 #11
0
        static public bool ReadDebugReg(byte boardAddress, UInt16 addr, out UInt16 val)
        {
            byte[]    dataBuffer = new byte[100];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0xf0);
            bool      ret;

            try
            {
                byte[] buffer = new byte[2];

                buffer[0] = (byte)(addr & 0xff);
                buffer[1] = (byte)(addr >> 8);

                Protocol.FrameTransmit.Send(1, boardAddress, 0xF0, buffer, 2);
                if (frameUnit.WaitData(3000) == false)
                {
                    val = 0;
                    ret = false;
                }
                else
                {
                    val  = dataBuffer[10];
                    val *= 256;
                    val += dataBuffer[9];
                    ret  = true;
                }
            }
            catch (ThreadAbortException)
            {
                ret = false;
                val = 0;
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            Thread.Sleep(20);
            return(ret);
        }
コード例 #12
0
        internal static bool RdScaleList(byte cardAddr, out Int16[] chValues)
        {
            byte[]    dataBuffer = new byte[4000];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0xE4);

            byte[] buffer = new byte[1];
            buffer[0] = 0x52;
            Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE4, buffer, 1);
            chValues = null;
            try
            {
                if (frameUnit.WaitData(3000) == false)
                {
                    return(false);
                }
                else
                {
                    if (frameUnit.FrameLength < 20)
                    {
                        return(false);
                    }
                    chValues = new Int16[(frameUnit.FrameLength - 4 - 8) / 2];
                    for (int i = 8; i < frameUnit.FrameLength - 4; i += 2)
                    {
                        chValues[(i - 8) / 2] = (Int16)(dataBuffer[i] + dataBuffer[i + 1] * 256);
                    }
                    return(true);
                }
            }
            catch (ThreadAbortException)
            {
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(false);
        }
コード例 #13
0
        internal static bool RdIdMessage(byte cardAddr, out String id, out DateTime calcTime)
        {
            id       = String.Empty;
            calcTime = DateTime.MinValue;
            byte[]    dataBuffer = new byte[4000];
            FrameUnit frameUnit  = FrameManager.CreateFrame(dataBuffer, 0xE4);

            byte[] buffer = new byte[1];
            buffer[0] = 0x50;
            Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE4, buffer, 1);

            try
            {
                if (frameUnit.WaitData(3000) == false)
                {
                    return(false);
                }
                else
                {
                    if (frameUnit.FrameLength < 8 + 16 + 4)
                    {
                        return(false);
                    }
                    int charCount = 12;
                    for (int i = 8; i < frameUnit.FrameLength - 4 && i < (8 + 12); i++)
                    {
                        if (dataBuffer[i] == 0x00 || dataBuffer[i] == 0xff)
                        {
                            charCount = i - 8;
                            break;
                        }
                    }
                    try
                    {
                        id = Encoding.Default.GetString(dataBuffer, 8, charCount);
                    }
                    catch
                    {
                    }
                    try
                    {
                        int year = dataBuffer[8 + 12] + (dataBuffer[8 + 13] << 8);
                        calcTime = calcTime.AddYears(year - 1);
                        calcTime = calcTime.AddMonths(dataBuffer[8 + 14] - 1);
                        calcTime = calcTime.AddDays(dataBuffer[8 + 15] - 1);
                    }
                    catch
                    {
                        calcTime = calcTime.AddYears(2000);
                    }
                    return(true);
                }
            }
            catch (ThreadAbortException)
            {
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(false);
        }
コード例 #14
0
        static public bool SendParam(byte[] paramAry)
        {
            bool ret = true;

            byte[]    dataBuffer = new byte[40000];
            byte[]    frameBuffer = new byte[40000];
            int       LimitValue, paramLength;
            byte      boardAddr = paramAry[0];
            FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 1);

            LimitValue = GetLimitValue(paramAry[1]);
            try
            {
                paramLength = paramAry.Length;
                if (paramAry.Length > 0)
                {
                    int startIndex = 0;
                    while (paramLength > 0)
                    {
                        if (paramLength > LimitValue)
                        {
                            frameBuffer[0] = (byte)(startIndex & 0xff);
                            frameBuffer[1] = (byte)((startIndex >> 8) & 0xff); //帧序号
                            frameBuffer[2] = paramAry[1];
                            frameBuffer[3] = 0;
                            frameBuffer[4] = 0;
                            for (int i = 0; i < LimitValue; i++)
                            {
                                frameBuffer[5 + i] = paramAry[startIndex * LimitValue + i];
                            }
                            ushort crc = Helper.CRC16.ComputeCRC16(frameBuffer, 0, LimitValue + 5);
                            frameBuffer[LimitValue + 5] = (byte)(crc & 0xff);
                            frameBuffer[LimitValue + 6] = (byte)((crc >> 8) & 0xff);
                            //CPU板地址为1
                            Protocol.FrameTransmit.Send(1, paramAry[0], 0x01, frameBuffer, LimitValue + 7);
                            startIndex++;
                            if (frameUnit.WaitData(500) == false)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            frameBuffer[0] = (byte)(startIndex & 0xff);
                            frameBuffer[1] = (byte)(((startIndex >> 8) & 0xff) | 0x80); //帧序号,bit15为1代表最后一帧
                            frameBuffer[2] = paramAry[1];
                            frameBuffer[3] = 0;
                            frameBuffer[4] = 0;
                            for (int i = 0; i < paramLength; i++)
                            {
                                frameBuffer[5 + i] = paramAry[startIndex * LimitValue + i];
                            }
                            ushort crc = Helper.CRC16.ComputeCRC16(frameBuffer, 0, paramLength + 5);
                            frameBuffer[paramLength + 5] = (byte)(crc & 0xff);
                            frameBuffer[paramLength + 6] = (byte)((crc >> 8) & 0xff);
                            //CPU板地址为1
                            Protocol.FrameTransmit.Send(1, paramAry[0], 0x01, frameBuffer, paramLength + 7);
                            //发送完毕
                            startIndex++;
                            if (frameUnit.WaitData(500) == false)
                            {
                                return(false);
                            }
                        }
                        paramLength -= LimitValue;
                    }
                }
                else
                {
                    frameBuffer[0] = (byte)(0 & 0xff);
                    frameBuffer[1] = (byte)(((0 >> 8) & 0xff) | 0x80); //帧序号,bit15为1代表最后一帧
                    frameBuffer[2] = paramAry[1];
                    frameBuffer[3] = 0;
                    frameBuffer[4] = 0;
                    frameBuffer[5] = paramAry[0];
                    frameBuffer[6] = paramAry[1];
                    ushort crc = Helper.CRC16.ComputeCRC16(frameBuffer, 0, 7);
                    frameBuffer[7] = (byte)(crc & 0xff);
                    frameBuffer[8] = (byte)((crc >> 8) & 0xff);
                    Protocol.FrameTransmit.Send(1, paramAry[0], 0x01, frameBuffer, 9);
                    if (frameUnit.WaitData(500) == false)
                    {
                        return(false);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                ret = false;
            }
            finally
            {
                FrameManager.DeleteFrame(frameUnit);
            }
            return(ret);
        }