public async Task WriteAsync( WakePacket packet ) { if( serialPort == null ) { return; } try { await Task.Run( () => { if( m_disposed ) { return; } byte[] buffer = packet.GetTransferBuffer(); string hex = BitConverter.ToString( buffer ).Replace( "-", "" ); serialPort.Write( buffer: buffer, offset: 0, count: buffer.Length ); } ); } catch( Exception ) { if( m_disposed ) { return; } throw; } }
private void SendCurrentHandCommandAsync() { if( lstHandCommands.Items.Count > 6 ) { lstHandCommands.Items.Clear(); } IEnumerable<byte[]> commands = m_handController.GetCommand(); foreach( byte[] cmd in commands ) { WakePacket packet = new WakePacket() { Address = 1, Command = 77, Data = cmd }; string outputCommand = String.Format( "H {0} {1}", cmd[0], cmd[1] ); lstHandCommands.Items.Add( outputCommand ); SendCommandAsync( packet ); } }
private async void SendCommandAsync( WakePacket packet ) { if( Current_com_port == null || packet == null ) { return; } try { await Current_com_port.WriteAsync( packet ); } catch( TaskCanceledException ) { //ignore } catch( Exception ex ) { if( Current_com_port != null ) { Current_com_port.Close(); Current_com_port = null; } MessageBox.Show( ex.Message ); } }
private void Send_current_joystick_command_to_com_port() { string command = Joystick_controller.Get_joystick_command_string(); if( m_lastWheelCommand == command ) { return; } m_lastWheelCommand = command; byte[] Command_bytes_array = Encoding.ASCII.GetBytes( command ); WakePacket packet = new WakePacket() { Address = 1, Command = 87, Data = Command_bytes_array }; SendCommandAsync( packet ); }