private Byte[] mN_ToHex(string SmN) { int mN = int.Parse(SmN); string mNHex = Convert.ToString(mN, 16); string returnString = ""; string mNSendMessage = "F8FA0006000A4E756D626572" + mNHex.PadLeft(4, '0').ToUpper(); Byte[] crcByte = new byte[mNSendMessage.Length / 2]; for (int i = 0; i < mNSendMessage.Length; i = i + 2) { crcByte[i / 2] = Convert.ToByte(mNSendMessage.Substring(i, 2), 16); } CommunicationBase cb = new CommunicationBase(); ushort crc = cb.CRC16CCITT(crcByte); string crcString = crc.ToString("X2"); if (crc < 1000) { crcString = "0" + crc.ToString(); } returnString = mNSendMessage + crcString; Byte[] returnByte = new byte[returnString.Length / 2]; // string to byte. for (int i = 0; i < returnString.Length; i = i + 2) { returnByte[i / 2] = Convert.ToByte(returnString.Substring(i, 2), 16); } return(returnByte); }
private void ThModify(object sender) { string thRecordTime_index = (string)sender; string head = thRecordTime_index.Split('*')[0]; string thRecordTime = thRecordTime_index.Split('*')[1].PadLeft(2, '0'); int index = int.Parse(thRecordTime_index.Split('*')[2]); int statusPosition = int.Parse(thRecordTime_index.Split('*')[3]); string recordTimeHex = ""; if (head.Equals("F8FA0005000A52656C6F636B00")) { Thread.Sleep(500); } for (int i = 0; i < thRecordTime.Length; i += 2) { recordTimeHex += int.Parse(thRecordTime.Substring(i, 2)).ToString("X2").PadLeft(2, '0'); } recordTimeHex = head + recordTimeHex; byte[] CrcByte = new byte[recordTimeHex.Length / 2]; for (int i = 0; i < recordTimeHex.Length; i += 2) { CrcByte[i / 2] = Convert.ToByte(recordTimeHex.Substring(i, 2), 16); } ushort CRC = cb.CRC16CCITT(CrcByte); string CRCInt = CRC.ToString("X2").PadLeft(4, '0'); string sendMsg = recordTimeHex + CRCInt; byte[] sendMsgByte = new byte[sendMsg.Length / 2]; for (int i = 0; i < sendMsg.Length; i += 2) { sendMsgByte[i / 2] = Convert.ToByte(sendMsg.Substring(i, 2), 16); } string reciveMsg = cb.Connection(sendMsgByte, ip); reciveMsg = reciveMsg.Replace(" ", ""); string status = reciveMsg.Substring(statusPosition, 2); if (status.Equals("01")) { isModifyFinish[index] = true; } string[] ipArray = ip.Split('.'); string ipSettingPath = Application.StartupPath + "\\DeviceInfo_Setting\\" + ipArray[0].PadLeft(3, '0') + "." + ipArray[1].PadLeft(3, '0') + "." + ipArray[2].PadLeft(3, '0') + "." + ipArray[3].PadLeft(3, '0') + ".ini"; StreamWriter sw1 = new StreamWriter(ipSettingPath); sw1.WriteLine(thRecordTime); sw1.Dispose(); }
private Byte[] IP_Mask_Gateway_ToHex() { string returnString = ""; string mN = mN_Ip_Mask_Gateway.Split('|')[0]; string ip = mN_Ip_Mask_Gateway.Split('|')[1]; string mask = mN_Ip_Mask_Gateway.Split('|')[2]; string gateway = mN_Ip_Mask_Gateway.Split('|')[3]; string[] ipArray = ip.Split('.'); string[] maskArray = mask.Split('.'); string[] gatewayArray = gateway.Split('.'); for (int i = 0; i < ipArray.Length; i++) { string hex = Convert.ToString(Int32.Parse(ipArray[i]), 16).ToUpper(); if (hex.Length < 2) { returnString += "0" + hex; } else { returnString += hex; } } for (int i = 0; i < maskArray.Length; i++) { string hex = Convert.ToString(Int32.Parse(maskArray[i]), 16).ToUpper(); if (hex.Length < 2) { returnString += "0" + hex; } else { returnString += hex; } } for (int i = 0; i < gatewayArray.Length; i++) { string hex = Convert.ToString(Int32.Parse(gatewayArray[i]), 16).ToUpper(); if (hex.Length < 2) { returnString += "0" + hex; } else { returnString += hex; } } returnString = "F8FA0001000F00" + returnString; Byte[] crcByte = new byte[returnString.Length / 2]; for (int i = 0; i < returnString.Length; i = i + 2) { crcByte[i / 2] = Convert.ToByte(returnString.Substring(i, 2), 16); } Byte[] sendMessage = new byte[] { 0xF8, 0xFA, 0x00, 0x01, 0x00, 0x0F, 0x00, 0xC0, 0xA8, 0x01, 0x03, 0xFF, 0xFF, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0xFE }; CommunicationBase cb = new CommunicationBase(); ushort crc = cb.CRC16CCITT(crcByte); string crcString = crc.ToString("X2"); if (crc < 1000) { crcString = "0" + crc.ToString(); } returnString = returnString + crcString; Byte[] returnByte = new byte[returnString.Length / 2]; for (int i = 0; i < returnString.Length; i = i + 2) { returnByte[i / 2] = Convert.ToByte(returnString.Substring(i, 2), 16); } return(returnByte); }
private void ThButtonDo() { bool isDo = true; string cardNumber = textBoxInput.Text; if (cardNumber.Length % 2 != 0 || cardNumber == "") { MessageBox.Show("輸入長度錯誤\r\n必須為偶數", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { // Stop RF. Byte[] sandStopMsgByte = new Byte[] { 0xFA }; string reciveMsgForStopRF = cb.Connection(sandStopMsgByte, labelIp.Text); // 新增名單 if (buttonDo.Text.Equals("新增")) { // CRC-3334 byte[] CRC3334Byte = new byte[cardNumber.Length / 2]; for (int i = 0; i < cardNumber.Length; i += 2) { CRC3334Byte[i / 2] = Convert.ToByte(cardNumber.Substring(i, 2), 16); } ushort CRC3334 = cb.CRC16CCITT(CRC3334Byte); string CRC3334String = CRC3334.ToString("X2"); // CRC-3536 string beforeCRC3536String = "0001" /*labelListCount.Text.Split(':')[1].Substring(0, 4)*/ + cardNumber + "0000000000000041FFFF" + CRC3334String; byte[] CRC3536Byte = new byte[beforeCRC3536String.Length / 2]; for (int i = 0; i < beforeCRC3536String.Length; i += 2) { CRC3536Byte[i / 2] = Convert.ToByte(beforeCRC3536String.Substring(i, 2), 16); } ushort CRC3536 = cb.CRC16CCITT(CRC3536Byte); string afterCRC3536String = CRC3536.ToString("X2").PadLeft(4, '0'); // CRC-HeadToEnd string beforeCRCHeadEndString = "F8FA000000242101" + beforeCRC3536String + afterCRC3536String + "00000000"; byte[] CRCHeadEndByte = new byte[beforeCRCHeadEndString.Length / 2]; for (int i = 0; i < beforeCRCHeadEndString.Length; i += 2) { CRCHeadEndByte[i / 2] = Convert.ToByte(beforeCRCHeadEndString.Substring(i, 2), 16); } ushort CRCHeadEnd = cb.CRC16CCITT(CRCHeadEndByte); string afterCRCHeadEndtring = CRCHeadEnd.ToString("X2"); string sendMessage = beforeCRCHeadEndString + afterCRCHeadEndtring; byte[] sendMessageByte = new byte[sendMessage.Length / 2]; for (int i = 0; i < sendMessage.Length; i += 2) { sendMessageByte[i / 2] = Convert.ToByte(sendMessage.Substring(i, 2), 16); } string reciveMessage = cb.Connection(sendMessageByte, labelIp.Text); reciveMessage = reciveMessage.Replace(" ", ""); string status = reciveMessage.Substring(12, 2); string showMessage = ""; string showMessageTitle = ""; MessageBoxIcon messageBoxIcon = MessageBoxIcon.Information; if (status.Equals("01")) { messageBoxIcon = MessageBoxIcon.Information; showMessageTitle = "提醒"; showMessage = "新增成功"; ListCount(); } else if (status.Equals("02")) { messageBoxIcon = MessageBoxIcon.Error; showMessageTitle = "錯誤"; showMessage = "新增失敗\r\n無法更改。"; } else if (status.Equals("05")) { messageBoxIcon = MessageBoxIcon.Error; showMessageTitle = "錯誤"; showMessage = "新增失敗\r\n已有相同卡號。"; } else if (status.Equals("06")) { messageBoxIcon = MessageBoxIcon.Error; showMessageTitle = "錯誤"; showMessage = "新增失敗\r\n名單已滿。"; } else { messageBoxIcon = MessageBoxIcon.Error; showMessageTitle = "錯誤"; showMessage = "未知錯誤\r\n請聯絡工程單位。"; } MessageBox.Show(showMessage, showMessageTitle, MessageBoxButtons.OK, messageBoxIcon); } // Delete else if (buttonDo.Text.Equals("刪除")) { string beforeCRCString = "F8FA000000102201" + cardNumber; byte[] CRCbyte = new byte[beforeCRCString.Length / 2]; for (int i = 0; i < beforeCRCString.Length; i += 2) { CRCbyte[i / 2] = Convert.ToByte(beforeCRCString.Substring(i, 2), 16); } ushort CRC = cb.CRC16CCITT(CRCbyte); string CRCString = CRC.ToString("X2"); string sendMsg = beforeCRCString + CRCString; byte[] sendMsgByte = new byte[sendMsg.Length / 2]; for (int i = 0; i < sendMsg.Length; i += 2) { sendMsgByte[i / 2] = Convert.ToByte(sendMsg.Substring(i, 2), 16); } string reciveMsg = cb.Connection(sendMsgByte, labelIp.Text); reciveMsg = reciveMsg.Replace(" ", ""); string status = reciveMsg.Substring(12, 2); string showMsg = ""; string showTitle = ""; MessageBoxIcon messageBoxIcon = MessageBoxIcon.Information; if (status.Equals("01")) { showMsg = "刪除成功"; showTitle = "提醒"; messageBoxIcon = MessageBoxIcon.Information; ListCount(); } else if (status.Equals("02")) { showMsg = "無法更改"; showTitle = "提醒"; messageBoxIcon = MessageBoxIcon.Error; } else if (status.Equals("05")) { showMsg = "已有相同卡號"; showTitle = "提醒"; messageBoxIcon = MessageBoxIcon.Error; } else { showMsg = "嚴重錯誤'\r\n請洽工程人員。"; showTitle = "錯誤"; messageBoxIcon = MessageBoxIcon.Error; } MessageBox.Show(showMsg, showTitle, MessageBoxButtons.OK, messageBoxIcon); } else if (buttonDo.Text.Equals("搜尋")) { string beforeCRCString = "F8FA00070011526561640E" + cardNumber; byte[] CRCbyte = new byte[beforeCRCString.Length / 2]; for (int i = 0; i < beforeCRCString.Length; i += 2) { CRCbyte[i / 2] = Convert.ToByte(beforeCRCString.Substring(i, 2), 16); } ushort CRC = cb.CRC16CCITT(CRCbyte); string CRCString = CRC.ToString("X2"); string sendMsg = beforeCRCString + CRCString; byte[] sendMsgbyte = new byte[sendMsg.Length / 2]; for (int i = 0; i < sendMsg.Length; i += 2) { sendMsgbyte[i / 2] = Convert.ToByte(sendMsg.Substring(i, 2), 16); } byte[] a = new byte[] { 0xF8, 0xFA, 0x00, 0x07, 0x00, 0x11, 0x52, 0x65, 0x61, 0x64, 0x0E, 0xCC, 0xBB, 0xAA, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xAA, 0xDA, 0xC9 }; string reciveMsg = cb.Connection(a, labelIp.Text); } Thread.Sleep(500); // Start RF. Byte[] sandStartMsgByte = new Byte[] { 0xFA, 0xF8, 0x00, 0x05, 0x00, 0x06, 0xFF, 0x1D, 0x01, 0x01, 0xAE, 0x78 }; string reciveMsgForStartRF = cb.Connection(sandStartMsgByte, labelIp.Text); } this.Invoke((MethodInvoker) delegate { panelDo.Enabled = true; this.Cursor = Cursors.Arrow; }); }
private void SchoolTime() { CommunicationBase cb = new CommunicationBase(); string week = ""; if (DateTime.Now.DayOfWeek.ToString().Equals("Monday")) { week = "01"; } else if (DateTime.Now.DayOfWeek.ToString().Equals("Tuesday")) { week = "02"; } else if (DateTime.Now.DayOfWeek.ToString().Equals("Wednesday")) { week = "03"; } else if (DateTime.Now.DayOfWeek.ToString().Equals("Thursday")) { week = "04"; } else if (DateTime.Now.DayOfWeek.ToString().Equals("Friday")) { week = "05"; } else if (DateTime.Now.DayOfWeek.ToString().Equals("Satursday")) { week = "06"; } else if (DateTime.Now.DayOfWeek.ToString().Equals("Sunday")) { week = "07"; } string date = DateTime.Now.ToString("yyyy MM dd " + week + " HH mm ss"); //date = "2017 04 11 02 16 13 00"; string[] dateArray = date.Split(' '); string dateHex = ""; for (int i = 0; i < dateArray.Length; i++) { if (i == 0) { for (int j = 0; j < dateArray[i].Length; j += 2) { dateHex += int.Parse(dateArray[i].Substring(j, 2)).ToString("X2").PadLeft(2, '0'); } } else { dateHex += int.Parse(dateArray[i]).ToString("X2").PadLeft(2, '0'); } } //for (int i = 0; i < dateArray.Length; i++) //{ // string temp = ""; // temp= int.Parse(dateArray[i]).ToString("X2"); // if (temp.Length == 3) // { // temp = temp.PadLeft(4, '0'); // } // else // { // temp = temp.PadLeft(2, '0'); // } // dateHex += temp; //} string beforeCRCMsg = "F8FC0000000A" + dateHex; byte[] CRCByte = new byte[beforeCRCMsg.Length / 2]; for (int i = 0; i < beforeCRCMsg.Length; i += 2) { CRCByte[i / 2] = Convert.ToByte(beforeCRCMsg.Substring(i, 2), 16); } ushort CRC = cb.CRC16CCITT(CRCByte); string afterCRCMsg = CRC.ToString("X2"); string sendMsg = /*"f8fc0000000a14140306050f2e2336a6"*/ beforeCRCMsg + afterCRCMsg.PadLeft(4, '0'); byte[] sendMsgByte = new byte[sendMsg.Length / 2]; for (int i = 0; i < sendMsg.Length; i += 2) { sendMsgByte[i / 2] = Convert.ToByte(sendMsg.Substring(i, 2), 16); } IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 50001); UdpClient uc = new UdpClient(); uc.Send(sendMsgByte, sendMsgByte.Length, ipep); byte[] receiveBytes = uc.Receive(ref ipep); uc.Dispose(); }