Ejemplo n.º 1
0
		//卡操作成功后触发时间
		private void loginForm_SendCardSuccessed()
		{
			Timer_SendCardOverTime.Enabled = false;

			CardInfoSystem cardInfoSystem = new CardInfoSystem();
			if(isBatchSendCard&&gridView3.RowCount>0)
			{
				if(batchSearchStu)
				{
					//cardInfoSystem.UpdateCardState(gridView3.GetDataRow(0)["info_stuCardNumber"].ToString());
				}
				else
				{
					cardInfoSystem.UpdateTeaCardState(gridView3.GetDataRow(0)["info_teaCardNumber"].ToString());
				}
			}
			
//			if(!isBatchSendCard&&isSendCard)
//			{
//				foreach(string cardNumber in cardsHasSend)
//				{
//					if(searchStu)
//					{
//						cardInfoSystem.UpdateCardState(cardNumber);
//					}
//					else
//					{
//						cardInfoSystem.UpdateTeaCardState(cardNumber);
//					}
//				}
//			}

			isCardLogOutSucceed = true;
			isCardSendSucceed = true;
			
			//Login.COM_PORT_IS_BUSY = false;
			try
			{
				LoginForm.sendCardThread.Resume();
			}
			catch{}
		}
Ejemplo n.º 2
0
		//发卡
		private void SendCard(object state)
		{
            var context = state as SynchronizationContext;
			try
			{
				FileStream fileStream = null;
				StreamReader streamReader = null;

				DataSet machineAddrList = new MachineSystem().GetMachineAddrList();

				DataFrame sendCardFrame = new DataFrame();

				sendCardFrame.sym = new byte[]{(byte)'@',(byte)'@'};
				sendCardFrame.desAddr = 0;
				sendCardFrame.seq = CPTT.SystemFramework.Util.FRAME_SEQUENCE_VALUE;

				if(!isBatchSendCard)//普通发卡
				{
					sendCardFrame.protocol = CPTT.SystemFramework.Util.SEND_CARD_TOKEN;

					sendCardFrame.comFrameLen = 30;
					sendCardFrame.frameData = new byte[22];
					string stuID = string.Empty;
					int stuNumber;

					if(searchStu)
					{
						stuID = (gridView1.GetDataRow(gridView1.GetSelectedRows()[0])["info_stuID"]).ToString();
						stuNumber = Convert.ToInt32(gridView1.GetDataRow(gridView1.GetSelectedRows()[0])["info_stuNumber"]);
					}
					else
					{
						stuID = (gridView1.GetDataRow(gridView1.GetSelectedRows()[0])["T_ID"]).ToString();
						stuNumber = Convert.ToInt32(gridView1.GetDataRow(gridView1.GetSelectedRows()[0])["T_Number"]);
					}

					sendCardFrame.frameData[0] = (byte)(stuNumber % 100);
				
					int tmp = stuNumber / 100;
					int stuClassNum = tmp / 10;
					stuClassNum <<= 4;
					stuClassNum += tmp % 10;

					sendCardFrame.frameData[1] = (byte)stuClassNum;
		
					int stuCardNumber = 0;
					if(!(SendCard1Number.Text.Trim().Equals(string.Empty)))
					{
#if CardValidCheck
						try
						{
							bool findCard = false;
							fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"encry.bin",FileMode.OpenOrCreate,FileAccess.Read);
							streamReader = new StreamReader(fileStream);
							long dataEncrypt = Convert.ToInt64(SendCard1Number.Text.Trim()) + myBirthday;
							StringBuilder enryString = new StringBuilder(dataEncrypt.ToString());
							for ( int i=0; i<enryString.Length; i++ )
							{
								char newChar = (char)(((int)enryString[i])+i*(i-1));
								enryString[i] = newChar;
							}
							while ( true )
							{
								string readStr = streamReader.ReadLine();
								if ( readStr != null )
								{
									if ( readStr.Equals(enryString.ToString()) )
									{
										findCard = true;
										break;
									}
								}
								else
								{
									if ( !findCard )
									{
										Timer_SendCardOverTime.Enabled = false;
										LoginForm.sendCardThread.Abort();
										throw new Exception();
									}
									else
										break;
								}
							}
						}
						catch
						{
							Login.COM_PORT_IS_BUSY = false;
							LoginForm.ResumeQueryThread();
							MessageBox.Show("存在非法卡号,发卡失败!");
							dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
							dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
							return;
						}
						finally
						{
							fileStream.Close();
							streamReader.Close();
						}
#endif

						unchecked
						{
							stuCardNumber = (int)(Convert.ToInt64(SendCard1Number.Text.Trim()));
						}

						CPTT.SystemFramework.Util.FillCard(sendCardFrame,stuCardNumber,0);
					}

					if(!(SendCard2Number.Text.Trim().Equals(string.Empty)))
					{
#if CardValidCheck
						try
						{
							bool findCard = false;
							fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"encry.bin",FileMode.OpenOrCreate,FileAccess.Read);
							streamReader = new StreamReader(fileStream);
							long dataEncrypt = Convert.ToInt64(SendCard2Number.Text.Trim()) + myBirthday;
							StringBuilder enryString = new StringBuilder(dataEncrypt.ToString());
							for ( int i=0; i<enryString.Length; i++ )
							{
								char newChar = (char)(((int)enryString[i])+i*(i-1));
								enryString[i] = newChar;
							}
							while ( true )
							{
								string readStr = streamReader.ReadLine();
								if ( readStr != null )
								{
									if ( readStr.Equals(enryString.ToString()) )
									{
										findCard = true;
										break;
									}
								}
								else
								{
									if ( !findCard )
									{
										Timer_SendCardOverTime.Enabled = false;
										LoginForm.sendCardThread.Abort();
										throw new Exception();
									}
									else
										break;
								}
							}
						}
						catch(Exception ex)
						{
							Login.COM_PORT_IS_BUSY = false;
							LoginForm.ResumeQueryThread();
							MessageBox.Show("存在非法卡号,发卡失败!");
							dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
							dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
							return;
						}
						finally
						{
							fileStream.Close();
							streamReader.Close();
						}
#endif

						unchecked
						{
							stuCardNumber = (int)(Convert.ToInt64(SendCard2Number.Text.Trim()));
						}

						CPTT.SystemFramework.Util.FillCard(sendCardFrame,stuCardNumber,4);
					}

					if(!(SendCard3Number.Text.Trim().Equals(string.Empty)))
					{
#if CardValidCheck
						try
						{
							bool findCard = false;
							fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"encry.bin",FileMode.OpenOrCreate,FileAccess.Read);
							streamReader = new StreamReader(fileStream);
							long dataEncrypt = Convert.ToInt64(SendCard3Number.Text.Trim()) + myBirthday;
							StringBuilder enryString = new StringBuilder(dataEncrypt.ToString());
							for ( int i=0; i<enryString.Length; i++ )
							{
								char newChar = (char)(((int)enryString[i])+i*(i-1));
								enryString[i] = newChar;
							}
							while ( true )
							{
								string readStr = streamReader.ReadLine();
								if ( readStr != null )
								{
									if ( readStr.Equals(enryString.ToString()) )
									{
										findCard = true;
										break;
									}
								}
								else
								{
									if ( !findCard )
									{
										Timer_SendCardOverTime.Enabled = false;
										LoginForm.sendCardThread.Abort();
										throw new Exception();
									}
									else
										break;
								}
							}
						}
						catch(Exception ex)
						{
							Login.COM_PORT_IS_BUSY = false;
							LoginForm.ResumeQueryThread();
							MessageBox.Show("存在非法卡号,发卡失败!");
							dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
							dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
							return;
						}
						finally
						{
							fileStream.Close();
							streamReader.Close();
						}
#endif 

						unchecked
						{
							stuCardNumber =(int)(Convert.ToInt64(SendCard3Number.Text.Trim()));
						}

						CPTT.SystemFramework.Util.FillCard(sendCardFrame,stuCardNumber,8);
					}

					if(!(SendCard4Number.Text.Trim().Equals(string.Empty)))
					{
#if CardValidCheck
						try
						{
							bool findCard = false;
							fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"encry.bin",FileMode.OpenOrCreate,FileAccess.Read);
							streamReader = new StreamReader(fileStream);
							long dataEncrypt = Convert.ToInt64(SendCard4Number.Text.Trim()) + myBirthday;
							StringBuilder enryString = new StringBuilder(dataEncrypt.ToString());
							for ( int i=0; i<enryString.Length; i++ )
							{
								char newChar = (char)(((int)enryString[i])+i*(i-1));
								enryString[i] = newChar;
							}
							while ( true )
							{
								string readStr = streamReader.ReadLine();
								if ( readStr != null )
								{
									if ( readStr.Equals(enryString.ToString()) )
									{
										findCard = true;
										break;
									}
								}
								else
								{
									if ( !findCard )
									{
										Timer_SendCardOverTime.Enabled = false;
										LoginForm.sendCardThread.Abort();
										throw new Exception();
									}
									else
										break;
								}
							}
						}
						catch(Exception ex)
						{
							Login.COM_PORT_IS_BUSY = false;
							LoginForm.ResumeQueryThread();
							MessageBox.Show("存在非法卡号,发卡失败!");
							dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
							dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
							return;
						}
						finally
						{
							fileStream.Close();
							streamReader.Close();
						}
