⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seriallcd.cs

📁 gps首先手机要有上网的硬件条件,然后获取某一网络地址映射的GUID ,最后通过调用ConnMgrEstablishConnectionSync函数让手机自己选择某一最 佳路径.
💻 CS
📖 第 1 页 / 共 2 页
字号:
							expectedPacketLengthIsSet = false;
							if (bytesRead == expectedPacketLength)
							{
								//The buffer contains only the bytes for this packet
								bytesRead = 0;
								bufferIndex = startPacketIndex;
							}
							else
							{
								//The buffer also has bytes for the next packet
								startPacketIndex += expectedPacketLength;
								startPacketIndex %= receiveBuffer.Length;
								bytesRead -= expectedPacketLength;
								bufferIndex = startPacketIndex + bytesRead;
							}
						}
					}

					bufferIndex %= receiveBuffer.Length;
					numBytesToRead = bufferIndex < startPacketIndex ? startPacketIndex - bufferIndex : receiveBuffer.Length - bufferIndex;
				}
			}
			catch (IOException)
			{
				// abort the thread
				System.Threading.Thread.CurrentThread.Abort();
			}
			catch (ObjectDisposedException)
			{
				if (receiveThread != null)
				{
					receiveThread = null;
				}
			}
		}
		private void ReportEventHandler()
		{
			try
			{
				LCDPacket packet = null;

				while (true)
				{
					while (null == packet)
					{
						lock (reportSignal)
						{
							if (0 != reportQueue.Count)
								packet = reportQueue.Dequeue();
							else
								Monitor.Wait(reportSignal);
						}
					}

					switch (packet.Type)
					{
						case KEY_ACTIVITY_REPORT:
							ReportKeyActivityEventHandler(packet);
							break;

						case TEMPATURE_SENSOR_REPORT:
							break;
					}
					packet = null;
				}
			}
			catch (IOException)
			{
				// abort the thread
				System.Threading.Thread.CurrentThread.Abort();
			}
			catch (ObjectDisposedException)
			{
				if (eventThread != null)
				{
					eventThread = null;
				}
			}
		}

		public LCDKey Key
		{
			get
			{
				return _key;
			}
			set
			{
				_key = value;
			}
		}

		private void ReportKeyActivityEventHandler(LCDPacket packet)
		{
			if (1 != packet.DataLength)
			{
				return;
			}

			switch (packet.Data[0])
			{
				case 1 :
					Key = LCDKey.Up;
					break;

				case 2 :
					Key = LCDKey.Down;
					break;

				case 3 :
					Key = LCDKey.Left;
					break;

				case 4 :
					Key = LCDKey.Right;
					break;

				case 5 :
					Key = LCDKey.Enter;
					break;

				case 6 :
					Key = LCDKey.Exit;
					break;

				case 7 :
					break;

				case 8 :
					break;

				case 9 :
					break;

				case 10 :
					break;

				case 11 :
					break;

				case 12 :
					break;
			}
		}

		private void ReportKeyActivityEventHandler(KeyActivityEventHandler keyActivityEventHandler)
		{
			try
			{
				if (null != keyActivityEventHandler)
					keyActivityEventHandler(this, null);
			}
			catch { }
		}

		private LCDPacket CreatePacket(byte[] buffer, int startIndex)
		{
			byte type = buffer[startIndex];
			byte dataLength = buffer[(startIndex + 1) % buffer.Length];
			byte[] data = new byte[dataLength];
			ushort crc = 0;

			for (int i = 0; i < dataLength; i++)
			{
				data[i] = buffer[(startIndex + 2 + i) % buffer.Length];
			}

			crc |= (ushort)buffer[(startIndex + 2 + dataLength) % buffer.Length];
			crc |= (ushort)(buffer[(startIndex + 2 + dataLength + 1) % buffer.Length] << 8);
			return new LCDPacket(type, dataLength, data, crc);
		}
		private bool AddPacket(byte[] buffer, int startIndex)
		{
			LCDPacket packet = CreatePacket(buffer, startIndex);
			ushort calculatedCRC = CRCGenerator.GenerateCRC(buffer, startIndex, packet.DataLength + 2, CRC_SEED);

			switch (packet.PacketType)
			{
				case LCDPacket.LCDPacketType.NORMAL_RESPONSE :
					AddResponsePacket(packet);
					break;

				case LCDPacket.LCDPacketType.NORMAL_REPORT :
					AddReportPacket(packet);
					break;

				case LCDPacket.LCDPacketType.ERROR_RESPONSE :
					AddResponsePacket(packet);
					break;
			}
			if (calculatedCRC != packet.CRC)
			{
				Console.WriteLine("CRC ERROR!!!: Calculated CRC={0} Actual CRC={1}", Convert.ToString(calculatedCRC, 16), packet.CRC);
				return false;
			}

			return true;
		}

		public void Close()
		{
			if (com.IsOpen)
			{
				Reset();
				// comPort
				com.Close();
				CloseThreads();
			}
		}

		private void AddResponsePacket(LCDPacket packet)
		{
			lock (responseSignal)
			{
				responsePacket = packet;
				Monitor.Pulse(responseSignal);
			}
		}
		private void AddReportPacket(LCDPacket packet)
		{
			lock (reportSignal)
			{
				reportQueue.Enqueue(packet);
				Monitor.Pulse(reportSignal);
			}
		}

		public bool IsOpen
		{
			get
			{
				if (com == null)
				{
					return false;
				}
				else
				{
					return com.IsOpen;
				}
			}
		}

		public void Reset()
		{
			SetLCDBacklight(100);
			SetLCDContrast(15);
			SetFan1Power(0);
			WriteLine1("Crystalfontz 633");
			WriteLine2("HW v1.2  FW v1.0");
		}

		private void CloseThreads()
		{
			if (receiveThread != null)
			{
				receiveThread.Abort();
			}
			if (eventThread != null)
			{
				eventThread.Abort();
			}
		}
	}
	public class LCDPacket
	{
		private byte type;
		private byte dataLength;
		private byte[] data;
		private ushort crc;
		public enum LCDPacketType { NORMAL_RESPONSE, NORMAL_REPORT, ERROR_RESPONSE };
		private const byte NORMAL_RESPONSE = 0x40;
		private const byte NORMAL_REPORT = 0x80;
		private const byte ERROR_RESPONSE = 0xC0;
		public LCDPacket(byte type, byte dataLength, byte[] data)
		{
			this.type = type;
			this.dataLength = dataLength;
			this.data = data;
		}
		public LCDPacket(byte type, byte dataLength, byte[] data, ushort crc)
		{
			this.type = type;
			this.dataLength = dataLength;
			this.data = data;
			this.crc = crc;
		}
		public override string ToString()
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			sb.Append("Type: " + Convert.ToString(type, 16) + "\n");
			sb.Append("DataLength: " + Convert.ToString(dataLength, 16) + "\n");
			sb.Append("Data: ");
			for (int i = 0; i < dataLength; i++)
			{
				sb.Append(Convert.ToString(data[i], 16) + ", ");
			}

			sb.Append("\n");
			sb.Append("CRC: " + Convert.ToString(crc, 16) + "\n");
			return sb.ToString();
		}
		public byte Type
		{
			get
			{
				return type;
			}
		}

		public LCDPacketType PacketType
		{
			get
			{
				switch (type & 0xC0)
				{
					case NORMAL_RESPONSE :
						return LCDPacketType.NORMAL_RESPONSE;

					case NORMAL_REPORT :
						return LCDPacketType.NORMAL_REPORT;

					case ERROR_RESPONSE :
						return LCDPacketType.ERROR_RESPONSE;

					default :
						throw new Exception("Unexpected Packet Type: " + System.Convert.ToString(type, 16));
				}
			}
		}

		public byte DataLength
		{
			get
			{
				return dataLength;
			}
		}
		public byte[] Data
		{
			get
			{
				return data;
			}
		}
		public ushort CRC
		{
			get
			{
				return crc;
			}
		}
	}

	public class CRCGenerator
	{
		//CRC lookup table to avoid bit-shifting loops.
		static ushort[] crcLookupTable = {
			0x00000, 0x01189, 0x02312, 0x0329B, 0x04624, 0x057AD, 0x06536, 0x074BF, 0x08C48, 0x09DC1, 0x0AF5A, 0x0BED3, 0x0CA6C, 0x0DBE5, 0x0E97E, 0x0F8F7, 0x01081, 0x00108, 0x03393, 0x0221A, 0x056A5, 0x0472C, 0x075B7, 0x0643E, 0x09CC9, 0x08D40, 0x0BFDB, 0x0AE52, 0x0DAED, 0x0CB64, 0x0F9FF, 0x0E876, 0x02102, 0x0308B, 0x00210, 0x01399, 0x06726, 0x076AF, 0x04434, 0x055BD, 0x0AD4A, 0x0BCC3, 0x08E58, 0x09FD1, 0x0EB6E, 0x0FAE7, 0x0C87C, 0x0D9F5, 0x03183, 0x0200A, 0x01291, 0x00318, 0x077A7, 0x0662E, 0x054B5, 0x0453C, 0x0BDCB, 0x0AC42, 0x09ED9, 0x08F50, 0x0FBEF, 0x0EA66, 0x0D8FD, 0x0C974, 0x04204, 0x0538D, 0x06116, 0x0709F, 0x00420, 0x015A9, 0x02732, 0x036BB, 0x0CE4C, 0x0DFC5, 0x0ED5E, 0x0FCD7, 0x08868, 0x099E1, 0x0AB7A, 0x0BAF3, 0x05285, 0x0430C, 0x07197, 0x0601E, 0x014A1, 0x00528, 0x037B3, 0x0263A, 0x0DECD, 0x0CF44, 0x0FDDF, 0x0EC56, 0x098E9, 0x08960, 0x0BBFB, 0x0AA72, 0x06306, 0x0728F, 0x04014, 0x0519D, 0x02522, 0x034AB, 0x00630, 0x017B9, 0x0EF4E, 0x0FEC7, 0x0CC5C, 0x0DDD5, 0x0A96A, 0x0B8E3, 0x08A78, 0x09BF1, 0x07387, 0x0620E, 0x05095, 0x0411C, 0x035A3, 0x0242A, 0x016B1, 0x00738, 0x0FFCF, 0x0EE46, 0x0DCDD, 0x0CD54, 0x0B9EB, 0x0A862, 0x09AF9, 0x08B70, 0x08408, 0x09581, 0x0A71A, 0x0B693, 0x0C22C, 0x0D3A5, 0x0E13E, 0x0F0B7, 0x00840, 0x019C9, 0x02B52, 0x03ADB, 0x04E64, 0x05FED, 0x06D76, 0x07CFF, 0x09489, 0x08500, 0x0B79B, 0x0A612, 0x0D2AD, 0x0C324, 0x0F1BF, 0x0E036, 0x018C1, 0x00948, 0x03BD3, 0x02A5A, 0x05EE5, 0x04F6C, 0x07DF7, 0x06C7E, 0x0A50A, 0x0B483, 0x08618, 0x09791, 0x0E32E, 0x0F2A7, 0x0C03C, 0x0D1B5, 0x02942, 0x038CB, 0x00A50, 0x01BD9, 0x06F66, 0x07EEF, 0x04C74, 0x05DFD, 0x0B58B, 0x0A402, 0x09699, 0x08710, 0x0F3AF, 0x0E226, 0x0D0BD, 0x0C134, 0x039C3, 0x0284A, 0x01AD1, 0x00B58, 0x07FE7, 0x06E6E, 0x05CF5, 0x04D7C, 0x0C60C, 0x0D785, 0x0E51E, 0x0F497, 0x08028, 0x091A1, 0x0A33A, 0x0B2B3, 0x04A44, 0x05BCD, 0x06956, 0x078DF, 0x00C60, 0x01DE9, 0x02F72, 0x03EFB, 0x0D68D, 0x0C704, 0x0F59F, 0x0E416, 0x090A9, 0x08120, 0x0B3BB, 0x0A232, 0x05AC5, 0x04B4C, 0x079D7, 0x0685E, 0x01CE1, 0x00D68, 0x03FF3, 0x02E7A, 0x0E70E, 0x0F687, 0x0C41C, 0x0D595, 0x0A12A, 0x0B0A3, 0x08238, 0x093B1, 0x06B46, 0x07ACF, 0x04854, 0x059DD, 0x02D62, 0x03CEB, 0x00E70, 0x01FF9, 0x0F78F, 0x0E606, 0x0D49D, 0x0C514, 0x0B1AB, 0x0A022, 0x092B9, 0x08330, 0x07BC7, 0x06A4E, 0x058D5, 0x0495C, 0x03DE3, 0x02C6A, 0x01EF1, 0x00F78
		};
		public static ushort GenerateCRC(byte[] data, int dataLength, ushort seed)
		{
			ushort newCrc;

			newCrc = seed;
			for (int i = 0; i < dataLength; i++)
			{
				newCrc = (ushort)((newCrc >> 8) ^ crcLookupTable[(newCrc ^ data[i]) & 0xff]);
			}

			return ((ushort)~newCrc);
		}
		public static ushort GenerateCRC(byte[] data, int startIndex, int length, ushort seed)
		{
			ushort newCrc;

			newCrc = seed;
			for (int i = 0; i < length; i++)
			{
				newCrc = (ushort)((newCrc >> 8) ^ crcLookupTable[(newCrc ^ data[(startIndex + i) % data.Length]) & 0xff]);
			}

			return ((ushort)~newCrc);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -