/// <summary> /// 写入串口数据 /// </summary> /// <param name="writeBytes"></param> /// <param name="visaAddress"></param> /// <returns></returns> public static ErrMsg WriteData(byte[] writeBytes, string visaAddress) { ErrMsg ret = new ErrMsg(); ret.ErrorCode = -1; ret.Result = true; ret.Msg = ""; try { if (serialSession == null || serialSession.ResourceName != visaAddress) { serialSession = (SerialSession)ResourceManager.GetLocalManager().Open(visaAddress, AccessModes.NoLock, 0); } ret = SetSerial(serialSession, ret); ret = FlushIO(serialSession, ret); ret = SetIOSize(serialSession, ret); ret = WriteIO(serialSession, writeBytes, ret); } catch (Exception e) { ret.ErrorCode = e.HResult; ret.Msg = e.Message; ret.Result = false; } //serialSession.Dispose(); return(ret); }
/// <summary> /// 设置串口visa参数 /// </summary> /// <param name="serialSession"></param> /// <param name="inErrMsg"></param> /// <returns></returns> private static ErrMsg SetSerial(SerialSession serialSession, ErrMsg inErrMsg) { if (!inErrMsg.Result) { return(inErrMsg); } ErrMsg ret = new ErrMsg(); ret.ErrorCode = -1; ret.Result = true; ret.Msg = ""; try { serialSession.Timeout = 1000; serialSession.BaudRate = 115200; serialSession.DataBits = 8; serialSession.StopBits = StopBitType.One; serialSession.FlowControl = FlowControlTypes.None; serialSession.Parity = Parity.None; serialSession.TerminationCharacter = 0xA; serialSession.TerminationCharacterEnabled = true; } catch (Exception e) { ret.ErrorCode = 010010; ret.Result = false; ret.Msg = e.Message + e.Source + e.StackTrace; } return(ret); }
public static void SetSerialAttribute(ref MessageBasedSession mbs) { SerialSession ser = (SerialSession)mbs; ser.BaudRate = GlobalVars.VISA_CLI_Option_SerialBaudRate; //设置速率 ser.DataBits = GlobalVars.VISA_CLI_Option_SerialDataBits; //数据位 ser.StopBits = GlobalVars.VISA_CLI_Option_SerialStopBits; //停止位 ser.Parity = GlobalVars.VISA_CLI_Option_SerialParity; //校验 NONE 0 Odd 1 Even 2 Mark 3 Space 4 ser.FlowControl = GlobalVars.VISA_CLI_Option_SerialFlowControl; //Flow Control NONE 0 XON/XOFF 1 使用 NI I/O Trace 监视 VISA Test Panel 设置时得到 ser.TerminationCharacter = GlobalVars.theReadTerminationCharactersOfRS232; //结束符,针对每个串口只能设置唯一的结束符,此处留给 Read操作,Write操作直接手动在命令末尾加指定的结束符 ser.ReadTermination = GlobalVars.VISA_CLI_Option_SerialTerminationMethodWhenRead; // 读回结束符选择 //VI_ATTR_ASRL_END_IN indicates the method used to terminate read operations ser.WriteTermination = GlobalVars.VISA_CLI_Option_SerialTerminationMethodWhenWrite; // 写入结束符选择(ser.TerminationCharacter 定义的终止符优先给读回终止符, 写入终止符由 --stmW=2 和 --terminateW="0x0A" 共同指定,若--terminateW不指定,默认使用0x0aA) // VI_ATTR_ASRL_END_OUT indicates the method used to terminate write operations if (GlobalVars.VISA_CLI_Option_PrintDebugMessage) { Console.WriteLine("当前正使用COM通信,当前设置为:\n速率 : " + GlobalVars.VISA_CLI_Option_SerialBaudRate.ToString() + " bps" + "\n数据位 : " + GlobalVars.VISA_CLI_Option_SerialDataBits.ToString() + "\n停止位 : " + (((int)GlobalVars.VISA_CLI_Option_SerialStopBits) / 10).ToString() + "\n校验方式(Parity) : " + GlobalVars.VISA_CLI_Option_SerialParityEnumList[((int)GlobalVars.VISA_CLI_Option_SerialParity)] + "\nFlowControl : " + GlobalVars.VISA_CLI_Option_SerialFlowControlEnumList[((int)GlobalVars.VISA_CLI_Option_SerialFlowControl)] + "\nTerminationCharacter : 0x" + GlobalVars.theReadTerminationCharactersOfRS232.ToString("X2") //https://stackoverflow.com/questions/5426582/turn-byte-into-two-digit-hexadecimal-number-just-using-tostring/5426587#5426587 + "\nTerminationCharactersOfRead : 0x" + GlobalVars.theReadTerminationCharactersOfRS232.ToString("X2") + "\nTerminationCharactersOfWrite : 0x" + GlobalVars.theWriteTerminationCharactersOfRS232.ToString("X2") + "\nSerialTerminationMethodOfRead : " + GlobalVars.VISA_CLI_Option_SerialTerminationMethodEnumList[((int)GlobalVars.VISA_CLI_Option_SerialTerminationMethodWhenRead)] + "\nSerialTerminationMethodOfWrite : " + GlobalVars.VISA_CLI_Option_SerialTerminationMethodEnumList[((int)GlobalVars.VISA_CLI_Option_SerialTerminationMethodWhenWrite)] + "\n\n请确保仪器设置与本设置相符" ); } }
/// <summary> /// 写入串口数据 /// </summary> /// <param name="serialSession"></param> /// <param name="writeBytes"></param> /// <param name="inErrMsg"></param> /// <returns></returns> private static ErrMsg WriteIO(SerialSession serialSession, byte[] writeBytes, ErrMsg inErrMsg) { if (!inErrMsg.Result) { return(inErrMsg); } ErrMsg ret = new ErrMsg(); ret.ErrorCode = -1; ret.Result = true; ret.Msg = ""; try { serialSession.Write(writeBytes); } catch (Exception e) { ret.ErrorCode = 010004; ret.Result = false; ret.Msg = e.Message; } return(ret); }
/// <summary> /// 清空IO缓冲区 /// </summary> /// <param name="serialSession"></param> /// <param name="inErrMsg"></param> /// <returns></returns> private static ErrMsg FlushIO(SerialSession serialSession, ErrMsg inErrMsg) { if (!inErrMsg.Result) { return(inErrMsg); } ErrMsg ret = new ErrMsg(); ret.ErrorCode = -1; ret.Result = true; ret.Msg = ""; try { serialSession.Flush(BufferTypes.InBuffer, true); } catch (Exception e) { ret.ErrorCode = 010002; ret.Result = false; ret.Msg = e.Message; } return(ret); }
public override void Connect() { serial = new SerialSession(address); serial.BaudRate = 9600; serial.DataBits = 8; serial.StopBits = StopBitType.One; serial.ReadTermination = SerialTerminationMethod.LastBit; }
public void Connect() { serial = new SerialSession(physicalAddress); serial.BaudRate = 115200; serial.DataBits = 8; serial.StopBits = SerialStopBitsMode.One; serial.ReadTermination = SerialTerminationMethod.HighestBit; serial.Parity = SerialParity.None; }
private void Connect() { if (!Environs.Debug) { serial = new SerialSession(address); serial.BaudRate = 9600; serial.DataBits = 8; serial.StopBits = StopBitType.One; serial.ReadTermination = SerialTerminationMethod.LastBit; } connected = true; }
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 override void Connect() { if (!Environs.Debug) { if (!Environs.Debug) { serial = new SerialSession(address); serial.BaudRate = 4800; serial.DataBits = 7; serial.StopBits = SerialStopBitsMode.One; serial.Parity = SerialParity.Even; serial.FlowControl = SerialFlowControlModes.XOnXOff; serial.ReadTermination = SerialTerminationMethod.HighestBit; serial.TerminationCharacter = TerminationCharacter; } connected = true; } }
/// <summary> /// This task will use the MessageBasedSession passed in. This task can either the MessageBasedSession or leave it open for the caller to close. /// </summary> /// <param name="session">MessageBasedSession used by this task.</param> /// <param name="taskHandlesSessionLifetime">If true, the task will close session when the task is disposed. If false, the caller is responsible for closing session.</param> public BK2831E_ReadVoltage(MessageBasedSession session, bool taskHandlesSessionLifetime) { if (session == null) { throw new ArgumentNullException("session"); } _instrumentSession = session; _instrumentSession.Timeout = 2000; SerialSession ss = (SerialSession)_instrumentSession; ss.ReadTermination = SerialTerminationMethod.TerminationCharacter; _instrumentSession.TerminationCharacterEnabled = true; _instrumentSession.TerminationCharacter = 10; // The caller can control the VISA session lifetime by passing in false for taskHandlesSessionLifetime. If taskHandlesSessionLifetime // is true, then the VISA session will be closed when the caller disposes this task. _handleSessionLifetime = taskHandlesSessionLifetime; // The MessageBasedSessionReader is used to read and parse data returned from the instrument _reader = new MessageBasedSessionReader(_instrumentSession); }
/// <summary> /// This task will use the MessageBasedSession passed in. This task can either the MessageBasedSession or leave it open for the caller to close. /// </summary> /// <param name="session">MessageBasedSession used by this task.</param> /// <param name="taskHandlesSessionLifetime">If true, the task will close session when the task is disposed. If false, the caller is responsible for closing session.</param> public SetChannel_B(MessageBasedSession session, bool taskHandlesSessionLifetime) { if (session == null) { throw new ArgumentNullException("session"); } _instrumentSession = session; _instrumentSession.Timeout = 2000; SerialSession ss = (SerialSession)_instrumentSession; ss.ReadTermination = SerialTerminationMethod.TerminationCharacter; _instrumentSession.TerminationCharacterEnabled = true; _instrumentSession.TerminationCharacter = 10; // The caller can control the VISA session lifetime by passing in false for taskHandlesSessionLifetime. If taskHandlesSessionLifetime // is true, then the VISA session will be closed when the caller disposes this task. _handleSessionLifetime = taskHandlesSessionLifetime; // The MessageBasedSessionWriter is used to write formatted data to the instrument _writer = new MessageBasedSessionWriter(_instrumentSession); }
/// <summary> /// 设置IO缓冲区 /// </summary> /// <param name="serialSession"></param> /// <param name="inErrMsg"></param> /// <returns></returns> private static ErrMsg SetIOSize(SerialSession serialSession, ErrMsg inErrMsg) { if (!inErrMsg.Result) { return(inErrMsg); } ErrMsg ret = new ErrMsg(); ret.ErrorCode = -1; ret.Result = true; ret.Msg = ""; try { serialSession.SetBufferSize(BufferTypes.InBuffer, 4096); } catch (Exception e) { ret.ErrorCode = 010003; ret.Result = false; ret.Msg = e.Message; } return(ret); }
private Instruments() { alignmentPowerCalibration = Math.Pow(10, powerAttenuation / 10); try { aerotechController = Controller.Connect(); } catch (A3200Exception ex) { Console.WriteLine("Error connecting to controller"); } aerotechController.Commands.Axes["Y"].Motion.Enable(); aerotechController.Commands.Axes["X"].Motion.Enable(); aerotechController.Commands.Axes["Z"].Motion.Enable(); using (var rm = new ResourceManager()) { string myResource = ""; try { IEnumerable <string> resources = rm.Find("ASRL?*INSTR"); foreach (string s in resources) { Debug.WriteLine("Found resource", s); myResource = s; } } catch (Exception ex) { Debug.WriteLine("Couldn't find Arroyo"); } SerialSession session = (SerialSession)rm.Open(myResource); session.BaudRate = 38400; session.RawIO.Write("*IDN?\n"); string r = session.RawIO.ReadString(); Debug.WriteLine(r); arroyo = session; try { IEnumerable <String> resources = rm.Find("USB?*"); foreach (string s in resources) { Debug.WriteLine(s); myResource = s; } } catch (Exception ex) { Debug.WriteLine("Couldn't find thorlabs"); } UsbSession session2 = (UsbSession)rm.Open(myResource); session2.SendEndEnabled = true; session2.TerminationCharacterEnabled = false; session2.RawIO.Write("*IDN?\n"); var t = session2.ReadStatusByte(); r = session2.RawIO.ReadString(); session2.RawIO.Write($"SENS:CORR {powerAttenuation}\n"); powerMeter = session2; } }