#endif

						unchecked
						{
							stuCardNumber = (int)(Convert.ToInt64(SendCard4Number.Text.Trim()));
						}

						CPTT.SystemFramework.Util.FillCard(sendCardFrame,stuCardNumber,12);
					}

					if(!(SendCard5Number.Text.Trim().Equals(string.Empty)))
					{
#if CardValidCheck
						try
						{
							bool findCard = false;
							fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"encry.bin",FileMode.OpenOrCreate,FileAccess.Read);
							streamReader = new StreamReader(fileStream);
							long dataEncrypt = Convert.ToInt64(SendCard5Number.Text.Trim()) + myBirthday;
							StringBuilder enryString = new StringBuilder(dataEncrypt.ToString());
							for ( int i=0; i<enryString.Length; i++ )
							{
								char newChar = (char)(((int)enryString[i])+i*(i-1));
								enryString[i] = newChar;
							}
							while ( true )
							{
								string readStr = streamReader.ReadLine();
								if ( readStr != null )
								{
									if ( readStr.Equals(enryString.ToString()) )
									{
										findCard = true;
										break;
									}
								}
								else
								{
									if ( !findCard )
									{
										Timer_SendCardOverTime.Enabled = false;
										LoginForm.sendCardThread.Abort();
										throw new Exception();
									}
									else
										break;
								}
							}
						}
						catch(Exception ex)
						{
							Login.COM_PORT_IS_BUSY = false;
							LoginForm.ResumeQueryThread();
							MessageBox.Show("存在非法卡号,发卡失败!");
							dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
							dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
							return;
						}
						finally
						{
							fileStream.Close();
							streamReader.Close();
						}
