/// <summary> /// Defined as extension method for easier migration, since this is the most used form of Open in SharpPcap 5.x /// </summary> /// <param name="device"></param> /// <param name="mode"></param> /// <param name="read_timeout"></param> public static void Open(this IPcapDevice device, DeviceModes mode = DeviceModes.None, int read_timeout = 1000) { var configuration = new DeviceConfiguration() { Mode = mode, ReadTimeout = read_timeout, }; device.Open(configuration); }
/// <summary> /// read the current state of the stage /// include IsHomed, MaxSteps, AbsPosition /// </summary> private void GetCurrentState() { // read home state DeviceModes mode = (DeviceModes)ZaberConversation.Request(Command.ReturnSetting, (int)Command.SetDeviceMode).Data; this.IsHomed = ((mode & DeviceModes.HomeStatus) == DeviceModes.HomeStatus); // read current position if (int.TryParse(ZaberConversation.Request(Command.ReturnSetting, (int)Command.SetCurrentPosition).Data.ToString(), out int pos)) { this.AbsPosition = pos; } else { this.AbsPosition = -1; } // read max position if (int.TryParse(ZaberConversation.Request(Command.ReturnSetting, (int)Command.SetMaximumPosition).Data.ToString(), out int max_steps)) { this.SCWL = max_steps; this.UnitHelper.ChangeMaxSteps(max_steps); // re-init the unit helper due to the max-steps is read from the hardware controller // var prev = this.UnitHelper.Clone() as RealworldDistanceUnitHelper; // this.UnitHelper = new RealworldDistanceUnitHelper(max_steps, prev.MaxStroke, prev.Unit, prev.Digits); LogHelper.WriteLine("{0} Max Steps was read from flash, the new value is {1}", this, max_steps, LogHelper.LogType.NORMAL); } else { // keep the value set in the config file } // read maximum speed if (int.TryParse(ZaberConversation.Request(Command.ReturnSetting, (int)Command.SetTargetSpeed).Data.ToString(), out int target_speed)) { this.MaxSpeed = target_speed; LogHelper.WriteLine("{0} Max Speed was read from flash, the new value is {1}", this, target_speed, LogHelper.LogType.NORMAL); } else { // keep the value set in the config file } }
void Awake() { // Attach communication components. receiver = gameObject.AddComponent <Receiver>(); receiver.DataReceived += (source, message) => OnDataReceivedFromNetwork(message); arduino = gameObject.AddComponent <Arduino>(); arduino.DataReceived += (source, data) => { OnDataReceivedFromArduino(data); }; // Setup UI components. menuCanvas = GameObject.Find("MenuCanvas").GetComponent <Canvas>(); intertrialImage = GameObject.Find("Blank").GetComponent <RawImage>(); consoleText = GameObject.Find("Console").GetComponent <Text>(); InputField startPositionInput = GameObject.Find("StartPosition").GetComponent <InputField>(); startPosition = PlayerPrefs.GetFloat(startPositionInput.name, startPosition); SetupCombo(GameObject.Find("SetStartPosition").GetComponent <Button>(), startPositionInput, startPosition.ToString(), TestFloat, () => { startPosition = float.Parse(startPositionInput.text); PlayerPrefs.SetFloat(startPositionInput.name, startPosition); PlayerPrefs.Save(); }, () => transform.position = new Vector3(transform.position.x, transform.position.y, startPosition) ); InputField finishPositionInput = GameObject.Find("FinishPosition").GetComponent <InputField>(); finishPosition = PlayerPrefs.GetFloat(finishPositionInput.name, finishPosition); SetupCombo(GameObject.Find("SetFinishPosition").GetComponent <Button>(), finishPositionInput, finishPosition.ToString(), TestFloat, () => { finishPosition = float.Parse(finishPositionInput.text); PlayerPrefs.SetFloat(finishPositionInput.name, finishPosition); PlayerPrefs.Save(); }, () => transform.position = new Vector3(transform.position.x, transform.position.y, finishPosition) ); InputField intertrialDurationInput = GameObject.Find("IntertrialDuration").GetComponent <InputField>(); intertrialDuration = PlayerPrefs.GetFloat(intertrialDurationInput.name, intertrialDuration); SetupCombo(GameObject.Find("SetIntertrialDuration").GetComponent <Button>(), intertrialDurationInput, intertrialDuration.ToString(), TestPositiveFloat, () => { intertrialDuration = float.Parse(intertrialDurationInput.text); PlayerPrefs.SetFloat(intertrialDurationInput.name, intertrialDuration); PlayerPrefs.Save(); }, () => StartIntertrial() ); InputField wheelRadiusInput = GameObject.Find("WheelRadius").GetComponent <InputField>(); wheelRadius = PlayerPrefs.GetFloat(wheelRadiusInput.name, wheelRadius); SetupCombo(GameObject.Find("SetWheelRadius").GetComponent <Button>(), wheelRadiusInput, wheelRadius.ToString(), TestFloat, () => { wheelRadius = float.Parse(wheelRadiusInput.text); wheelFactor = 2 * Mathf.PI / wheelSteps * wheelRadius; PlayerPrefs.SetFloat(wheelRadiusInput.name, wheelRadius); PlayerPrefs.Save(); } ); InputField wheelStepsInput = GameObject.Find("WheelSteps").GetComponent <InputField>(); wheelSteps = (int)PlayerPrefs.GetFloat(wheelStepsInput.name, wheelSteps); SetupCombo(GameObject.Find("SetWheelSteps").GetComponent <Button>(), wheelStepsInput, wheelSteps.ToString(), TestInt, () => { wheelSteps = int.Parse(wheelStepsInput.text); wheelFactor = 2 * Mathf.PI / wheelSteps * wheelRadius; PlayerPrefs.SetFloat(wheelStepsInput.name, wheelSteps); PlayerPrefs.Save(); } ); InputField serialNameInput = GameObject.Find("SerialName").GetComponent <InputField>(); Button serialNameButton = GameObject.Find("SetSerialName").GetComponent <Button>(); Text serialNameButtonText = serialNameButton.GetComponentInChildren <Text>(); serialNameButtonText.text = "Connect"; serialName = PlayerPrefs.GetString(serialNameInput.name, serialName); serialNameInput.text = serialName; bool serialConnected = false; serialNameInput.onValidateInput += delegate(string input, int charIndex, char addedChar) { if (serialConnected) { serialNameButton.interactable = true; serialConnected = false; arduino.Stop(); } serialNameButtonText.text = "Connect"; return(addedChar); }; serialNameButton.onClick.AddListener( delegate { if (serialConnected) { serialConnected = false; serialNameButtonText.text = "Connect"; arduino.Stop(); } else { serialNameButton.interactable = false; arduino.Setup(serialNameInput.text, baudrate, (connected, message) => { serialNameButton.interactable = true; if (connected) { serialConnected = true; serialNameButtonText.text = "Disconnect"; } else { Log(string.Format("Unable to connect to '{0}': {1}", serialNameInput.text, message.Trim())); } } ); // Always save regarless of connection outcome. serialName = serialNameInput.text; PlayerPrefs.SetString(serialNameInput.name, serialName); PlayerPrefs.Save(); } } ); InputField ipsInput = GameObject.Find("IPs").GetComponent <InputField>(); Button ipsButton = GameObject.Find("SetIPs").GetComponent <Button>(); Text ipsButtonText = ipsButton.GetComponentInChildren <Text>(); ipsButtonText.text = "Connect"; ips = PlayerPrefs.GetString(ipsInput.name, ips); ipsInput.text = ips; bool senderConnected = false; ipsInput.onValidateInput += delegate(string input, int charIndex, char addedChar) { if (senderConnected) { senderConnected = false; sender.Stop(); } ipsButtonText.text = "Connect"; return(addedChar); }; ipsButton.onClick.AddListener( delegate { if (senderConnected) { senderConnected = false; ipsButtonText.text = "Connect"; sender.Stop(); } else { bool success = true; string[] ipArray = Regex.Split(ipsInput.text, @"[\s,]+"); foreach (string ip in ipArray) { if (ip.Trim() != String.Empty && !Sender.Validate(ip)) { Log(string.Format("'{0}' is not a valid IP address.", ip)); success = false; } } if (success) { senderConnected = true; ipsButtonText.text = "Disconnect"; ips = ipsInput.text; PlayerPrefs.SetString(ipsInput.name, ips); PlayerPrefs.Save(); sender.Setup(ipArray, port); } } } ); viewAngleText = GameObject.Find("ViewAngle").GetComponentInChildren <Text>(); Button viewAngleButton = GameObject.Find("SetViewAngle").GetComponent <Button>(); viewAngleIndex = PlayerPrefs.GetInt("ViewAngleIndex", viewAngleIndex); ViewAngle = viewAngles[viewAngleIndex]; // Rotate views. viewAngleButton.onClick.AddListener( () => { viewAngleIndex = (viewAngleIndex + 1) % viewAngles.Length; ViewAngle = viewAngles[viewAngleIndex]; PlayerPrefs.SetInt("ViewAngleIndex", viewAngleIndex); PlayerPrefs.Save(); } ); deviceModeText = GameObject.Find("DeviceMode").GetComponentInChildren <Text>(); Button deviceModeButton = GameObject.Find("SetDeviceMode").GetComponent <Button>(); deviceMode = (DeviceModes)Enum.Parse(typeof(DeviceModes), PlayerPrefs.GetString("DeviceMode", deviceMode.ToString())); DeviceMode = deviceMode; // Alternate device modes. deviceModeButton.onClick.AddListener( () => { switch (DeviceMode) { case DeviceModes.Control: DeviceMode = DeviceModes.Monitor; deviceModeText.text = "Monitor"; break; case DeviceModes.Monitor: DeviceMode = DeviceModes.Control; deviceModeText.text = "Control"; break; } PlayerPrefs.SetString("DeviceMode", DeviceMode.ToString()); PlayerPrefs.Save(); } ); wheelFactor = 2 * Mathf.PI / wheelSteps * wheelRadius; transform.position = new Vector3(transform.position.x, transform.position.y, startPosition); if (!receiver.Setup(port)) { Log(string.Format("Port {0} is unavailable in this device and remote data won't reach this device.", port)); } string filename = System.IO.Path.Combine(outputFolder, string.Format("VR{0}.csv", System.DateTime.Now.ToString("yyyyMMddHHmmss"))); logger = new Logger(filename); Log("Version: " + version); Log("About: [email protected]"); Log("Filename: " + filename); }