/// <summary> /// 把父类的成员数据转换成字节填充到数组中 /// </summary> /// <param name="needFill">需要填充的数组</param> /// <returns>下标偏移的位置</returns> protected int FillBytes(byte[] needFill) { int offset = 0; Buffer.BlockCopy(MyBitConverter.GetBytes(this.Header), 0, needFill, offset, 2); offset += 2; needFill[offset++] = this.Len; needFill[offset++] = this.SerialNum; needFill[offset++] = this.Type; Buffer.BlockCopy(MyBitConverter.GetBytes(this.AgvId), 0, needFill, offset, 2); offset += 2; return(offset); }
public override byte[] GetBytes() { byte[] packData = new byte[Len]; int offset = FillBytes(packData); Buffer.BlockCopy(MyBitConverter.GetBytes(desPoint.X), 0, packData, offset, 4); offset += 4; Buffer.BlockCopy(MyBitConverter.GetBytes(desPoint.Y), 0, packData, offset, 4); offset += 4; packData[NeedLen() - 1] = CaculateCheckSum(packData); this.CheckSum = packData[NeedLen() - 1]; return(packData); }
public override byte[] GetBytes() { byte[] packData = new byte[Len]; int offset = FillBytes(packData); packData[offset++] = (byte)this.direction; Buffer.BlockCopy(MyBitConverter.GetBytes(speed), 0, packData, offset, 2); offset += 2; Buffer.BlockCopy(locations.GetBytes(), 0, packData, offset, 19); packData[NeedLen() - 1] = CaculateCheckSum(packData); this.CheckSum = packData[NeedLen() - 1]; return(packData); }
/// <summary> /// Read sub image header. /// </summary> /// <param name="stream">Stream to read local header from.</param> /// <param name="endianness">Big or little, as defined by TIFF header.</param> public ImageFileDirectory(Stream stream, MyBitConverter.Endianness endianness) : this() { var bytes = stream.ReadBytes(2); NumberOfEntries = MyBitConverter.ToUInt16(bytes, 0, endianness); FieldDescriptors = new List <FieldDescriptor>(); for (int i = 0; i < NumberOfEntries; i++) { bytes = stream.ReadBytes(12); FieldDescriptors.Add(new FieldDescriptor(bytes, endianness, stream)); } }
/// <summary> /// Phương thức này sẽ chạy khi Trigger được kích hoạt, không cần gọi tới nó /// </summary> /// <param name="taskInstance">hệ thống sẽ tự động tạo instance này khi chạy</param> public async void Run(IBackgroundTaskInstance taskInstance) { //nếu sử dụng một phương thức async nào trong hàm này, hàm run sẽ kết thúc trước khi phương thức đó thực hiện xong. // sử dụng defferal để thông báo với hệ thống rằng chưa được phép kết thúc phương thức run // nếu bạn không sử dụng phương thức async nào, bạn có thể bỏ defferal đi. BackgroundTaskDeferral defferal = taskInstance.GetDeferral(); System.Diagnostics.Debug.WriteLine("Đã nhận được toast"); // TODO: làm những thứ bạn muốn trong background task ở đây // Lưu ý: tất cả các phương thức async nào nằm ngoài khu này đều sẽ không thực hiện được string content = (taskInstance.TriggerDetails as RawNotification).Content; try { // lấy id long notificationid = long.Parse(content); System.Diagnostics.Debug.WriteLine("Notification ID : " + notificationid.ToString()); #region Bắt đầu liên lạc trực tiếp với server để lấy nội dung thông báo CommunicatePacket packet = new CommunicatePacket(CommunicateType.GetNotificationContent, new GetNotificationContentCommunicateData(notificationid)); try { UWPTCPClient.UWPTCPClient client = new UWPTCPClient.UWPTCPClient("115.74.126.7", "22112", ((data) => { MyBitConverter <SendObject> contentconverter = new MyBitConverter <SendObject>(); SendObject notifyContent = contentconverter.BytesToObject(data); UserCode.Run(notifyContent); // sau khi xử lí xong, ta thông báo với hệ thống là hàm run đã thực hiện xong và hệ thống có thể đóng hàm run lại //việc này đồng nghĩa với background task sẽ kết thúc defferal.Complete(); })); await client.ConnectAsync(); await client.SendAsync(new Models.MyBitConverter <CommunicatePacket>().ObjectToBytes(new CommunicatePacket(CommunicateType.GetNotificationContent, new GetNotificationContentCommunicateData(notificationid)))); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } #endregion } catch (Exception ex) { // đường truyền lỗi nên không nhận được id System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
public float GetFloat() { if (bytes == null) { return(0); } if (bytes.Length < start + 4) { return(0); } float result = MyBitConverter.ToFloat(bytes, start); start = start + 4; return(result); }
public int GetInt() { if (bytes == null) { return(0); } if (bytes.Length < start + 4) { return(0); } int result = MyBitConverter.ToInt32(bytes, start); start = start + 4; return(result); }
/// <summary> /// Read field descriptor from block. /// A "property" of the image. /// </summary> /// <param name="IDBlock">Block containing descriptor, but NOT it's data.</param> /// <param name="endianness">Big or little endianness defined by TIFF header.</param> /// <param name="dataStream">Full image stream to read descriptor data from.</param> public FieldDescriptor(byte[] IDBlock, MyBitConverter.Endianness endianness, Stream dataStream) { FieldTag = (FieldTags)MyBitConverter.ToInt16(IDBlock, 0, endianness); FieldType = (FieldTypes)MyBitConverter.ToInt16(IDBlock, 2, endianness); FieldLength = MyBitConverter.ToInt32(IDBlock, 4, endianness); DataOffset = MyBitConverter.ToInt32(IDBlock, 8, endianness); // Read data indicated by descriptor long oldOffset = dataStream.Position; dataStream.Seek(DataOffset, SeekOrigin.Begin); Data = new byte[FieldLength]; dataStream.Read(Data, 0, FieldLength); // Reset stream position for next descriptor dataStream.Seek(oldOffset, SeekOrigin.Begin); }
/// <summary> /// 初始化父类中的成员数据 /// </summary> /// <param name="ClassTpye">子类的名称</param> /// <param name="data">数据</param> protected ReceiveBasePacket(string ClassTpye, byte[] data) { if (data == null || data.Length < NeedLen()) { throw new PacketException(ClassTpye, ExceptionCode.DataMiss); } if (data[NeedLen() - 1] != CaculateCheckSum(data)) { throw new PacketException(ClassTpye, ExceptionCode.CheckSumError); } int offset = 0; this.Header = MyBitConverter.ToUInt16(data, ref offset); this.Len = data[offset++]; this.SerialNum = data[offset++]; this.Type = data[offset++]; this.AgvId = MyBitConverter.ToUInt16(data, ref offset); this.CheckSum = data[NeedLen() - 1]; }
public async Task Rotate(double angle) { if (IsBusy) { throw new InvalidOperationException(); } IsBusy = true; short angleShort = (short)(angle / Math.PI * 180); byte[] packet = new byte[4]; packet[0] = Magic; packet[1] = RotateCmd; MyBitConverter.GetBytesBE(angleShort).CopyTo(packet, 2); await WriteSerialAsync(packet, 0, packet.Length); await ReceiveAcknowledgment(); Angle += angle; IsBusy = false; }
internal Windows10RawNotificationSender(string packageSID, string secretKey, EventHandler <Exception> internetErrorcOccurred, EventHandler <DelegatesAndEnums.BadURIEventArgs> badURIErrorOccurred, EventHandler <Exception> notAccecptableErrorOccurred, EventHandler <Exception> unknowErrorOccurred, EventHandler <DeviceNotification> sendSuccessed, EventHandler <Exception> wrongPackageSIDOrSecretKey) { PackageSID = packageSID; SecretKey = secretKey; InternetOrFirewallErrorOccurred = internetErrorcOccurred; BadURIErrorOccurred = badURIErrorOccurred; NotAccecptableErrorOccurred = notAccecptableErrorOccurred; UnknowErrorOccurred = unknowErrorOccurred; SendSuccessed = sendSuccessed; WrongPackageSIDOrSecretKey = wrongPackageSIDOrSecretKey; token = new Windows10TokenAccess(PackageSID, SecretKey); Tconverter = new MyBitConverter <T>(); // mới bật chương trình lên thì làm mới lại token bất đồng bộ để tránh cản trở chương trình khởi động //Task.Run(() => //{ // while (true) // { // try // { // token.RenewToken(); // break; // } // catch (UnauthorizedAccessException uex) // { // if (WrongPackageSIDOrSecretKey(uex, new DelegatesAndEnums.ChoicesOnAuthenticationErrorOccurreddEventArgs(uex, ChangePackAgeSIDAndSecretKey)) == DelegatesAndEnums.TryAgainOrCancel.Cancel) // break; // } // catch // { // break; // } // } //}); }
/// <summary> /// Read header of JPG image. /// </summary> /// <param name="stream">Fully formatted JPG image.</param> /// <returns>Length of header.</returns> protected override long Load(Stream stream) { base.Load(stream); byte[] temp = stream.ReadBytes(HeaderSize); if (!CheckIdentifier(temp)) { throw new FormatException("Stream is not a BMP Image"); } DataSectionLength = BitConverter.ToInt16(temp, 4); Identifier = BitConverter.ToString(temp, 6, 5); Version = temp[11] + ".0" + temp[12]; ResolutionUnits = (UnitsType)temp[13]; HorizontalResolution = MyBitConverter.ToInt16(temp, 14, MyBitConverter.Endianness.BigEndian); VerticalResolution = MyBitConverter.ToInt16(temp, 16, MyBitConverter.Endianness.BigEndian); XThumbnailPixelCount = temp[18]; YThumbnailPixelCount = temp[19]; return(HeaderSize); }
/// <summary> /// Loads PNG header from stream. /// </summary> /// <param name="stream">Fully formatted header stream. Position not relevant, but not reset.</param> /// <returns>Header length.</returns> protected override long Load(Stream stream) { base.Load(stream); byte[] temp = stream.ReadBytes(HeaderSize); if (!CheckIdentifier(temp)) { throw new FormatException("Stream is not a PNG Image"); } PNGChunk header = new PNGChunk(temp); Width = MyBitConverter.ToInt32(header.ChunkData, 0, MyBitConverter.Endianness.BigEndian); Height = MyBitConverter.ToInt32(header.ChunkData, 4, MyBitConverter.Endianness.BigEndian); BitDepth = header.ChunkData[8]; colourType = (ColourType)header.ChunkData[9]; CompressionMethod = (CompressionMethods)header.ChunkData[10]; FilterMethod = (FilterMethods)header.ChunkData[11]; InterlaceMethod = (InterlaceMethdods)header.ChunkData[12]; return(-1); // Since we don't know the length of the entire header, no point returning any value. }
public LasPoint2Long(byte[] rawBytes, int startPosition = 0) : base(rawBytes, startPosition) { _red = (ushort)MyBitConverter.ToInt16(rawBytes, startPosition + 20); _green = (ushort)MyBitConverter.ToInt16(rawBytes, startPosition + 22); _blue = (ushort)MyBitConverter.ToInt16(rawBytes, startPosition + 24); }
private async Task SetReg32(int addr, int value) { byte[] bytes = MyBitConverter.GetBytesLE(value); await WriteAsync(addr, bytes, 0, bytes.Length); }