public static string SerialCommPortToString(SerialCommPort port) { if (port == SerialCommPort.COM01) { return "COM1"; } if (port == SerialCommPort.COM02) { return "COM2"; } if (port == SerialCommPort.COM03) { return "COM3"; } if (port == SerialCommPort.COM04) { return "COM4"; } if (port == SerialCommPort.COM05) { return "COM5"; } if (port == SerialCommPort.COM06) { return "COM6"; } if (port == SerialCommPort.COM07) { return "COM7"; } if (port == SerialCommPort.COM08) { return "COM8"; } if (port == SerialCommPort.COM09) { return "COM9"; } if (port == SerialCommPort.COM10) { return "COM10"; } if (port == SerialCommPort.COM11) { return "COM11"; } if (port == SerialCommPort.COM12) { return "COM12"; } if (port == SerialCommPort.COM13) { return "COM13"; } if (port == SerialCommPort.COM14) { return "COM14"; } if (port == SerialCommPort.COM15) { return "COM15"; } if (port == SerialCommPort.COM16) { return "COM16"; } if (port == SerialCommPort.COM17) { return "COM17"; } if (port == SerialCommPort.COM18) { return "COM18"; } if (port == SerialCommPort.COM19) { return "COM19"; } if (port == SerialCommPort.COM20) { return "COM20"; } if (port == SerialCommPort.COM21) { return "COM21"; } if (port == SerialCommPort.COM22) { return "COM22"; } if (port == SerialCommPort.COM23) { return "COM23"; } if (port == SerialCommPort.COM24) { return "COM24"; } if (port == SerialCommPort.COM25) { return "COM25"; } if (port == SerialCommPort.COM26) { return "COM26"; } if (port == SerialCommPort.COM27) { return "COM27"; } if (port == SerialCommPort.COM28) { return "COM28"; } if (port == SerialCommPort.COM29) { return "COM29"; } if (port == SerialCommPort.COM30) { return "COM30"; } if (port == SerialCommPort.COM31) { return "COM31"; } if (port == SerialCommPort.COM32) { return "COM32"; } if (port == SerialCommPort.COM33) { return "COM33"; } if (port == SerialCommPort.COM34) { return "COM34"; } if (port == SerialCommPort.COM35) { return "COM35"; } if (port == SerialCommPort.COM36) { return "COM36"; } if (port == SerialCommPort.COM37) { return "COM37"; } if (port == SerialCommPort.COM38) { return "COM38"; } if (port == SerialCommPort.COM39) { return "COM39"; } if (port == SerialCommPort.COM40) { return "COM40"; } if (port == SerialCommPort.COM41) { return "COM41"; } if (port == SerialCommPort.COM42) { return "COM42"; } if (port == SerialCommPort.COM43) { return "COM43"; } if (port == SerialCommPort.COM44) { return "COM44"; } if (port == SerialCommPort.COM45) { return "COM45"; } if (port == SerialCommPort.COM46) { return "COM46"; } if (port == SerialCommPort.COM47) { return "COM47"; } if (port == SerialCommPort.COM48) { return "COM48"; } if (port == SerialCommPort.COM49) { return "COM49"; } if (port == SerialCommPort.COM50) { return "COM50"; } if (port == SerialCommPort.COM51) { return "COM51"; } if (port == SerialCommPort.COM52) { return "COM52"; } if (port == SerialCommPort.COM53) { return "COM53"; } if (port == SerialCommPort.COM54) { return "COM54"; } if (port == SerialCommPort.COM55) { return "COM55"; } if (port == SerialCommPort.COM56) { return "COM56"; } if (port == SerialCommPort.COM57) { return "COM57"; } if (port == SerialCommPort.COM58) { return "COM58"; } if (port == SerialCommPort.COM59) { return "COM59"; } if (port == SerialCommPort.COM60) { return "COM60"; } if (port == SerialCommPort.COM61) { return "COM61"; } if (port == SerialCommPort.COM62) { return "COM62"; } if (port == SerialCommPort.COM63) { return "COM63"; } if (port == SerialCommPort.COM64) { return "COM64"; } if (port == SerialCommPort.COM65) { return "COM65"; } if (port == SerialCommPort.COM66) { return "COM66"; } if (port == SerialCommPort.COM67) { return "COM67"; } if (port == SerialCommPort.COM68) { return "COM68"; } if (port == SerialCommPort.COM69) { return "COM69"; } if (port == SerialCommPort.COM70) { return "COM70"; } if (port == SerialCommPort.COM81) { return "COM81"; } if (port == SerialCommPort.COM82) { return "COM82"; } if (port == SerialCommPort.COM83) { return "COM83"; } if (port == SerialCommPort.COM84) { return "COM84"; } if (port == SerialCommPort.COM85) { return "COM85"; } if (port == SerialCommPort.COM86) { return "COM86"; } if (port == SerialCommPort.COM87) { return "COM87"; } if (port == SerialCommPort.COM88) { return "COM88"; } if (port == SerialCommPort.COM89) { return "COM89"; } if (port == SerialCommPort.COM90) { return "COM90"; } if (port == SerialCommPort.COM91) { return "COM91"; } if (port == SerialCommPort.COM92) { return "COM92"; } if (port == SerialCommPort.COM93) { return "COM93"; } if (port == SerialCommPort.COM94) { return "COM94"; } if (port == SerialCommPort.COM95) { return "COM95"; } if (port == SerialCommPort.COM96) { return "COM96"; } if (port == SerialCommPort.COM97) { return "COM97"; } if (port == SerialCommPort.COM98) { return "COM98"; } if (port == SerialCommPort.COM99) { return "COM99"; } if (port == SerialCommPort.COM100) { return "COM100"; } if (port == SerialCommPort.COM101) { return "COM101"; } if (port == SerialCommPort.COM102) { return "COM102"; } if (port == SerialCommPort.COM103) { return "COM103"; } if (port == SerialCommPort.COM104) { return "COM104"; } if (port == SerialCommPort.COM105) { return "COM105"; } if (port == SerialCommPort.COM106) { return "COM106"; } if (port == SerialCommPort.COM107) { return "COM107"; } if (port == SerialCommPort.COM108) { return "COM108"; } if (port == SerialCommPort.COM109) { return "COM109"; } if (port == SerialCommPort.COM110) { return "COM110"; } if (port == SerialCommPort.COM111) { return "COM111"; } if (port == SerialCommPort.COM112) { return "COM112"; } if (port == SerialCommPort.COM113) { return "COM113"; } if (port == SerialCommPort.COM114) { return "COM114"; } if (port == SerialCommPort.COM115) { return "COM115"; } if (port == SerialCommPort.COM116) { return "COM116"; } if (port == SerialCommPort.COM117) { return "COM117"; } if (port == SerialCommPort.COM118) { return "COM118"; } if (port == SerialCommPort.COM119) { return "COM119"; } if (port == SerialCommPort.COM120) { return "COM120"; } if (port == SerialCommPort.COM121) { return "COM121"; } if (port == SerialCommPort.COM122) { return "COM122"; } if (port == SerialCommPort.COM123) { return "COM123"; } if (port == SerialCommPort.COM124) { return "COM124"; } if (port == SerialCommPort.COM125) { return "COM125"; } if (port == SerialCommPort.COM126) { return "COM126"; } if (port == SerialCommPort.COM127) { return "COM127"; } if (port == SerialCommPort.COM128) { return "COM128"; } if (port == SerialCommPort.COM129) { return "COM129"; } if (port == SerialCommPort.COM130) { return "COM130"; } if (port == SerialCommPort.COM131) { return "COM131"; } if (port == SerialCommPort.COM132) { return "COM132"; } if (port == SerialCommPort.COM133) { return "COM133"; } if (port == SerialCommPort.COM134) { return "COM134"; } if (port == SerialCommPort.COM135) { return "COM135"; } if (port == SerialCommPort.COM136) { return "COM136"; } if (port == SerialCommPort.COM137) { return "COM137"; } if (port == SerialCommPort.COM138) { return "COM138"; } if (port == SerialCommPort.COM139) { return "COM139"; } if (port == SerialCommPort.COM140) { return "COM140"; } if (port == SerialCommPort.COM141) { return "COM141"; } if (port == SerialCommPort.COM142) { return "COM142"; } if (port == SerialCommPort.COM143) { return "COM143"; } if (port == SerialCommPort.COM144) { return "COM144"; } if (port == SerialCommPort.COM145) { return "COM145"; } if (port == SerialCommPort.COM146) { return "COM146"; } if (port == SerialCommPort.COM147) { return "COM147"; } if (port == SerialCommPort.COM148) { return "COM148"; } if (port == SerialCommPort.COM149) { return "COM149"; } if (port == SerialCommPort.COM150) { return "COM150"; } if (port == SerialCommPort.COM151) { return "COM151"; } if (port == SerialCommPort.COM152) { return "COM152"; } if (port == SerialCommPort.COM153) { return "COM153"; } if (port == SerialCommPort.COM154) { return "COM154"; } if (port == SerialCommPort.COM155) { return "COM155"; } if (port == SerialCommPort.COM156) { return "COM156"; } if (port == SerialCommPort.COM157) { return "COM157"; } if (port == SerialCommPort.COM158) { return "COM158"; } if (port == SerialCommPort.COM159) { return "COM159"; } if (port == SerialCommPort.COM160) { return "COM160"; } if (port == SerialCommPort.COM161) { return "COM161"; } if (port == SerialCommPort.COM162) { return "COM162"; } if (port == SerialCommPort.COM163) { return "COM163"; } if (port == SerialCommPort.COM164) { return "COM164"; } if (port == SerialCommPort.COM165) { return "COM165"; } if (port == SerialCommPort.COM166) { return "COM166"; } if (port == SerialCommPort.COM167) { return "COM167"; } if (port == SerialCommPort.COM168) { return "COM168"; } if (port == SerialCommPort.COM169) { return "COM169"; } if (port == SerialCommPort.COM170) { return "COM170"; } if (port == SerialCommPort.COM181) { return "COM181"; } if (port == SerialCommPort.COM182) { return "COM182"; } if (port == SerialCommPort.COM183) { return "COM183"; } if (port == SerialCommPort.COM184) { return "COM184"; } if (port == SerialCommPort.COM185) { return "COM185"; } if (port == SerialCommPort.COM186) { return "COM186"; } if (port == SerialCommPort.COM187) { return "COM187"; } if (port == SerialCommPort.COM188) { return "COM188"; } if (port == SerialCommPort.COM189) { return "COM189"; } if (port == SerialCommPort.COM190) { return "COM190"; } if (port == SerialCommPort.COM191) { return "COM191"; } if (port == SerialCommPort.COM192) { return "COM192"; } if (port == SerialCommPort.COM193) { return "COM193"; } if (port == SerialCommPort.COM194) { return "COM194"; } if (port == SerialCommPort.COM95) { return "COM195"; } if (port == SerialCommPort.COM196) { return "COM196"; } if (port == SerialCommPort.COM197) { return "COM197"; } if (port == SerialCommPort.COM198) { return "COM198"; } if (port == SerialCommPort.COM199) { return "COM199"; } if (port == SerialCommPort.COM200) { return "COM200"; } if (port == SerialCommPort.COM201) { return "COM201"; } if (port == SerialCommPort.COM202) { return "COM202"; } if (port == SerialCommPort.COM203) { return "COM203"; } if (port == SerialCommPort.COM204) { return "COM204"; } if (port == SerialCommPort.COM205) { return "COM205"; } if (port == SerialCommPort.COM206) { return "COM206"; } if (port == SerialCommPort.COM207) { return "COM207"; } if (port == SerialCommPort.COM208) { return "COM208"; } if (port == SerialCommPort.COM209) { return "COM209"; } if (port == SerialCommPort.COM210) { return "COM210"; } if (port == SerialCommPort.COM211) { return "COM211"; } if (port == SerialCommPort.COM212) { return "COM212"; } if (port == SerialCommPort.COM213) { return "COM213"; } if (port == SerialCommPort.COM214) { return "COM214"; } if (port == SerialCommPort.COM215) { return "COM215"; } if (port == SerialCommPort.COM216) { return "COM216"; } if (port == SerialCommPort.COM217) { return "COM217"; } if (port == SerialCommPort.COM218) { return "COM218"; } if (port == SerialCommPort.COM219) { return "COM219"; } if (port == SerialCommPort.COM220) { return "COM220"; } if (port == SerialCommPort.COM221) { return "COM221"; } if (port == SerialCommPort.COM222) { return "COM222"; } if (port == SerialCommPort.COM223) { return "COM223"; } if (port == SerialCommPort.COM224) { return "COM224"; } if (port == SerialCommPort.COM225) { return "COM225"; } if (port == SerialCommPort.COM226) { return "COM226"; } if (port == SerialCommPort.COM227) { return "COM227"; } if (port == SerialCommPort.COM228) { return "COM228"; } if (port == SerialCommPort.COM229) { return "COM229"; } if (port == SerialCommPort.COM230) { return "COM230"; } if (port == SerialCommPort.COM231) { return "COM231"; } if (port == SerialCommPort.COM232) { return "COM232"; } if (port == SerialCommPort.COM233) { return "COM233"; } if (port == SerialCommPort.COM234) { return "COM234"; } if (port == SerialCommPort.COM235) { return "COM235"; } if (port == SerialCommPort.COM236) { return "COM236"; } if (port == SerialCommPort.COM237) { return "COM237"; } if (port == SerialCommPort.COM238) { return "COM238"; } if (port == SerialCommPort.COM239) { return "COM239"; } if (port == SerialCommPort.COM240) { return "COM240"; } if (port == SerialCommPort.COM241) { return "COM241"; } if (port == SerialCommPort.COM242) { return "COM242"; } if (port == SerialCommPort.COM243) { return "COM243"; } if (port == SerialCommPort.COM244) { return "COM244"; } if (port == SerialCommPort.COM245) { return "COM245"; } if (port == SerialCommPort.COM246) { return "COM246"; } if (port == SerialCommPort.COM247) { return "COM247"; } if (port == SerialCommPort.COM248) { return "COM248"; } if (port == SerialCommPort.COM249) { return "COM249"; } if (port == SerialCommPort.COM250) { return "COM250"; } if (port == SerialCommPort.COM251) { return "COM251"; } if (port == SerialCommPort.COM252) { return "COM252"; } if (port == SerialCommPort.COM253) { return "COM253"; } if (port == SerialCommPort.COM254) { return "COM254"; } if (port == SerialCommPort.COM255) { return "COM255"; } return string.Empty; }
public SerialPort() { comDevice = IntPtr.Zero; m_Port = SerialCommPort.COM02; m_BaudRate = SerialBaudRate.br004800; m_DataWidth = SerialDataWidth.dw8Bits; m_EnableDTROnOpen = true; m_EnableRTSOnOpen = true; Delay = 100; m_Priority = ThreadPriority.Normal; m_InputBuffer = 0x1000; m_OutputBuffer = 0x1000; m_ReadIntervalTimeout = -1; m_WriteTotalTimeoutMultiplier = 100; m_WriteTotalTimeoutConstant = 0x3e8; NativeMethods.timeBeginPeriod(1); }
public bool Open() { if (m_Port != m_ConnectedTo) { bool flag; if (m_ConnectedTo != SerialCommPort.None) Close(); try { comDevice = NativeMethods.CreateFile(@"\\.\COM" + ((uint)m_Port), 0xc0000000, 0, 0, 3, 0, 0); if (((int)comDevice) <= 0) return false; if (!SetDCBState(comDevice)) return false; if (!SetBuffers(comDevice, m_InputBuffer, m_OutputBuffer)) return false; SetTimeouts(comDevice); comThread = new Thread(ThreadJob) { Priority = m_Priority }; comThread.Start(); m_ConnectedTo = m_Port; if (NeedSynchronization) { lock (synchronizeVariable) { OnConnected(new ConnectionEventArgs(m_Port)); } } else OnConnected(new ConnectionEventArgs(m_Port)); flag = true; } catch { flag = false; } return flag; } return false; }
public static SerialCommPort[] GetExistingCommPorts() { try { RegistryKey key = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM"); string[] valueNames = key.GetValueNames(); SerialCommPort[] portArray = new SerialCommPort[valueNames.Length]; for (int i = 0; i < valueNames.Length; i++) { portArray[i] = StringToSerialCommPort(key.GetValue(valueNames[i]).ToString()); } return portArray; } catch { return null; } }
public SerialPort(SerialCommPort port, SerialDataWidth dataWidth, SerialStopBits stopBits, SerialParityBits parityBits, SerialHardwareFlowControl hwFlowControl, SerialSoftwareFlowControl swFlowControl, bool enableDTROnOpen, bool enableRTSOnOpen) : this(port,dataWidth,stopBits,parityBits,hwFlowControl,swFlowControl) { m_EnableDTROnOpen = enableDTROnOpen; m_EnableRTSOnOpen = enableRTSOnOpen; }
public void Close() { try { if (comDevice.ToInt32() <= 0) { return; } if (NeedSynchronization) { lock (synchronizeVariable) { OnDisconnected(new ConnectionEventArgs(m_ConnectedTo)); } } else OnDisconnected(new ConnectionEventArgs(m_ConnectedTo)); if (comDevice.ToInt32() > 0) { NativeMethods.PurgeComm(comDevice, 15); NativeMethods.CloseHandle(comDevice); comDevice = IntPtr.Zero; } m_ConnectedTo = SerialCommPort.None; if ((comThread != null) && comThread.IsAlive) { comThread.Abort(); } } catch { comDevice = IntPtr.Zero; m_ConnectedTo = SerialCommPort.None; } }
public SerialPort(SerialCommPort port, SerialDataWidth dataWidth, SerialStopBits stopBits, SerialParityBits parityBits, SerialHardwareFlowControl hwFlowControl, SerialSoftwareFlowControl swFlowControl) : this(port,dataWidth,stopBits,parityBits) { m_HwFlowControl = hwFlowControl; m_SwFlowControl = swFlowControl; }
public SerialPort(SerialCommPort port, SerialDataWidth dataWidth, SerialStopBits stopBits, SerialParityBits parityBits) : this(port) { m_DataWidth = dataWidth; m_StopBits = stopBits; m_ParityBits = parityBits; }
public SerialPort(SerialCommPort port) : this() { m_Port = port; }
public Config() { switch (ConfigurationManager.AppSettings["BaudRate"]) { case "115200": baudRate = SerialBaudRate.br115200; break; case "9600": baudRate = SerialBaudRate.br009600; break; case "14400": baudRate = SerialBaudRate.br014400; break; case "19200": baudRate = SerialBaudRate.br019200; break; case "38400": baudRate = SerialBaudRate.br038400; break; case "57600": baudRate = SerialBaudRate.br057600; break; case "128000": baudRate = SerialBaudRate.br128000; break; case "7200": baudRate = SerialBaudRate.br007200; break; case "4800": baudRate = SerialBaudRate.br004800; break; case "2400": baudRate = SerialBaudRate.br002400; break; case "1800": baudRate = SerialBaudRate.br001800; break; case "1200": baudRate = SerialBaudRate.br001200; break; case "600": baudRate = SerialBaudRate.br000600; break; default: baudRate = SerialBaudRate.br000300; break; } switch (Int32.Parse(ConfigurationManager.AppSettings["DataWidth"])) { case 8: dataWidth = SerialDataWidth.dw8Bits; break; case 7: dataWidth = SerialDataWidth.dw7Bits; break; case 6: dataWidth = SerialDataWidth.dw6Bits; break; default: dataWidth = SerialDataWidth.dw5Bits; break; } switch (Int32.Parse(ConfigurationManager.AppSettings["StopBits"])) { case 1: stopBits = SerialStopBits.sb1Bit; break; case 2: stopBits = SerialStopBits.sb2Bits; break; default: stopBits = SerialStopBits.sb1_5Bits; break; } switch (ConfigurationManager.AppSettings["ParityBits"].ToUpper()) { case "NONE": parityBits = SerialParityBits.pbNone; break; case "EVEN": parityBits = SerialParityBits.pbEven; break; case "ODD": parityBits = SerialParityBits.pbOdd; break; case "SPACE": parityBits = SerialParityBits.pbSpace; break; default: parityBits = SerialParityBits.pbMark; break; } port = SerialPort.StringToSerialCommPort(ConfigurationManager.AppSettings["Port"].ToUpper()); }
public ConnectionEventArgs(SerialCommPort port) { this.m_Port = port; }