在 16x2 液晶屏上显示英特尔伽利略 IP 地址



我正在运行 Windows Developer Program for IoT (https://ms-iot.github.io/content/16x2LCD.htm( 中的 16x2 LCD 示例。获取并在显示屏上显示伽利略IP地址而不是"Hello!"消息的最佳方法是什么?问候。

法典

stdafx.h

#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include "arduino.h"
#include "LiquidCrystal.h" // we need this library for the LCD commands

主.cpp

#include "stdafx.h"
int RS = 4;
int ENABLE = 5;
int D0 = 6;
int D1 = 7;
int D2 = 8;
int D3 = 9;
LiquidCrystal lcd = LiquidCrystal(RS, ENABLE, D0, D1, D2, D3); // define our LCD and which pins to use
int _tmain(int argc, _TCHAR* argv [])
{
    return RunArduinoSketch();
}
void setup()
{
    Log(L"LCD Samplen");
    lcd.begin(16, 2); // need to specify how many columns and rows are in the LCD unit (it calls clear at the end of begin)
    lcd.setCursor(0, 0);
    lcd.print("Hello!");
    lcd.setCursor(0, 1);
    lcd.print(3.14159, 4); // prints a double, the 2nd number is the digits to print after the .
}
void loop()
{
}

我会使用Windows API以字符串形式获取IP地址,然后使用lcd.print将字符串打印到LCD。

此 MSDN 页面很好地解释了和展示如何使用 Windows API 获取 IP 地址。

MSDN 上的 GetAdaptersInfo 页面提供了获取 IP 地址的示例代码。我基本上将下面的代码添加到伽利略项目的设置功能中,并在LCD上显示IP地址:

lcd.begin(16, 2); // columns and rows, LCD unit (it calls clear at the end of begin)
lcd.setCursor(0, 0);
lcd.print("IP Address:");
PIP_ADAPTER_INFO    pAdapterInfo = NULL;
PIP_ADAPTER_INFO    pAdapter = NULL;
ULONG               ulOutBufLen = sizeof(IP_ADAPTER_INFO);
DWORD               dwRetVal = 0;
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
    printf("Error allocating memory needed to call GetAdaptersinfon");
}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
    FREE(pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
    if (pAdapterInfo == NULL) {
        printf("Error allocating memory needed to call GetAdaptersinfon");
    }
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
    pAdapter = pAdapterInfo;
    lcd.setCursor(0, 1);
    lcd.print(pAdapter->IpAddressList.IpAddress.String);
}

写了一篇博客文章,展示了我为让它工作而采取的方法。希望这有帮助。

最新更新