#endif

						unchecked
						{
							stuCardNumber = (int)(Convert.ToInt64(SendCard5Number.Text.Trim()));
						}

						CPTT.SystemFramework.Util.FillCard(sendCardFrame,stuCardNumber,16);
					}

					foreach(DataRow machineAddr in machineAddrList.Tables[0].Rows)
					{

						cardTrace = Convert.ToInt32(machineAddr["machine_address"]);

						sendCardFrame.desAddr = Convert.ToByte(machineAddr["machine_address"]);

						sendCardFrame.computeCheckSum();

						//Monitor.Enter(Login.handleComClass);
						Monitor.Enter(Login.syncRoot);
						try
						{
							Login.handleComClass.WriteSerialCmd(sendCardFrame.comFrameLen,sendCardFrame.frameToBytes());//发送问询帧
						}
						finally
						{
							Monitor.Exit(Login.syncRoot);
						}
						//Monitor.Exit(Login.handleComClass);

						Timer_SendCardOverTime.Enabled = true;
						
						LoginForm.sendCardThread.Suspend();
					}

					Thread.Sleep(300);
					if (this.InvokeRequired)
					{
						this.Invoke(new getCardDetailsContextSyncDelegate(GetCardDetailsContextSyncDelegate), new object[]{stuID});
					}
					else
					{
						GetCardDetailsContextSyncDelegate(stuID);
					}

