树莓派开发扩展十二 -C#编写客户端控制小车 Xamarin.Forms

1、效果

2、前台代码

<StackLayout>
    <Frame
        Padding="24"
        BackgroundColor="#2196F3"
        CornerRadius="0">
        <Label
            FontSize="30"
            HorizontalTextAlignment="Center"
            Text="树霉派小车控制器"
            TextColor="White" />
    </Frame>
 
    <Label
        x:Name="lbInfo"
        Padding="30,10,30,10"
        FontSize="18"
        Text="Wait Info!" />
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackLayout Grid.ColumnSpan="3" Orientation="Horizontal">
            <Label
                Margin="15,0,0,0"
                Text="IP地址:"
                VerticalTextAlignment="Center" />
            <Editor x:Name="tbIP" WidthRequest="200" />
        </StackLayout>

        <StackLayout
            Grid.Row="1"
            Grid.ColumnSpan="3"
            Orientation="Horizontal">
            <Label Text="通讯端口:" VerticalTextAlignment="Center" />
            <Editor x:Name="tbPort" WidthRequest="200" />
        </StackLayout>

        <Button
            Grid.Row="0"
            Grid.Column="3"
            Clicked="Button_Click"
            Text="连接服务" />
        <Button
            Grid.Row="1"
            Grid.Column="3"
            Clicked="Button_Click"
            Text="断开服务" />

        <Button
            Grid.Row="2"
            Grid.Column="0"
            Clicked="Button_Click"
            Text="手动模式" />
        <Button
            Grid.Row="2"
            Grid.Column="1"
            Clicked="Button_Click"
            Text="自动模式" />
        <Button
            Grid.Row="2"
            Grid.Column="2"
            Clicked="Button_Click"
            Text="智能模式" />


        <Button
            Grid.Row="3"
            Grid.Column="0"
            Clicked="Button_Click"
            Text="加速" />
        <Button
            Grid.Row="3"
            Grid.Column="1"
            Clicked="Button_Click"
            Text="减速" />

        <Button
            x:Name="butFornt"
            Grid.Row="4"
            Grid.Column="1"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="前" />
        <Button
            x:Name="butLeft"
            Grid.Row="5"
            Grid.Column="0"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="左" />
        <Button
            x:Name="butRight"
            Grid.Row="5"
            Grid.Column="2"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="右" />
        <Button
            x:Name="butBack"
            Grid.Row="6"
            Grid.Column="1"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="后" />
    </Grid>
</StackLayout>

3、后台代码

 public partial class MainPage : ContentPage
 {
     TcpClient client = null;
     public MainPage()
     {
         InitializeComponent();
          
     }

     private void Button_Click(object sender, EventArgs e)
     {
         Button but = sender as Button;
         if (but != null)
         {
             string tag = but.Text;
             switch (tag)
             {
                 case "连接服务":
                     try
                     {
                         IPAddress address = IPAddress.Parse(tbIP.Text.Trim());
                         int port = Convert.ToInt32(tbPort.Text.Trim());
                         IPEndPoint endPoint = new IPEndPoint(address, port);
                         client = new TcpClient();
                         client.Connect(endPoint);
                         SetInfo("连接服务器成功!");
                     }
                     catch (Exception ex)
                     {
                         SetInfo("连接服务器异常:" + ex.Message);

                     }
                     break;
                 case "断开服务":
                     try
                     {
                         client.Close();
                         SetInfo("断开服务器成功!");


                     }
                     catch (Exception ex)
                     {
                         SetInfo("断开服务器异常:" + ex.Message);

                     }
                     break;
                 case "手动模式":
                     SendMsgToServer("s");
                     break;
                 case "自动模式":
                     SendMsgToServer("a");
                     break;
                 case "智能模式":
                     SendMsgToServer("mu");
                     break;

                 case "加速":
                     SendMsgToServer("up");
                     break;
                 case "减速":
                     SendMsgToServer("down");
                     break;
                 default:
                     SetInfo("按钮关联事件失败");
                     break;
             }
          
         }

     }

     private void SetInfo(string msg)
     {
         lbInfo.Text = msg;
     }



     private void ButReleased_Click(object sender, EventArgs e)
     {
         Button but = sender as Button;
         if (but != null)
         {
             string tag = but.Text;
             switch (tag)
             {
                 case "前":
                 case "后":
                 case "左":
                 case "右":
                     SendMsgToServer("s");
                     break;
                 default:
                     SetInfo("按钮关联事件失败");
                     break;
             }
         }
     }

     private void SendMsgToServer(string msg)
     {
         if (client == null)
         {
             return;
         }
         try
         {
             byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
             client.GetStream().Write(data, 0, data.Length);
             SetInfo("发送信息至服务器:" + msg);
         }
         catch (Exception ex)
         {
             SetInfo("发送信息至服务器异常:" + ex.Message);

         }


     }

     private void ButPressed_Click(object sender, EventArgs e)
     {
         Button but = sender as Button;
         if (but != null)
         {
             string tag = but.Text;
             switch (tag)
             {
                 case "前":
                     SendMsgToServer("f");
                     break;
                 case "后":
                     SendMsgToServer("b");
                     break;
                 case "左":
                     SendMsgToServer("l");
                     break;
                 case "右":
                     SendMsgToServer("r");
                     break;
                 default:
                     SetInfo("按钮关联事件失败");
                     break;
             }
         }
     }
 }

上一篇:Centos Stream 9安装Jenkins-2.485


下一篇:003 STM32基础、架构以及资料介绍——常识