当前位置: 首页 > article >正文

树莓派开发扩展十二 -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;
             }
         }
     }
 }


http://www.kler.cn/a/403620.html

相关文章:

  • STM32 ADC 读取模拟量
  • MySQL数据库-索引的介绍和使用
  • nginx代理解决跨域问题CORS错误
  • Vue2与Vue3:深入比较与迁移指南
  • 【智谱清言-注册_登录安全分析报告】
  • 【Swift】字符串和字符
  • -bash: ./kafka-topics.sh: No such file or directory--解决方案
  • unittest和pytest
  • 摄像机ISP和DSP的区别?
  • PostgreSQL 单机与集群部署详细教程
  • 基于YOLOv8深度学习的医学影像肝脏肿瘤病症检测与诊断系统(PyQt5界面+数据集+训练代码)
  • HarmonyOS4+NEXT星河版入门与项目实战------Button组件
  • centos7 如何卸载和安装达梦数据库实例
  • Java项目实战II基于Spring Boot的社区医疗综合服务平台的设计与实现(开发文档+数据库+源码)
  • PostgreSQL TRUNCATE TABLE
  • 自然语言处理(NLP):让机器理解人类语言
  • 【模块一】kubernetes容器编排进阶实战资源对象之Configmap与Secret简介
  • 3、PyTorch张量的运算API(下)
  • .NET SDK 各操作系统开发环境搭建
  • 爬虫优化策略优化请求逻辑
  • i春秋-GetFlag(md5加密,字符串比较绕过)
  • 基于单片机的婴儿监测系统设计
  • 淘宝 NPM 镜像源
  • 【AI系统】AI 发展驱动力
  • cocoscreator-doc-TS-脚本开发-模块化脚本
  • 使用vscode+expo+Android夜神模拟器运行react-native项目