//항온항습기(전산실 - 7) private void serialPortHvic1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { isHvicReceiveData1 = true; serialPortHvic1.Read(msgHvichex1, 0, msgHvichex1.Length); for (int i = 0; i < msgHvichex1.Length; i++) { msgHvicstr1[i] = String.Format("{0:X2}", (int)msgHvichex1[i]); Console.Write(msgHvicstr1[i]); } Console.WriteLine("항온항습1"); //수신데이터 CRC확인하기 int chkCRC1 = msgHvichex1[55]; int chkCRC2 = 0; byte[] chkCRCok = new byte[55]; Array.Copy(msgHvichex1, 0, chkCRCok, 0, 55); chkCRC2 = c.EXOR(chkCRCok.Length, chkCRCok); if (chkCRC1 != chkCRC2) { Console.WriteLine("CRC오류 - CRC {0}={1}", chkCRC1, chkCRC2); return; } else { AddReceiveDataHVIC1(); } } catch (Exception err) { ld.logDate(err.ToString()); } }
private void OnTimedEvent1(object source, ElapsedEventArgs e) { Firedataload1(); //온습도 if (serialPortTH.IsOpen) { serialPortTH.Close(); serialPortTH.Open(); } else { serialPortTH.Open(); } if (serialTHSendCount > 9) { serialTHSendCount = 0; } int num = serialTHSendCount + 1; byte[] sMagTH = new byte[] { (byte)0xFA, (byte)num, (byte)0x0A }; byte[] chkCRCok = new byte[2]; int chkCRC2 = 0; Array.Copy(sMagTH, 1, chkCRCok, 0, 2); chkCRC2 = c.EXOR(chkCRCok.Length, chkCRCok); byte[] bin = new byte[] { (byte)0xFA, (byte)num, (byte)0x0A, (byte)chkCRC2, (byte)0x03 }; //byte[] sMagTH = new byte[] { (byte)num, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x02 }; //byte[] bin = new byte[sMagTH.Length + 2]; //CRC체크상환받을곳 //Array.ConstrainedCopy(sMagTH, 0, bin, 0, sMagTH.Length); //CRC체크하기 //Array.ConstrainedCopy(c.CRC16(sMagTH.Length, sMagTH), 0, bin, sMagTH.Length, 2); serialTHSendCount++; serialPortTH.Write(bin, 0, bin.Length); //온습도ups if (!serialPortTH2.IsOpen) { //serialPortTH2.Close(); serialPortTH2.Open(); } //else //{ // serialPortTH2.Open(); //} byte[] bin2 = new byte[] { (byte)0xFA, (byte)0x01, (byte)0x0A, (byte)0x0B, (byte)0x03 }; //byte[] sMagTH = new byte[] { (byte)num, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x02 }; //byte[] bin = new byte[sMagTH.Length + 2]; //CRC체크상환받을곳 //Array.ConstrainedCopy(sMagTH, 0, bin, 0, sMagTH.Length); //CRC체크하기 //Array.ConstrainedCopy(c.CRC16(sMagTH.Length, sMagTH), 0, bin, sMagTH.Length, 2); serialPortTH2.Write(bin2, 0, bin2.Length); //통신 요청 타이머 가동 t2.Start(); }