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

📄 gprscomm.cs

📁 GPRS数据采集驱动源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
					//50H:80==接收到载荷规定长度的数据包 51H:81==接收到超时形成的数据包 52H:82==定时发送的数据包 53H:83==心跳数据包
					if((DatapackID==80) || (DatapackID==81) || (DatapackID==82))
					{
						//判断是否为有效的实时数据
						//得到设备号
						String DeviceIDStr=null;
						uint nDeviceID=0;
						for(int i=0; i<GPRS_DEVICEID_WIDTH_BUFFER; i++)
							DeviceIDStr=String.Concat(DeviceIDStr,(char)bytesRecv[GPRS_DEVICEID_INDEX_BUFFER+i]);
						nDeviceID =(uint)Int32.Parse(DeviceIDStr);




						validCount++;
						invalidCount = totalCount - validCount;

						//界面送显
						//strInfo = "接收到" + nDeviceID.ToString().PadLeft(3,' ') + "号设备实时数据包, " + "时间: " + System.DateTime.Now.ToString()+ "  " + Recvpackage;
						strInfo = "接收到" + nDeviceID.ToString().PadLeft(3,' ') + "号设备实时数据包, " + "时间: " + System.DateTime.Now.ToString()+ "  " ;

						for(int cnt=GPRS_CHANNEL_DATA_INDEX_BUFFER; cnt<nRecvCount; cnt++)
							strInfo += MyTypeTranslator.ByteToHex(bytesRecv[cnt])+" ";

						//显示全部包内容
						//for(int cnt=0; cnt<nRecvCount; cnt++)
						//	strInfo += MyTypeTranslator.ByteToHex(bytesRecv[cnt])+" ";

						//this.lbData.Invoke(new showInfo_sub_delegate(this.showInfo_sub), new object[] {strInfo});

						listBox1Buffer.Items.Add(strInfo);

 
						//形成发送到中控的数据包
						bytesSendToCenter=CreateCenterPackage("1", nDeviceID, bytesRecv);

						if(SocketSendToCenterBuffer!=null&&EndPointSendToCenterBuffer!=null)
						{
							SocketSendToCenterBuffer.SendTo(bytesSendToCenter,EndPointSendToCenterBuffer);
						}


					}					
					else
					{
						invalidCount++;
						validCount = totalCount - invalidCount;
					}
					
					//showStatistic(totalCount, validCount, invalidCount);

					statusBar1Buffer.Panels[0].Text = "GPRS包:" + totalCount.ToString()+"个";
					statusBar1Buffer.Panels[1].Text = "有效数据包:" + validCount.ToString()+"个";
					statusBar1Buffer.Panels[2].Text = "无效数据包:" + invalidCount.ToString()+"个";
				}
				catch (Exception err)
				{
					WriteToText(ExceptionCountBuffer,DebugFileNameBuffer,swDebugFileBuffer,err);
				}
			}
		}

		/// <summary>
		/// 关闭Socket连接
		/// </summary>
		/// <returns></returns>
		public bool CloseSocketLink()
		{
			String DebugFileNameBuffer=DebugFileName;
			uint ExceptionCountBuffer=ExceptionCount;
			StreamWriter swDebugFileBuffer=null;
			try
			{
				//关闭Socket:从GPRS接收实时数据
				if (SocketRecvFromGprs != null)
				{
					SocketRecvFromGprs.Close();
					SocketRecvFromGprs = null;
					EndPointRecvFromGprs = null;
				}

				//关闭Socket:发送实时数据到中控
				if (SocketSendToCenter != null)
				{
					SocketSendToCenter.Close();
					SocketSendToCenter = null;
					EndPointSendToCenter = null;
				}

				return true;
			}
			catch (Exception err)
			{
				WriteToText(ExceptionCountBuffer,DebugFileNameBuffer,swDebugFileBuffer,err);

				return false;
			}
		}		

		/// <summary>
		/// 添加用户界面显示信息
		/// </summary>
		/// <param name="str"></param>
		public void showInfo_sub(string str)
		{
			if (this.lbData.Items.Count > 500)
				this.lbData.Items.Clear();

			this.lbData.Items.Add(str);			
		}

		/// <summary>
		/// 添加包统计信息
		/// </summary>
		/// <param name="nTotal"></param>
		/// <param name="nValid"></param>
		/// <param name="nInvalid"></param>
		public void showStatistic(uint nTotal, uint nValid, uint nInvalid)
		{
			this.sbStatistic.Invoke(new showStatistic_sub_delegate(this.showStatistic_sub),
				new object[]{nTotal, nValid, nInvalid});
		}
		public void showStatistic_sub(uint nTotal, uint nValid, uint nInvalid)
		{
			this.sbStatistic.Panels[0].Text = "GPRS包:"+nTotal.ToString()+"个";
			this.sbStatistic.Panels[1].Text = "有效数据包:"+nValid.ToString()+"个";
			this.sbStatistic.Panels[2].Text = "无效数据包:"+nInvalid.ToString()+"个";
		}

		#endregion


		#region 静态方法		
		public static void WriteToText(uint ExceptionCountBuffer,string DebugFileNameBuffer,StreamWriter swDebugFileBuffer,Exception err)
		{
			ExceptionCountBuffer++;
			if(!File.Exists(DebugFileNameBuffer))
				swDebugFileBuffer = File.CreateText(DebugFileNameBuffer);
			else
				swDebugFileBuffer = File.AppendText(DebugFileNameBuffer);
			if(swDebugFileBuffer!=null)
			{
				using(swDebugFileBuffer)
				{
					swDebugFileBuffer.WriteLine(ExceptionCountBuffer.ToString());
					swDebugFileBuffer.WriteLine(DateTime.Now.ToString());
					swDebugFileBuffer.WriteLine(err.Message);
					swDebugFileBuffer.WriteLine(err.StackTrace);
					swDebugFileBuffer.WriteLine();
				}
			}
		}

		private void WriteToTxt(StreamWriter swDebugFileBuffer,string str)
		{
			string DataTextName=Application.StartupPath+"\\DataText.txt";
			if(!File.Exists(DataTextName))
				swDebugFileBuffer = File.CreateText(DataTextName);
			else
				swDebugFileBuffer = File.AppendText(DataTextName);
			if(swDebugFileBuffer!=null)
			{
				using(swDebugFileBuffer)
				{
					swDebugFileBuffer.WriteLine(DateTime.Now.ToString()+"  "+str);
					swDebugFileBuffer.WriteLine();
				}
			}
		}
		#endregion

		#region 属性
		public string ErrorMsg
		{
			get{return errorMsg;}
			set{errorMsg=value;}
		}
		#endregion


	
		
		
		
	}



	#region 驱动配置类
	public class DriverConfig
	{
		//驱动ID
		private string driverID;

		//从GPRS接收实时数据IP,端口
		private string ipRecvFromGprs;
		private int portRecvFromGprs;

		//发送到中控的实时数据IP,端口
		private string ipSendToCenter;
		private int portSendToCenter;
	
		public DriverConfig()
		{
			//构造函数
		}


		/// <summary>
		/// 设备号
		/// </summary>
		public string DriverID
		{
			get { return driverID;}
			set { driverID = value;}
		}

		/// <summary>
		///  从GPRS接收实时数据的IP
		/// </summary>
		public string IpRecvFromGprs
		{
			get { return ipRecvFromGprs;}
			set { ipRecvFromGprs = value;}
		}

		/// <summary>
		/// 从GPRS接收实时数据Port
		/// </summary>
		public int PortRecvFromGprs
		{
			get { return portRecvFromGprs;}
			set { portRecvFromGprs = value;}
		}

		/// <summary>
		/// 发送到中控的实时数据IP
		/// </summary>
		public string IpSendToCenter
		{
			get { return ipSendToCenter;}
			set { ipSendToCenter = value;}
		}
		
		/// <summary>
		/// 发送到中控的实时数据Port
		/// </summary>
		public int PortSendToCenter
		{
			get { return portSendToCenter;}
			set { portSendToCenter = value;}
		}
		
	}
	#endregion

	#region 字节类型转换
	public class MyTypeTranslator
	{
		public MyTypeTranslator()
		{
		}

		public static bool TwoBytesToUint(byte[] bytes, ref uint number)
		{
			if(bytes.Length==2)
			{
				number=0;
				number=number|(uint)bytes[1]*1;
				number=number|(uint)bytes[0]*256;
				return true;
			}
			else
				return false;
		}

		public static bool ThreeBytesToUint(byte[] bytes, ref uint number)
		{
			if(bytes.Length==3)
			{
				number=0;
				number=number|(uint)bytes[2]*1;
				number=number|(uint)bytes[1]*256;
				number=number|(uint)bytes[0]*256*256;
				return true;
			}
			else
				return false;
		}

		public static bool FourBytesToUint(byte[] bytes,ref uint number)
		{
			if(bytes.Length==4)
			{
				number=0;
				number=number|(uint)bytes[3]*1;
				number=number|(uint)bytes[2]*256;
				number=number|(uint)bytes[1]*256*256;
				number=number|(uint)bytes[0]*256*256*256;
				return true;
			}
			else
				return false;
		}

		public static bool UintToFourBytes(uint number,ref byte[] bytes)
		{
			if(bytes.Length==4)
			{			
				bytes[3]=(byte)(number&255);
				bytes[2]=(byte)((number&65280)/256);
				bytes[1]=(byte)((number&16711680)/(256*256));
				bytes[0]=(byte)((number&4278190080)/(256*256*256));
				return true;
			}
			else
				return false;
		}

		public static bool UintToTwoBytes(uint number,ref byte[] bytes)
		{
			if(bytes.Length==2)
			{			
				bytes[1]=(byte)(number&255);
				bytes[0]=(byte)((number&65280)/256);
				return true;
			}
			else
				return false;
		}

		public static String ByteToHex(byte Value)
		{
			String Result=null;
			String High=null,Low=null;

			byte Measure1=240;
			byte Value1=(byte)(Value & Measure1);
			Value1/=16;
			switch(Value1)
			{
				case 0:				
					High="0";
					break;
				case 1:
					High="1";
					break;
				case 2:
					High="2";
					break;
				case 3:
					High="3";
					break;
				case 4:
					High="4";
					break;
				case 5:
					High="5";
					break;
				case 6:
					High="6";
					break;
				case 7:
					High="7";
					break;
				case 8:
					High="8";
					break;
				case 9:
					High="9";
					break;
				case 10:
					High="A";
					break;
				case 11:
					High="B";
					break;
				case 12:
					High="C";
					break;
				case 13:
					High="D";
					break;
				case 14:
					High="E";
					break;
				case 15:
					High="F";
					break;
			}

			byte Measure2=15;
			byte Value2=(byte)(Value & Measure2);			
			switch(Value2)
			{
				case 0:				
					Low="0";
					break;
				case 1:
					Low="1";
					break;
				case 2:
					Low="2";
					break;
				case 3:
					Low="3";
					break;
				case 4:
					Low="4";
					break;
				case 5:
					Low="5";
					break;
				case 6:
					Low="6";
					break;
				case 7:
					Low="7";
					break;
				case 8:
					Low="8";
					break;
				case 9:
					Low="9";
					break;
				case 10:
					Low="A";
					break;
				case 11:
					Low="B";
					break;
				case 12:
					Low="C";
					break;
				case 13:
					Low="D";
					break;
				case 14:
					Low="E";
					break;
				case 15:
					Low="F";
					break;
			}

			Result=High+Low;
			return Result;
		}

		public static string BytesToString(byte[] bytes, int nCount)
		{
			string strReturn = null;
			for (int i = 0; i < nCount; i++)
			{
				strReturn += bytes[i] + " ";
			}
			return strReturn;
		}

	}

	#endregion


}

⌨️ 快捷键说明

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