我正在尝试使用Visual Studio 2015中的CoAPSharp二进制文件创建CoAP NON服务器。我的目标设备是带有Windows IoT核心的Raspberry Pi。
“Remotesender”部分有错误。我不知道如何解决它
/// <summary>
/// Called when a request is received
/// </summary>
/// <param name="coapReq">The CoAPRequest object</param>
void OnCoAPRequestReceived(CoAPRequest coapReq)
{
//This sample only works on NON requests of type GET
//This sample simualtes a temperature sensor at the path "sensors/temp"
string reqURIPath = (coapReq.GetPath() != null) ? coapReq.GetPath ().ToLower() : "";
/**
* Draft 18 of the specification, section 5.2.3 states, that if against a NON message,
* a response is required, then it must be sent as a NON message
*/
if (coapReq.MessageType.Value != CoAPMessageType.NON)
{
//only NON combination supported..we do not understand this send a RST back
CoAPResponse msgTypeNotSupported = new CoAPResponse (CoAPMessageType.RST, /*Message type*/
CoAPMessageCode.NOT_IMPLEMENTED, /*Not implemented*/
coapReq.ID.Value /*copy message Id*/);
msgTypeNotSupported.Token = coapReq.Token; //Always match the request/response token
msgTypeNotSupported.RemoteSender = coapReq.RemoteSender;
//send response to client
this._coapServer.Send(msgTypeNotSupported);
}
else if (coapReq.Code.Value != CoAPMessageCode.GET)
{
//only GET method supported..we do not understand this send a RST back
CoAPResponse unsupportedCType = new CoAPResponse (CoAPMessageType.RST, /*Message type*/
CoAPMessageCode.METHOD_NOT_ALLOWED, /*Method not allowed*/
coapReq.ID.Value /*copy message Id*/);
unsupportedCType.Token = coapReq.Token; //Always match the request/response token
unsupportedCType.RemoteSender = coapReq.RemoteSender;
//send response to client
this._coapServer.Send(unsupportedCType);
}
else if (reqURIPath != "sensors/temp")
{
//classic 404 not found..we do not understand this send a RST back
CoAPResponse unsupportedPath = new CoAPResponse (CoAPMessageType.RST, /*Message type*/
CoAPMessageCode.NOT_FOUND, /*Not found*/
coapReq.ID.Value /*copy message Id*/);
unsupportedPath.Token = coapReq.Token; //Always match the request/response token
unsupportedPath.RemoteSender = coapReq.RemoteSender;
//send response to client
this._coapServer.Send(unsupportedPath);
}
else
{
//All is well...send the measured temperature back
//Again, this is a NON message...we will send this message as a JSON
//string
Hashtable valuesForJSON = new Hashtable();
valuesForJSON.Add("temp", this.GetRoomTemperature());
string tempAsJSON = JSONResult.ToJSON(valuesForJSON);
//Now prepare the object
CoAPResponse measuredTemp = new CoAPResponse(CoAPMessageType.NON, /*Message type*/
CoAPMessageCode.CONTENT, /*Carries content*/
coapReq.ID.Value/*copy message Id*/);
measuredTemp.Token = coapReq.Token; //Always match the request/response token
//Add the payload
measuredTemp.Payload = new CoAPPayload(tempAsJSON);
//Indicate the content-type of the payload
measuredTemp.AddOption(CoAPHeaderOption.CONTENT_FORMAT,
AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_JSON));
//Add remote sender address details
measuredTemp.RemoteSender = coapReq.RemoteSender;
//send response to client
this._coapServer.Send(measuredTemp);
}
}
错误:
错误:CS1061 C#不包含定义,也没有接受第一个类型参数的扩展方法(你是否缺少using指令或汇编引用?)
只需按照CoAPSharp官方网站上的教程进行操作。
答案 0 :(得分:0)
如果要在带有Windows 10 IoT核心的Raspberry Pi上运行CoAPSharp库,则需要下载适用于Windows 10 IoT核心的实验版本。网址为http://www.coapsharp.com/releases/。请参阅上一个下载链接。
此外,您可能希望获取源代码并重新编译以获取要在主项目中引用的最新二进制文件。
在11月3日至16日添加的其他信息: 好的,我明白了问题所在。这是一个完整而大的答案: - )
CoAPSharp实验库有一个小错误(尽管与您的问题无关)。我是CoAPSharp开发人员之一,并为构建库的公司工作。带有修复程序的更新库应该在一两天内可用。问题出在同步接收上。
您尝试运行的示例适用于NETMF而不适用于Windows 10 IoT Core,这就是您收到错误的原因。 Raspberry Pi实验版没有样品。为了帮助您解决问题,我将提供以下逐步解决方案:
首先,下载最新的CoAPSharp实验库以获取同步接收错误的修复。
接下来,创建一个解决方案,在其中创建一个UWA项目并将CoAPSharp库添加到解决方案中。在UWA项目中引用库。
5.1在此项目中为Raspberry Pi创建一个小型服务器代码,如下所示:
/// <summary>
/// We will start a server locally and then connect to it
/// </summary>
private void TestLocalCoAPServer()
{
/*_coapServer variable is a class level variable*/
_coapServer = new CoAPServerChannel();
_coapServer.CoAPRequestReceived += OnCoAPRequestReceived;
_coapServer.Initialize(null, 5683);
}
/// <summary>
/// Gets called everytime a CoAP request is received
/// </summary>
/// <param name="coapReq">The CoAP Request object instance</param>
private async void OnCoAPRequestReceived(CoAPRequest coapReq)
{
//send ACK back
Debug.WriteLine("Received Request::" + coapReq.ToString());
CoAPResponse coapResp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq);
coapResp.AddPayload("GOT IT!");
await _coapServer.Send(coapResp);
}
5.2接下来,确保Raspberry Pi上没有阻止端口5683。如果是,则使用powershell取消阻止它。
5.3接下来,在桌面上创建一个新的解决方案(我使用的是Windows 10和Visual Studio 2015社区版)并添加另一个CoAPSharp Raspberry Pi实验库副本。
5.3现在在桌面上创建另一个UWA项目。另外,请参考此新项目中的CoAPSharp项目。
5.4现在在这个项目中编写一个客户端,它将从Raspberry Pi上托管的服务器发送/接收CoAP消息:
private async void TestCoAPAsyncClient()
{
/*_coapAsyncClient is declared at class level*/
this._coapAsyncClient = new CoAPClientChannel();
/*minwinpc was the name of the device running Windows IoT core on Raspberry Pi*/
this._coapAsyncClient.Initialize("minwinpc", 5683);
this._coapAsyncClient.CoAPError += delegate (Exception e, AbstractCoAPMessage associatedMsg) {
Debug.WriteLine("Exception e=" + e.Message);
Debug.WriteLine("Associated Message=" + ((associatedMsg != null) ? associatedMsg.ToString() : "NULL"));
};
this._coapAsyncClient.CoAPRequestReceived += delegate (CoAPRequest coapReq) {
Debug.WriteLine("REQUEST RECEIVED !!!!!!!!!!!!!!" + coapReq.ToString());
};
this._coapAsyncClient.CoAPResponseReceived += delegate (CoAPResponse coapResp) {
Debug.WriteLine("RESPONSE RECEIVED <<<<<<<<<<<<<" + coapResp.ToString());
};
CoAPRequest req = new CoAPRequest(CoAPMessageType.CON, CoAPMessageCode.GET, 100);
//req.SetURL("coap://capi.coapworks.com:5683/v1/time/pcl?mid=CPWK-TESTM");
req.SetURL("coap://localhost:5683/someurl");
await this._coapAsyncClient.Send(req);
}
您可以在按钮单击或加载页面时调用方法TestCoAPAsyncClient()。
我使用的设置是一个带有Windows 10和Visual Studio Community Edition 2015的桌面。它连接到运行Windows 10 IoT核心的Raspberry Pi(操作系统:10.0.10240.16384)。我的桌面和Raspberry Pi都连接到以太网。
CoAPSharp团队在链接http://www.coapsharp.com/coap-server-windows-10-iot-core-raspberry-pi/添加了一个示例,以进一步详细说明。