在学习官方课程进阶课程的LESSON4-设备与网络,TCP通讯技术实现时, 发现小龟作为SocketClient客户端,连接本地PC搭建的TCP服务器进行通讯. 我就在想,是否可以将小龟作为TcpServer来使用呢. 经研究测试, 实现了钛极小龟作为SocketServer进行TCP通讯的效果.并开发C#程序, 运行于PC端作为SocketClient, 与开发板通讯, 定时获取温湿度,光照度等各项参数并显示.
实验步骤:
1.烧写程序
开发板程序采用多线程, 使用三个线程: Socket监听线程, Socket发送线程, 传感器测量线程. 相互配合又互不干扰, 实现将最新的测量参数发送回客户端.
PC端程序采用线程作为SocketClient轮询开发板, 定时获取相关参数并显示.
2.参数显示
2.1 初始状态
初始时,显示当前的温湿度,光照,声响,按键,触摸,红外的状态
2.2 遮盖BH1750
遮盖光照传感器后, 可看到光照度下降了很多
2.3 按下按键
按下开发板按钮, 可看到按键状态从无变为有
2.4 按下遥控
按下红外遥控按键, 可看到软件界面显示遥控键码0-64
3.LCD显示
LCD实时显示环境温湿度,光照度,声响参数
开发板代码:
- try {
- tiWiFi obj = TiWiFi.getInstance();
- System.out.println("SSID: " + obj.getSSID());
- obj.startup(30);
- System.out.println("Wi-Fi Connect. IP: " + obj.getAddress());
- //obj.startupSoftAP(false);
- //System.out.println("Start SoftAP. IP: " + obj.getSoftAPAddress());
- } catch (IOException ex) {
- System.out.println("Wi-Fi Connect Failed.");
- ex.printStackTrace();
- return;
- }
- try {
- // GPIO资源分配,GPIO0的PIN2/3/4/5脚
- TiGPIO gpio0 = TiGPIO.open(0, 2, 3, 4, 5);
- // I2C主机总线资源分配,I2CM0
- TiI2CMaster i2cm0 = TiI2CMaster.open(0);
- // ADC资源分配,ADC0的CH0通道
- TiADC adc0 = TiADC.open(0, 0);
- /* ---资源绑定--- */
- // I2C主机总线资源PORT0与屏幕对象绑定,屏幕地址:0x3C
- TiOLED_UG2864 oled = new TiOLED_UG2864(i2cm0, 0x3c);
- // I2C主机总线资源PORT0与照度计BH1750对象绑定,默认地址:0x23
- TiBH1750 bh1750 = new TiBH1750(i2cm0);
- // GPIO总线资源PIN2与继电器对象绑定
- TiRelay1CH relay = new TiRelay1CH(gpio0, 2);
- // GPIO总线资源PIN3与温湿度传感器DHT11绑定
- TiDHT dht11 = new TiDHT(gpio0, 3);
- // GPIO总线资源PIN4与触摸按钮对象绑定,触发电平:高电平
- TiButton touch = new TiButton(gpio0, 4, true);
- // GPIO总线资源PIN5与红外解码对象绑定
- TiVS1838BNEC vs1838b = new TiVS1838BNEC(gpio0, 5);
- // 获取键盘实例并设置键盘事件监听对象
- TiKeyboard keyboard = TiKeyboard.getInstance();
- keyboard.setEventListener(new KeyboardListener());
- // 设置ADC参考电压及分压倍数
- adc0.setRefVoltageValue(1.0, 2);
- /* ---资源使用--- */
- // 屏幕开启并清屏
- oled.turnOn();
- oled.clear();
- oled.print(0, 0, "Socket Test!");
-
- touch.setEventListener(new TouchListener(relay));
- vs1838b.setEventListener(new IRDecodeListener());
- Thread.sleep(2000);
-
- //启动传感器测量线程
- SensorComm sensorThread = new SensorComm(bh1750, dht11, oled, adc0, touch, keyboard, vs1838b);
- sensorThread.start();
- //启动TCP通讯线程
- ServerComm td = new ServerComm();
- td.start();
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- try {
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
复制代码
PC端SocketClient代码:
while (continueread)
- while (ContinueRead)
- {
- try
- {
- Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- clientSocket.Connect(ipe);
- byte[] sendBytes = Encoding.ASCII.GetBytes("GET");
- clientSocket.Send(sendBytes);
- string recStr = "";
- byte[] recBytes = new byte[1024];
- clientSocket.SendTimeout = 5000;
- int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
- recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
- clientSocket.Close();
- Console.WriteLine("Rcv:" + recStr);
- string[] arys = recStr.Split(';');
- this.Invoke((EventHandler)(delegate {
- label4.Text = arys[0];
- label8.Text = arys[1];
- label11.Text = arys[2];
- label14.Text = arys[3];
- label17.Text = (arys[4] == "1" ? "有" : "无");
- label20.Text = (arys[5] == "1" ? "有" : "无");
- label23.Text = (arys[6] == "N" ? "无" : arys[6]);
- }));
- Thread.Sleep(500);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
复制代码
实验完毕.
实验过程中TCP通讯出现了一些错误, 而比较遗憾的是Eclipse给出的错误提示(e.printStackTrace())又太过简略, 无法提示准确错误原因, 导致调试测试花费了太多的时间. 希望官方可以优化.
|