public void getMessage(object o) { ReaderConnect reader = o as ReaderConnect; string rec_data = reader.response(); this.Invoke(new UpdateMessage(printMessage), new object[] { rec_data }); }
private void open_Click(object sender, EventArgs e) { if (rA_bt == true || rB_bt == true || rC_bt == true) //代表其中一個Reader有連線資料庫就要連線 { if (Global.database == false) { DBConn.Open(); } timer1.Enabled = true; } string key = Reader_list.SelectedItem as string; if (key == "中央噴水池") { rA = new ReaderConnect("140.134.208.81", 4000); this.Invoke(new UpdateMessage(printMessage), new object[] { "中央噴水池 Reader 已開" + "\x0d\x0a" }); rA_bt = true; //reader A 連線已開 open.Enabled = false; close.Enabled = true; tSB_On.Enabled = true; tSB_Off.Enabled = false; } else if (key == "208教室") { rB = new ReaderConnect("127.0.0.1", 4000); this.Invoke(new UpdateMessage(printMessage), new object[] { "208教室 Reader 已開" + "\x0d\x0a" }); rB_bt = true; //reader B 連線已開 open.Enabled = false; close.Enabled = true; tSB_On.Enabled = true; tSB_Off.Enabled = false; } else if (key == "停車場") { rC = new ReaderConnect("127.0.0.1", 3000); this.Invoke(new UpdateMessage(printMessage), new object[] { "停車場 Reader 已開" + "\x0d\x0a" }); rC_bt = true; //reader C 連線已開 open.Enabled = false; close.Enabled = true; tSB_On.Enabled = true; tSB_Off.Enabled = false; } else if (key == "全部") { MessageBox.Show("還沒實作"); } else { MessageBox.Show("請選擇Reader!"); } }
/*送指令給reader*/ public void send_common(ReaderConnect reader, string common) { String msg = common + "\x0d\x0a"; reader.request(msg); }
public void receive(object o) { ReaderConnect r = o as ReaderConnect; String recData; Boolean repeat = false; while (r.client.Connected) { if (r.stream.CanRead) { try { Byte[] data = new Byte[256]; Int32 bytes = r.stream.Read(data, 0, data.Length); if (!r.stream.DataAvailable) { recData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); if (recData.Length == 23) { this.Invoke(new UpdateMessage(printMessage), new object[] { recData }); //string test = "溫度: " + recData.Substring(18,3); //this.Invoke(new UpdateMessage(printMessage), new object[] { test }); //this.Invoke(new UpdateMessage(printMessage), new object[] { "temp_up: " + Global.temp_up + " temp_down: " + Global.temp_down }); //test體溫上下限 if (Convert.ToInt32(recData.Substring(18, 3)) > Global.temp_up || Convert.ToInt32(recData.Substring(18, 3)) < Global.temp_down) { Global.BeepOn = true; Global.alarm_voice(Global.BeepOn); //蜂鳴聲 switch (recData.Substring(1, 6)) //判斷為何地點 { case "000011": //中央噴水池 pB1.BackColor = Color.Red; happen[0] = true; break; case "000022": //208教室 pB3.BackColor = Color.Red; happen[1] = true; break; case "000033": //停車場 pB10.BackColor = Color.Red; happen[2] = true; break; } //讀取資料庫的資料 找學生 MySQLCommand DBComm = new MySQLCommand("select * from `RFID_Project`.`student`", DBConn); MySQLCommand firstCmd = new MySQLCommand("set names big5", DBConn); firstCmd.ExecuteNonQuery(); MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); DBReader.Read(); do { string id = DBReader.GetValue(1).ToString(); //this.Invoke(new UpdateMessage(printMessage), new object[] { " id= " + id }); //this.Invoke(new UpdateMessage(printMessage), new object[] { " recData.Substring(9, 6)= " + recData.Substring(9, 6) }); if (recData.Substring(9, 6) == id) { record = recData; //map專用訊息 //讀取資料庫的資料 找地點 MySQLCommand DBComm_reader = new MySQLCommand("select * from `RFID_Project`.`reader`", DBConn); MySQLDataReader DBReader_reader = DBComm_reader.ExecuteReaderEx(); DBReader_reader.Read(); do { string reader_id = DBReader_reader.GetValue(0).ToString(); if (recData.Substring(1, 6) == reader_id) { string date = System.DateTime.Now.ToString("yyyy/MM/dd"); string time = System.DateTime.Now.ToString("HH:mm:ss"); string temp = recData.Substring(18, 2) + "." + recData.Substring(20, 1); string teacher = ""; //抓老師資料 MySQLCommand DBComT = new MySQLCommand("select * from `RFID_Project`.`teacher`", DBConn); MySQLDataReader DBReader_teacher = DBComT.ExecuteReaderEx(); DBReader_teacher.Read(); do { if (DBReader_teacher.GetString(0) == DBReader.GetString(3)) { teacher = DBReader_teacher.GetString(1); } } while (DBReader_teacher.Read()); //判斷事件是否重複 try { MySQLCommand DBComRecord = new MySQLCommand("select * from `RFID_Project`.`event_record`", DBConn); MySQLDataReader DBReader_Record = DBComRecord.ExecuteReaderEx(); DBReader_Record.Read(); do { MessageBox.Show(DBReader.GetString(0) + " / " + DBReader_Record.GetString(3)); if (date == DBReader_Record.GetString(1) && DBReader.GetString(0) == DBReader_Record.GetString(3) && DBReader_reader.GetString(1) == DBReader_Record.GetString(5)) { repeat = true; break; } else { repeat = false; } } while (DBReader_Record.Read()); } catch { repeat = false; } if (repeat == false) { this.Invoke(new UpdateDatagridview(printDatagridview), new object[] { date, time, temp, DBReader.GetValue(0), DBReader.GetValue(2), DBReader_reader.GetValue(1), teacher }); Global.event_count++; this.Invoke(new UpdateCount(printCount), new object[] { }); //記錄至資料庫. 要按處理鍵.結果才會改成y MySQLCommand DBComm_DB = new MySQLCommand("INSERT INTO `RFID_Project`.`event_record` (`no`,`date`,`time`,`stu_id`,`stu_name`,`place`,`temp`,`result`)VALUES ('" + event_no + "','" + date + "','" + time + "','" + DBReader.GetValue(0) + "','" + DBReader.GetValue(2) + "','" + DBReader_reader.GetValue(1) + "','" + recData.Substring(18, 3) + "','" + "n" + "');", DBConn); //登錄記錄 MySQLDataReader DBReader_DB = DBComm_DB.ExecuteReaderEx(); event_no++; } Thread.Sleep(0); break; } } while (DBReader_reader.Read()); } } while (DBReader.Read()); } } else { this.Invoke(new UpdateMessage(printMessage), new object[] { recData }); } } else { Thread.Sleep(0); } } catch (Exception e) { MessageBox.Show(e.ToString()); break; } } } }