public static String GetMessageString(PromptOccurPlace promptOccurPlace, PromptMessageType promptMessageType, Object obj) { String messageString = String.Empty; LanguageLibrary languageLibrary = new LanguageLibrary("中文"); String[] messagePlaces = languageLibrary.PromptOccurPlace; String[] messageTypes = languageLibrary.PromptMessageType; int placeIndex = (int)promptOccurPlace; int typeIndex = (int)promptMessageType; if (placeIndex < messagePlaces.Length && typeIndex < messageTypes.Length) { String[] messageFields = languageLibrary.MessageFiledStrings; messageString += String.Format("{0} {1}\n", messageFields[0], messagePlaces[placeIndex]); messageString += String.Format("{0} {1}\n", messageFields[1], messageTypes[typeIndex]); if (obj != null) { messageString += String.Format("{0} {1}", messageFields[2], obj.ToString()); } } else { if (obj != null) { messageString = obj.ToString(); } } return(messageString); }
public void SendSerialData(byte[] serialDatas, byte comd) { int length = serialDatas.Length; byte[] sendDatas = new byte[length + 5]; try { sendDatas[0] = comd; sendDatas[1] = Convert.ToByte(length + 5); sendDatas[2] = Convert.ToByte((length + 5) >> 8); for (int i = 0; i < length; i++) { sendDatas[i + 3] = serialDatas[i]; } int crcValue = 0xFFFF; int index = 0; for (; index < length + 3; index++) { crcValue ^= sendDatas[index]; for (int j = 0; j < 8; j++) { if ((crcValue & 0x01) != 0) { crcValue = (crcValue >> 1) ^ 0xa001; } else { crcValue = crcValue >> 1; } } } sendDatas[index] = Convert.ToByte(crcValue & 0xFF); sendDatas[index + 1] = Convert.ToByte((crcValue >> 8) & 0xFF); serialPort.DiscardOutBuffer(); serialPort.Write(sendDatas, 0, length + 5); } catch (Exception ex) { PromptMessageType promptMessageType = PromptMessageType.Null; PromptOccurPlace promptOccurPlace = PromptOccurPlace.SerialPortSendError; if (ex is ArgumentOutOfRangeException) { promptMessageType = PromptMessageType.ArgumentOutOfRangeException; } else if (ex is InvalidOperationException) { promptMessageType = PromptMessageType.InvalidOperationException; } else if (ex is TimeoutException) { promptMessageType = PromptMessageType.TimeoutException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType); } }
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口 接收 { byte[] receiveDatas = null; byte receiveComd = 0; int bufferLength = serialPort.BytesToRead; try { for (int i = 0; i < bufferLength; i++) { if (serialPort.IsOpen) { communicatHelper.WriteByteToBuffer(Convert.ToByte(serialPort.ReadByte())); } } int checkLength = communicatHelper.GetCheckDataLength(); int dataLength = communicatHelper.GetDataLength(); if (dataLength == checkLength && dataLength > 2 && communicatHelper.GetComd() != 0) { if (communicatHelper.CrcCheckOut()) { receiveDatas = communicatHelper.GetRecieveData(); receiveComd = communicatHelper.GetComd(); } } } catch (Exception ex) { PromptMessageType promptMessageType = PromptMessageType.Null; PromptOccurPlace promptOccurPlace = PromptOccurPlace.SerialPortReceiveError; if (ex is EndOfStreamException) { promptMessageType = PromptMessageType.EndOfStreamException; } else if (ex is ObjectDisposedException) { promptMessageType = PromptMessageType.ObjectDisposedException; } else if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType); } OnDataArrived(receiveDatas, receiveComd); }
/// <summary> /// 遍历整个Xml,将读取元素名和元素的值 /// </summary> /// <param name="xmlName"></param> /// <returns></returns> public static InfoHashTable ReadXmlValues(String xmlName) { InfoHashTable infoHashTable = new InfoHashTable(); if (File.Exists(xmlName)) { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(xmlName); XmlNode rootNode = xmlDocument.DocumentElement; TraverseXmlNode(rootNode, infoHashTable); xmlDocument.Save(xmlName); xmlDocument = null; } catch (Exception ex) { PromptOccurPlace promptOccurPlace = PromptOccurPlace.XmlReadError; PromptMessageType promptMessageType = PromptMessageType.Null; if (ex is XmlException) { promptMessageType = PromptMessageType.XmlException; } else if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is NullReferenceException) { promptMessageType = PromptMessageType.NullReferenceException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else if (ex is StackOverflowException) { promptMessageType = PromptMessageType.StackOverflowException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, Path.GetFileName(xmlName)); } } return(infoHashTable); }
public static void SetElementValue(String fileName, String elementName, String elementValue) { if (File.Exists(fileName)) { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(fileName); XmlNode xmlNode = xmlDocument.DocumentElement; XmlNode currentNode = xmlNode.SelectSingleNode(elementName); if (currentNode != null) { currentNode.InnerText = elementValue; } xmlDocument.Save(fileName); } catch (Exception ex) { PromptOccurPlace promptOccurPlace = PromptOccurPlace.XmlWriteError; PromptMessageType promptMessageType = PromptMessageType.Null; if (ex is XmlException) { promptMessageType = PromptMessageType.XmlException; } else if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else if (ex is NullReferenceException) { promptMessageType = PromptMessageType.NullReferenceException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, Path.GetFileName(fileName)); } finally { xmlDocument = null; } } }
public void OpenSerialPort() { if (serialPort == null) { serialPort = new SerialPort(); serialPort.BaudRate = 19200; serialPort.DataBits = 8; serialPort.PortName = "COM1"; serialPort.StopBits = StopBits.One; serialPort.ReadTimeout = 200; serialPort.WriteTimeout = 200; serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); } if (!serialPort.IsOpen) { try { serialPort.Open(); } catch (Exception ex) { PromptMessageType promptMessageType = PromptMessageType.Null; PromptOccurPlace promptOccurPlace = PromptOccurPlace.SerialPortSendError; if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else { promptMessageType = PromptMessageType.UnKnown; } String portInfos = String.Empty; foreach (String name in SerialPort.GetPortNames()) { portInfos += name + " "; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, portInfos); } } communicatHelper = new CommunicatHelper(); }
public static void AppendElementValues(String elementName, String elementValue, String xmlName) { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(xmlName); XmlNode rootNode = xmlDocument.DocumentElement; XmlElement appendElement = xmlDocument.CreateElement(elementName); appendElement.InnerText = elementValue; rootNode.AppendChild(appendElement); xmlDocument.Save(xmlName); xmlDocument = null; } catch (Exception ex) { PromptOccurPlace promptOccurPlace = PromptOccurPlace.XmlWriteError; PromptMessageType promptMessageType = PromptMessageType.Null; if (ex is XmlException) { promptMessageType = PromptMessageType.XmlException; } else if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is NullReferenceException) { promptMessageType = PromptMessageType.NullReferenceException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, Path.GetFileName(xmlName)); } }
public static void SetErrorMessage(PromptOccurPlace promptOccurPlace, PromptMessageType promptMessageType) { OnOccurError(new ErrorMessage(promptOccurPlace, promptMessageType, null)); }
public static void SetErrorMessage(PromptOccurPlace promptOccurPlace, PromptMessageType promptMessageType, object errorInfo) { OnOccurError(new ErrorMessage(promptOccurPlace, promptMessageType, errorInfo)); }
private ErrorMessage(PromptOccurPlace promptOccurPlace, PromptMessageType promptMessageType, object errorInfo) { this.promptMessageType = promptMessageType; this.promptOccurPlace = promptOccurPlace; this.errorInfo = errorInfo; }
public static void AppendElementValues(InfoHashTable hashTable, String xmlName) { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(xmlName); XmlNode rootNode = xmlDocument.DocumentElement; XmlNode appendNode = null; XmlElement appendElement = null; foreach (String key in hashTable.Keys) { if (hashTable[key] != null) { appendElement = xmlDocument.CreateElement(key); appendElement.InnerText = hashTable[key].ToString(); if (appendNode != null) { appendNode.AppendChild(appendElement); } } else { if (key != "End") { appendNode = xmlDocument.CreateElement(key); } else { rootNode.AppendChild(appendNode); } } } xmlDocument.Save(xmlName); } catch (Exception ex) { PromptOccurPlace promptOccurPlace = PromptOccurPlace.XmlWriteError; PromptMessageType promptMessageType = PromptMessageType.Null; if (ex is XmlException) { promptMessageType = PromptMessageType.XmlException; } else if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is NullReferenceException) { promptMessageType = PromptMessageType.NullReferenceException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, Path.GetFileName(xmlName)); } }
/// <summary> /// 该函数用于读写只有一个父级,最简单的XML格式 /// </summary> /// <param name="xmlName"></param> /// <param name="elementNames"></param> /// <returns></returns> public static InfoHashTable ReadXmlValues(String xmlName, String[] elementNams) { InfoHashTable infoHashTable = null; if (File.Exists(xmlName)) { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(xmlName); XmlNode xmlRootNode = xmlDocument.DocumentElement; XmlNode xmlCurrentNode = null; infoHashTable = new InfoHashTable(); List <String> notFoundDataList = new List <string>(); for (int i = 0; i < elementNams.Length; i++) { xmlCurrentNode = xmlRootNode.SelectSingleNode(elementNams[i]); if (xmlCurrentNode != null) { infoHashTable.Add(elementNams[i], xmlCurrentNode.InnerText); } else { infoHashTable.Add(elementNams[i], "0"); notFoundDataList.Add(elementNams[i]); } } if (notFoundDataList.Count > 0) { for (int i = 0; i < notFoundDataList.Count; i++) { AppendElementValues(notFoundDataList[i], "0", xmlName); } } } catch (Exception ex) { PromptOccurPlace promptOccurPlace = PromptOccurPlace.XmlReadError; PromptMessageType promptMessageType = PromptMessageType.Null; if (ex is XmlException) { promptMessageType = PromptMessageType.XmlException; } else if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is NullReferenceException) { promptMessageType = PromptMessageType.NullReferenceException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, Path.GetFileName(xmlName)); } finally { xmlDocument = null; } } return(infoHashTable); }
public static void CreateXml(InfoHashTable hashTable, String xmlName) { String directName = Path.GetDirectoryName(xmlName); if (!Directory.Exists(directName)) { Directory.CreateDirectory(directName); } XmlTextWriter xmlTextWriter = null; try { xmlTextWriter = new XmlTextWriter(xmlName, Encoding.UTF8); xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.WriteStartDocument(); foreach (SingleHashTable singleHashTable in hashTable) { if (singleHashTable.Value != null) { xmlTextWriter.WriteElementString(singleHashTable.Key.ToString(), singleHashTable.Value.ToString()); } else { if (singleHashTable.Key.ToString() == "End") { xmlTextWriter.WriteEndElement(); } else { xmlTextWriter.WriteStartElement(singleHashTable.Key.ToString()); } } } xmlTextWriter.WriteEndDocument(); } catch (Exception ex) { PromptOccurPlace promptOccurPlace = PromptOccurPlace.XmlWriteError; PromptMessageType promptMessageType = PromptMessageType.Null; if (ex is IOException) { promptMessageType = PromptMessageType.IOException; } else if (ex is UnauthorizedAccessException) { promptMessageType = PromptMessageType.UnauthorizedAccessException; } else if (ex is NullReferenceException) { promptMessageType = PromptMessageType.NullReferenceException; } else if (ex is SecurityException) { promptMessageType = PromptMessageType.SecurityException; } else { promptMessageType = PromptMessageType.UnKnown; } ErrorMessage.SetErrorMessage(promptOccurPlace, promptMessageType, Path.GetFileName(xmlName)); } finally { if (xmlTextWriter != null) { xmlTextWriter.Flush(); xmlTextWriter.Close(); xmlTextWriter = null; } } }