void CreatePort() { ComPortUartConfig uartConfig = comPortConfig.UartConfig; sp = new System.IO.Ports.SerialPort(); // sp.PortName = comPortConfig.PortName; // do this when opening the port sp.BaudRate = uartConfig.BaudRateInt; sp.DataBits = uartConfig.DataBits; sp.Handshake = uartConfig.Handshake; sp.Parity = uartConfig.Parity; sp.StopBits = uartConfig.StopBits; sp.RtsEnable = uartConfig.Handshake == System.IO.Ports.Handshake.None; sp.DtrEnable = true; sp.ReadTimeout = 0; // read operations are non-blocking at this level sp.WriteTimeout = 100; // write operations are blocking at this level - we assume that there will always be enough buffer space to accept the bytes relatively quickly! // sp.Encoding; // accept default of Ascii sp.ReadBufferSize = (int)PortConfig.RxBufferSize; sp.WriteBufferSize = (int)PortConfig.TxBufferSize; sp.ReceivedBytesThreshold = 1; // fire event after 1 character sp.PinChanged += delegate(object sender, SerialPinChangedEventArgs e) { TraceData.Emit("<PinChangedEvent type='{0}'/>", e.EventType); threadWakeupNotifier.Notify(); }; sp.ErrorReceived += delegate(object sender, SerialErrorReceivedEventArgs e) { TraceData.Emit("<ErrorReceivedEvent type='{0}'/>", e.EventType); threadWakeupNotifier.Notify(); }; sp.DataReceived += delegate(object sender, SerialDataReceivedEventArgs e) { threadWakeupNotifier.Notify(); }; }
public bool ParseString(ref StringScanner scan) { bool success = true; this = new ComPortUartConfig(); // clear scan.SkipOverWhiteSpace(); if (scan.MatchToken("<UartConfig", false, false)) { fromXml = true; bool endFound = false; // mode, baud, dataBits, stopBits, parity while (!endFound && success && scan.IsIdxValid) { if (scan.MatchToken("/>", false, false)) { endFound = true; continue; } if (!(scan.ParseXmlAttribute("Baud", out baudRate) && baudRate >= 0.0) && !scan.ParseXmlAttribute("DataBits", DataBitsCharTokenValueMap, out dataBits) && !scan.ParseXmlAttribute("Mode", PortModeTokenValueMap, out portMode) && !scan.ParseXmlAttribute("Parity", ParityTokenValueMap, out parity) && !scan.ParseXmlAttribute("StopBits", StopBitsCharTokenValueMap, out stopBits) ) { success = false; } } if (!endFound) success = false; } else { // parse as dos mode style string token; success = scan.ExtractToken(out token) && success; string [] tokens = token.Split(DosModeDelimiters); int idx = 0; string baudStr = (tokens.Length > idx ? tokens[idx++] : string.Empty); string parityCharStr = (tokens.Length > idx ? tokens[idx++] : string.Empty); string dataBitsCharStr = (tokens.Length > idx ? tokens[idx++] : string.Empty); string stopBitsCharStr = (tokens.Length > idx ? tokens[idx++] : string.Empty); string modeCharStr = (tokens.Length > idx ? tokens[idx++] : string.Empty); success = (tokens.Length >= 4 && tokens.Length <= 5); success = Double.TryParse(baudStr, out baudRate) && success; success = StringScanner.FindTokenValueByName(parityCharStr, ParityCharTokenValueMap, out parity) && success; success = StringScanner.FindTokenValueByName(dataBitsCharStr, DataBitsCharTokenValueMap, out dataBits) && success; success = StringScanner.FindTokenValueByName(stopBitsCharStr, StopBitsCharTokenValueMap, out stopBits) && success; success = StringScanner.FindTokenValueByName(modeCharStr, PortModeCharTokenValueMap, out portMode) && success; } scan.SkipOverWhiteSpace(); return success; }