private void tmr_Tick(object sender, EventArgs e) { try { state = joystik.GetCurrentState(); curJoyState.x = state.X; curJoyState.y = state.Y; curJoyState.buttons[0] = state.Buttons[0]; curJoyState.buttons[1] = state.Buttons[1]; curJoyState.buttons[2] = state.Buttons[2]; curJoyState.buttons[3] = state.Buttons[3]; curJoyState.buttons[4] = state.Buttons[4]; if (myJoystickStateReceived != null) { myJoystickStateReceived(curJoyState); } } catch (Exception ex) { if (msgAppeared != null) { if (ex is SharpDX.SharpDXException) { msgAppeared(String.Format("{0}", ((SharpDX.SharpDXException)ex).Descriptor.Description, periodInit)); } else { msgAppeared(ex.Message); } currentTry++; if (!InitJoystick()) { tmr.Period = periodInit; if (currentTry >= cntTry) { msgAppeared(String.Format("Подключите джойстик и перезагрузите программу")); tmr.Stop(); } else { msgAppeared(String.Format("Попытка #{0} переподключения через {1} мс.", currentTry, periodInit)); } } else { tmr.Period = periodWorking; currentTry = 0; } } } }
private void Start_Click(object sender, RoutedEventArgs e) { int bpm = Int16.Parse(textBox.Text); tik = new SoundPlayer(Properties.Resources.tik); tok = new SoundPlayer(Properties.Resources.tok); if (!isPlaying) { _time.Period = (int)(1000 * (60f / bpm)); _time.Start(); count = 0; _time.Tick += _timer_Tick; isPlaying = true; Start.Content = "Stop"; } else { _time.Tick -= _timer_Tick; _time.Stop(); count = 0; isPlaying = false; Start.Content = "Start"; for (int i = 0; i < timesignature; i++) { blacks[i].Visibility = Visibility.Visible; greys[i].Visibility = Visibility.Hidden; } } }
public void Close() { if (tmr.IsRunning) { tmr.Stop(); } if (joystik == null) { return; } joystik.Unacquire(); }
//прием данных по тику таймера обмена 2 мс void timer_Tick(object sender, EventArgs e) { return;//not use data receive/processing on the timer tick since we are using uart.receive event if (!active) { timer.Stop(); uart.DiscardInBuffer(); return; } if (!uart.IsOpen) { timer.Stop(); uart.DiscardInBuffer(); return; } if (state_rx == STATE_RX.START) { if (uart.BytesToRead >= 1) { command_in.START = (byte)uart.ReadByte(); if (command_in.START == 0x5a) { state_rx = STATE_RX.ADDRESS; } else { //неправильный стартовый байт command_in.result = CmdResult.BAD_START_BYTE; timer.Stop(); uart.DiscardInBuffer(); //вызов метода uart_received(Cmd com_in) в MainWindow через делегат RcvdMsg для отображения информации received(command_in); } } } if (state_rx == STATE_RX.ADDRESS) { if (uart.BytesToRead >= 1) { command_in.ADDRESS = (byte)uart.ReadByte(); state_rx = STATE_RX.DATALENGTH; } } if (state_rx == STATE_RX.DATALENGTH) { if (uart.BytesToRead >= 2) { for (int i = 0; i < 2 /*command_in.LENGTH.Length*2*/; i++) { command_in.LENGTH[i] = (byte)uart.ReadByte(); } state_rx = STATE_RX.CHKSUM1; } } if (state_rx == STATE_RX.CHKSUM1) { if (uart.BytesToRead >= 2) { byte [] checksum1Arr = new byte[2]; checksum1Arr[0] = (byte)uart.ReadByte(); checksum1Arr[1] = (byte)uart.ReadByte(); command_in.CHECKSUM1 = BitConverter.ToUInt16(checksum1Arr, 0); byte[] b = command_in.GetBufToSend(); if (checksum1Arr[0] != b[4] || checksum1Arr[1] != b[5]) { command_in.result = CmdResult.BAD_CHKSUM1; } else { state_rx = STATE_RX.DATA; } //timer.Stop(); //uart.DiscardInBuffer(); //received(command_in); } } if (state_rx == STATE_RX.DATA) { if (uart.BytesToRead >= command_in.DATA.Length) { for (int i = 0; i < 12 /*command_in.DATA.Length*/; i++)//должно быть 10 байт, а не 12 ? длина пакета данных 10 байт для ГСП { command_in.DATA[i] = (byte)uart.ReadByte(); } state_rx = STATE_RX.CHKSUM2; } } if (state_rx == STATE_RX.CHKSUM2) { if (uart.BytesToRead >= 2) { byte[] checksum2Arr = new byte[2]; checksum2Arr[0] = (byte)uart.ReadByte(); checksum2Arr[1] = (byte)uart.ReadByte(); command_in.CHECKSUM2 = BitConverter.ToUInt16(checksum2Arr, 0); byte[] b = command_in.GetBufToSend(); if (checksum2Arr[0] != b[GSP_PACKET_SIZE - 2] || checksum2Arr[1] != b[GSP_PACKET_SIZE - 1]) { command_in.result = CmdResult.BAD_CHKSUM2; } else { command_in.result = CmdResult.SUCCESS; } timer.Stop(); uart.DiscardInBuffer(); received(command_in); } } if (time <= 0) { command_in.result = CmdResult.TIMEOUT; timer.Stop(); uart.DiscardInBuffer(); received(command_in); } time -= (uint)timer.Period; return; }
/// <summary> /// Called by all constructors /// </summary> private void Setup() { SerialPortOpen = true; BufferPollTimer = new Multimedia.Timer(); BufferPollTimer.Period = 1; BufferPollTimer.Resolution = 0; BufferPollTimer.Tick +=new EventHandler(BufferPollTimer_Tick); BufferPollTimer.Start(); ResponseTimer = new Multimedia.Timer(); ResponseTimer.Period = TimeOut; ResponseTimer.Mode = TimerMode.Periodic; ResponseTimer.Resolution = 0; ResponseTimer.Stop(); ResponseTimer.Tick += new EventHandler(ResponseTimer_Tick); }
private void btnStop_Click(object sender, EventArgs e) { mmTimer.Stop(); }