Пример #1
0
 public void Read(Speed speed, UInt16 address, byte[] buffer, int offset = 0, UInt16 length = 0)
 {
     lock (Daq) {
         int byteCount = buffer.Length;
         if (length != 0 && length < buffer.Length)
         {
             byteCount = length;
         }
         if (byteCount > offset)
         {
             byteCount -= offset;
         }
         while (byteCount != 0)
         {
             UInt16 byteChunk = (UInt16)Math.Min(MaxByteChunk, byteCount);
             BasicTypeSerializer.Put(SendContext, (byte)NwazetDAQ.Command.I2c);
             BasicTypeSerializer.Put(SendContext, (byte)Command.Read);
             BasicTypeSerializer.Put(SendContext, (byte)speed);
             BasicTypeSerializer.Put(SendContext, (UInt16)address);
             BasicTypeSerializer.Put(SendContext, byteChunk);
             Daq.Execute();
             Daq.Receive();
             CheckResult();
             ReceiveContext.CopyBytesFromInternalBuffer(buffer, offset, byteChunk);
             offset    += byteChunk;
             byteCount -= byteChunk;
         }
     }
 }
Пример #2
0
 public UInt16 ReadFile(UInt16 fileId, byte[] buffer, int bytesToRead)
 {
     lock (Daq) {
         int    byteCount      = bytesToRead;
         UInt16 bytesRead      = 0;
         UInt16 totalBytesRead = 0;
         int    offset         = 0;
         while (byteCount != 0)
         {
             UInt16 byteChunk = (UInt16)Math.Min(MaxByteChunk, byteCount);
             BasicTypeSerializer.Put(SendContext, (byte)NwazetDAQ.Command.FileSystem);
             BasicTypeSerializer.Put(SendContext, (byte)Command.ReadFile);
             BasicTypeSerializer.Put(SendContext, fileId);
             BasicTypeSerializer.Put(SendContext, byteChunk);
             Daq.Execute();
             Daq.Receive();
             CheckResult();
             bytesRead = BasicTypeDeSerializer.Get(ReceiveContext, bytesRead);
             ReceiveContext.CopyBytesFromInternalBuffer(buffer, offset, bytesRead);
             totalBytesRead += bytesRead;
             offset         += bytesRead;
             byteCount      -= bytesRead;
             if (bytesRead < byteChunk)
             {
                 break;
             }
         }
         return(totalBytesRead);
     }
 }
Пример #3
0
        public UsartReadResult Read(byte[] buffer, int offset = 0, int length = 0, UInt32 totalTimeoutMs = 0, bool useTerminator = false, byte terminator = 0)
        {
            lock (Daq) {
                var result    = new UsartReadResult();
                int byteCount = buffer.Length;
                if (length != 0 && length < buffer.Length)
                {
                    byteCount = length;
                }
                if (byteCount > offset)
                {
                    byteCount -= offset;
                }
                while (byteCount != 0)
                {
                    UInt16 byteChunk = (UInt16)Math.Min(MaxByteChunk, byteCount);
                    BasicTypeSerializer.Put(SendContext, (byte)NwazetDAQ.Command.Usart);
                    BasicTypeSerializer.Put(SendContext, (byte)Command.Read);
                    BasicTypeSerializer.Put(SendContext, byteChunk);
                    BasicTypeSerializer.Put(SendContext, totalTimeoutMs);
                    BasicTypeSerializer.Put(SendContext, (byte)((useTerminator) ? 1 : 0));
                    BasicTypeSerializer.Put(SendContext, (byte)terminator);
                    Daq.Execute();
                    Daq.Receive();
                    byte   returnCode = BasicTypeDeSerializer.Get(ReceiveContext);
                    UInt16 bytesRead  = 0;
                    bytesRead = BasicTypeDeSerializer.Get(ReceiveContext, bytesRead);
                    ReceiveContext.CopyBytesFromInternalBuffer(buffer, offset, bytesRead);
                    result.TotalBytesRead += bytesRead;
                    offset    += bytesRead;
                    byteCount -= bytesRead;
                    switch ((Result)returnCode)
                    {
                    case Result.InvalidParameter:
                        throw new UsartException((int)returnCode, ResultStrings[returnCode]);

                    case Result.TerminatorReceived:
                        result.TerminatorReceived = true;
                        return(result);

                    case Result.Timeout:
                        result.TimedOut = true;
                        return(result);
                    }
                }
                return(result);
            }
        }