//					if(searchStu)
//					{
//						cardDetails = new CardInfoSystem().GetStuCardByID(stuID);
//					}
//					else
//					{
//						cardDetails = new CardInfoSystem().GetTeaCardByID(stuID);
//					}
//					DisplayCardInfo(cardDetails);
//
//					dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
//					dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
				}
				else//批量发卡
				{
					try
					{
						sendCardFrame.protocol = CPTT.SystemFramework.Util.BATCH_SEND_CARD_TOKEN;

						sendCardFrame.comFrameLen = 15;
						sendCardFrame.frameData = new byte[7];
						string stuID = string.Empty;
						int stuNumber = 0;

						while(gridView3.RowCount>0)
						{
							isCardSendSucceed = true;

							if(batchSearchStu)
							{
								stuID = (gridView3.GetDataRow(0)["info_stuID"]).ToString();
								stuNumber = Convert.ToInt32(gridView3.GetDataRow(0)["info_stuNumber"]);
							}
							else
							{
								stuID = (gridView3.GetDataRow(0)["T_ID"]).ToString();
								stuNumber = Convert.ToInt32(gridView3.GetDataRow(0)["T_Number"]);
							}

							sendCardFrame.frameData[0] = (byte)(stuNumber % 100);
			
							int tmp = stuNumber / 100;
							int stuClassNum = tmp / 10;
							stuClassNum <<= 4;
							stuClassNum += tmp % 10;

							sendCardFrame.frameData[1] = (byte)stuClassNum;

#if CardValidCheck3
							try
							{
								bool findCard = false;
								Int64 cardNumber = 0;

								if(batchSearchStu)
								{
									cardNumber = Convert.ToInt64(gridView3.GetDataRow(0)["info_stuCardNumber"]);
								}
								else
								{
									cardNumber = Convert.ToInt64(gridView3.GetDataRow(0)["info_teaCardNumber"]);
								}

								fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"encry.bin",FileMode.OpenOrCreate,FileAccess.Read);
								streamReader = new StreamReader(fileStream);
								long dataEncrypt = cardNumber + myBirthday;
								StringBuilder enryString = new StringBuilder(dataEncrypt.ToString());
								for ( int i=0; i<enryString.Length; i++ )
								{
									char newChar = (char)(((int)enryString[i])+i*(i-1));
									enryString[i] = newChar;
								}
								while ( true )
								{
									string readStr = streamReader.ReadLine();
									if ( readStr != null )
									{
										if ( readStr.Equals(enryString.ToString()) )
										{
											findCard = true;
											break;
										}
									}
									else
									{
										if ( !findCard )
										{
											Timer_SendCardOverTime.Enabled = false;
											LoginForm.sendCardThread.Abort();
											throw new Exception();
										}
										else
											break;
									}
								}
							}
							catch(Exception ex)
							{
								Login.COM_PORT_IS_BUSY = false;
								LoginForm.ResumeQueryThread();
								simpleButton2.Enabled = true;
								MessageBox.Show("存在非法卡号,发卡失败!");
								return;
							}
							finally
							{
								fileStream.Close();
								streamReader.Close();
							}
#endif

							int stuCardNumber = 0;

							unchecked
							{
								if(batchSearchStu)
								{
									stuCardNumber = (int)(Convert.ToInt64(gridView3.GetDataRow(0)["info_stuCardNumber"]));
								}
								else
								{
									stuCardNumber = (int)(Convert.ToInt64(gridView3.GetDataRow(0)["info_teaCardNumber"]));
								}
							}
				
							foreach(DataRow machineAddr in machineAddrList.Tables[0].Rows)
							{

								cardTrace = Convert.ToInt32(machineAddr["machine_address"]);
						

								sendCardFrame.desAddr =Convert.ToByte(machineAddr["machine_address"]);

								CPTT.SystemFramework.Util.FillCard(sendCardFrame,stuCardNumber,0);

								if(batchSearchStu)
								{
									sendCardFrame.frameData[6] = Convert.ToByte(gridView3.GetDataRow(0)["info_stuCardSeq"]);
								}
								else
								{
									sendCardFrame.frameData[6] = Convert.ToByte(gridView3.GetDataRow(0)["info_teaCardSeq"]);
								}

								sendCardFrame.computeCheckSum();	
								Monitor.Enter(Login.syncRoot);
								try
								{
									Login.handleComClass.WriteSerialCmd(sendCardFrame.comFrameLen,sendCardFrame.frameToBytes());//发送问询帧
								}
								finally
								{
									Monitor.Exit(Login.syncRoot);
								}
								Timer_SendCardOverTime.Enabled = true;
								LoginForm.sendCardThread.Suspend();
							}

							if ( isCardSendSucceed )
							{
								CardInfoSystem cardInfoSystem = new CardInfoSystem();
								if(batchSearchStu)
								{
									cardInfoSystem.UpdateCardState(gridView3.GetDataRow(0)["info_stuCardNumber"].ToString());
								}
								else
								{
									cardInfoSystem.UpdateTeaCardState(gridView3.GetDataRow(0)["info_teaCardNumber"].ToString());
								}

                                context.Send((_) => gridView3.DeleteRow(0), null);
							}
						}

                        context.Send((_) => simpleButton2.Enabled = true, null);
					}
					catch(ThreadAbortException taex)
					{
						if (taex.ExceptionState == null || taex.ExceptionState.GetType() != typeof(bool))
						{
							if ((LoginForm.sendCardThread.ThreadState & ThreadState.Aborted) == ThreadState.Aborted
								|| (LoginForm.sendCardThread.ThreadState & ThreadState.AbortRequested) == ThreadState.AbortRequested
								|| (LoginForm.sendCardThread.ThreadState & ThreadState.Stopped) == ThreadState.Stopped
								|| (LoginForm.sendCardThread.ThreadState & ThreadState.StopRequested) == ThreadState.StopRequested)
							{
                                LoginForm.sendCardThread = new Thread(SendCard);
                                LoginForm.sendCardThread.Start(SynchronizationContext.Current);
							}
							Thread.ResetAbort();
						}
						return;
					}
				}

				Login.COM_PORT_IS_BUSY = false;

				reTryTime = 0;

				//			LoginForm.ResumeThread();
				LoginForm.ResumeQueryThread();

				if ( isCardSendSucceed )
					MessageBox.Show("发卡成功!!","系统信息!",
						MessageBoxButtons.OK,MessageBoxIcon.Information);

				try
				{
					LoginForm.sendCardThread.Abort();
				}
				catch
				{
					Thread.ResetAbort();
				}
			}
			catch(Exception ex)
			{
				CPTT.SystemFramework.Util.WriteLog(ex.Message,CPTT.SystemFramework.Util.EXCEPTION_LOG_TITLE);
                MessageBox.Show("发卡中断,点击发卡重试!");
			}
			finally
			{
                if (context != null)
                {
                    context.Send((_) =>
                    {
                        dataNavigator_CardSent.Buttons.CustomButtons[0].Enabled = true;
                        dataNavigator_CardSent.Buttons.CustomButtons[1].Enabled = true;
                    }, null);
                }

				
				Login.COM_PORT_IS_BUSY = false;
				
				LoginForm.ResumeQueryThread();

                if (LoginForm.sendCardThread.IsAlive)
                {
                    try
                    {
                        LoginForm.sendCardThread.Abort();
                    }
                    catch (Exception)
                    {

                    }
                }

			}
		}