protected void Connect(SerialTerminationMethod method) { if (!Environs.Debug) { if (!Environs.Debug) { serial = new SerialSession(address); serial.BaudRate = 9600; serial.DataBits = 8; serial.StopBits = StopBitType.One; serial.ReadTermination = method; } connected = true; } }
protected void Connect(SerialTerminationMethod method) { if (!Environs.Debug) { if (!Environs.Debug) { serial = new SerialSession(address); serial.BaudRate = BaudRate; serial.DataBits = DataBits; serial.StopBits = StopBit; serial.Parity = ParitySetting; serial.FlowControl = FlowControl; serial.ReadTermination = method; serial.TerminationCharacter = TerminationCharacter; } connected = true; } }
public enum Mode : short { GPIB = 1, SERIAL, USBRAW, USBTMC, TCPIP } //https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum public static void ParseArgs(string[] args) //https://mail.gnome.org/archives/commits-list/2012-December/msg00139.html { bool showHelp = false; var p = new OptionSet() { // //GPIB related { "G|useGPIB", "GPIB mode", v => GlobalVars.VISA_CLI_Option_CurrentMode = (short)Mode.GPIB }, { "gpib|gpibBoardIndex=", "GPIB board index(Default 0)", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_GPIB_BoardIndex) }, //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number { "pad|PrimaryAddress=", "primary address", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_GPIB_PrimaryAddress) }, { "sad|SecondaryAddress=", "secondary address", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_GPIB_SecondaryAddress) }, //COM related { "S|useSERIAL", "SERIAL mode", v => GlobalVars.VISA_CLI_Option_CurrentMode = (short)Mode.SERIAL }, { "port|SerialPortNumber=", "Serial Port Number", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_Serial_PortNumber) }, { "baud|BaudRate=", "Baud of Serial Port(Default 19200)", v => int.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialBaudRate) }, { "data|DataBits=", "Data bits (Default 8)", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialDataBits) }, { "stop|StopBits=", "Stop bits (Default 10)", v => StopBitType.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialStopBits) }, { "parity|SerialParity=", "Serial Parity: NONE 0 Odd 1 Even 2 Mark 3 Space 4 (Default NONE)", v => Parity.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialParity) }, { "flow|FlowControlTypes=", "Flow Control Types: NONE 0 XON/XOFF 1 (Default NONE)", v => FlowControlTypes.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialFlowControl) }, { "stmR|SerialTerminationMethodWhenRead=", "Serial Termination Method When Read : None 0 LastBit 1 TerminationCharacter 2 Break 3 (Default TerminationCharacter)", v => SerialTerminationMethod.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialTerminationMethodWhenRead) }, { "stmW|SerialTerminationMethodWhenWrite=", "Serial Termination Method When Write : None 0 LastBit 1 TerminationCharacter 2 Break 3 (Default NONE)", v => SerialTerminationMethod.TryParse(v, out GlobalVars.VISA_CLI_Option_SerialTerminationMethodWhenWrite) }, //{ "terminateW|TerminationCharactersOfWrite=", "Termination Characters of Serial When Write (Default 0x0A(\"\\n\"))", v => GlobalVars.theWriteTerminationCharactersOfRS232 = Convert.ToByte(v,16) }, // --terminateW "" 时会产生异常,用 Byte.TryParse() //{ "terminateR|TerminationCharactersOfRead=", "Termination Characters of Serial When Read (Default 0x0A(\"\\n\"))", v => GlobalVars.theReadTerminationCharactersOfRS232 = Convert.ToByte(v,16) }, { "terminateW|TerminationCharactersOfWrite=", "Termination Characters of Serial When Write (Default 0x0A(\"\\n\"))", v => Byte.TryParse(Regex.Replace(v, @"0[x,X]", ""), NumberStyles.HexNumber, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.theWriteTerminationCharactersOfRS232) }, //https://stackoverflow.com/questions/2801509/uint32-tryparse-hex-number-not-working/3570612#3570612 https://stackoverflow.com/questions/16117043/regular-expression-replace-in-c-sharp/16117150#16117150 { "terminateR|TerminationCharactersOfRead=", "Termination Characters of Serial When Read (Default 0x0A(\"\\n\"))", v => Byte.TryParse(Regex.Replace(v, @"0[x,X]", ""), NumberStyles.HexNumber, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.theReadTerminationCharactersOfRS232) }, //USBTMC USB0::0x0699::0x0415::C022855::INSTR 少了等号=导致出现 解析为 USB0::0xvid::0xpid::sn::INSTR { "U|useUSBTMC", "USBTMC mode", v => GlobalVars.VISA_CLI_Option_CurrentMode = (short)Mode.USBTMC }, { "usb|usbBoardIndex=", "USB board index(Default 0)", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_USB_BoardIndex) }, { "vid|usbVID=", "USB Vendor ID", v => GlobalVars.VISA_CLI_Option_USB_VID = v }, { "pid|usbPID=", "USB Model ID", v => GlobalVars.VISA_CLI_Option_USB_PID = v }, { "sn|usbSerialNumber=", "USB Serial Number", v => GlobalVars.VISA_CLI_Option_USB_SerialNumber = v }, { "raw|USBRAW", "USB Raw Mode", v => GlobalVars.VISA_CLI_Option_USB_Raw = v != null }, //TCPIP TCPIP0::192.168.1.2::inst0::INSTR { "T|useTCPIP", "TCPIP mode", v => GlobalVars.VISA_CLI_Option_CurrentMode = (short)Mode.TCPIP }, { "tcpip|tcpipAdapterBoardIndex=", "TCPIP Adapter board index(Default 0)", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_TCPIP_BoardIndex) }, { "ip|ipAddress=", "IP Address or hostname of the device", v => GlobalVars.VISA_CLI_Option_TCPIP_IPAddressOrHostName = v }, { "inst|instNumber=", "LAN Device Name :inst number ,(Default 0)", v => short.TryParse(v, out GlobalVars.VISA_CLI_Option_TCPIP_instNumber) }, //Common { "C|cmdstr|CommandString=", "command(s) to send to the device", v => GlobalVars.VISA_CLI_Option_CommandString = v }, { "W|write|JustWriteCommand", "just write (default)", v => GlobalVars.VISA_CLI_Option_JustWriteCommand = v != null }, { "R|read|JustReadBack", "just read back", v => GlobalVars.VISA_CLI_Option_JustReadBack = v != null }, { "Q|query|QueryCommand", "the command is a query command", v => GlobalVars.VISA_CLI_Option_isQueryCommand = v != null }, { "D|debug|PrintDebugMessage", "prints debug messages", v => GlobalVars.VISA_CLI_Option_PrintDebugMessage = v != null }, { "F|save2file|FileName=", "save the response binary data to specify file", v => GlobalVars.VISA_CLI_Option_FileNameOriginal = v }, { "O|overwrite|OverwriteFile", "if file exist ,overwrite it", v => GlobalVars.VISA_CLI_Option_OverwriteFile = v != null }, { "N|rBytes|ReadBackNbytes=", "how many bytes should be read back", v => Decimal.TryParse(v, NumberStyles.Any, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.VISA_CLI_Option_ReadBackNbytes) }, { "E|skip|SkipFirstNbytes=", "skip first n bytes of received data", v => Decimal.TryParse(v, NumberStyles.Any, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.VISA_CLI_Option_SkipFirstNbytes) }, { "L|ls|ListAllInstruments", "List All Instruments on interface", v => GlobalVars.VISA_CLI_Option_ListInstruments = v != null }, { "X|dcl|DeviceClear", "Send Device Clear before commands send ", v => GlobalVars.VISA_CLI_Option_isDeviceClearSend = v != null }, { "I|InteractiveMode", "Interactive Mode ", v => GlobalVars.VISA_CLI_Option_isInteractiveMode = v != null }, { "t|timeout=", "Timeout milliseconds (Default 10000ms) ", v => Decimal.TryParse(v, NumberStyles.Any, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.VISASessionTimeout) }, { "v|visa|VisaResourceName=", "VISA Resource Name, if this filed specified, Mode and model related parameters should be omitted", v => GlobalVars.VISAResourceName = v }, { "m|mix|MixMode", "Support Mix string input, For example string '0x39\\37\\x398' will be prase as string '9798'.\\r \\n \\t... also will be treat as Carriage Return / Line Feed / Tab ... \nThe priority of this switch is the highest, if both --MixMode and --HexInputMode specified, string '0x39\\37\\x398' will be prase as string '9798' at first ,then it will be treat as hex string and prase as string 'ùÿ'(ASCII table 0x97 0x98) finally", v => GlobalVars.VISA_CLI_Option_isMixMode = v != null }, { "i|hi|Hi|HexInputMode", "Treat argument of --CommandString as hexadecimal, please see option --MixMode for detail", v => GlobalVars.VISA_CLI_Option_isInputModeHex = v != null }, { "o|ho|Ho|HexOutputMode", "Format output as hexadecimal string,this function ONLY applied on the standard output, when save to file,data will always be saved as raw binary", v => GlobalVars.VISA_CLI_Option_isOutputModeHex = v != null }, { "c|clear|ClearConsole", "clear the console before each operation", v => GlobalVars.VISA_CLI_Option_isClearConsole = v != null }, { "l|cycle|LoopCycle=", "The cycle of loop mode (Default : 1 cycle ,operate once)", v => Decimal.TryParse(v, NumberStyles.Any, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.VISA_CLI_Option_CycleOfLoopMode) }, { "delay|DelayTime=", "The delay time(milliseconds) in loop mode(Default 0 ms)", v => Decimal.TryParse(v, NumberStyles.Any, /*CultureInfo.CurrentCulture*/ null, out GlobalVars.VISA_CLI_Option_DelayTimeOfLoopMode_ms) }, { "h|?|help", "show this message and exit.", v => showHelp = v != null }, }; try { p.Parse(args); } catch (OptionException e) { WriteExceptionMessageToConsole(System.Diagnostics.Process.GetCurrentProcess().ProcessName + e.Message + "\n" + "Try '" + System.AppDomain.CurrentDomain.FriendlyName + "--help' for more information."); } if (showHelp) { Console.WriteLine("Usage: {0} -G|-S [MODE related options] -C \"command string\"", System.AppDomain.CurrentDomain.FriendlyName);// System.Diagnostics.Process.GetCurrentProcess().ProcessName); //https://stackoverflow.com/questions/37459509/how-to-get-the-exe-name-while-the-program-is-running/37459553#37459553 Console.WriteLine(); Console.WriteLine("Options:"); p.WriteOptionDescriptions(Console.Out); System.Environment.Exit(-1); //https://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application } }