【问题标题】:Can't read from NETMF socket无法从 NETMF 套接字读取
【发布时间】:2012-01-12 06:49:42
【问题描述】:

这种对 Netduino 的 Pachube 样本的简单改编不起作用。它不会呕吐,它只是默默地失败。我注意到,如果您在写入后稍等片刻,套接字会在其 Available 属性中报告 315 个字节,但尝试读取数据根本不起作用 - cb 包含零并且未更改的缓冲区同意。

为什么我无法读取这些数据?我怀疑这是一个服务器响应,它很可能会帮助我找出应用程序的其余部分出了什么问题。

代码如下:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using SecretLabs.NETMF.Hardware;

public class HelloPachubeSockets
{
    static string apiKey = "wggHejZTKTvE6ZplSc5feSs1rQLeD4cYmi7WPYiG0VLIx_TC-isp8CI9vkKq3nQxadhydl3gUiSK8vS5SBd9JCCoXQG-g0N8FXYlYcNjEfJVMQJA-usgijpE1LRT-xw5";
    static string feedId = "43974";
    static double maxVoltage = 3.3;
    static int maxAdcValue = 1023;
    static int interval = 20000;
    static Socket connection = null;
    static Timer timer;
    static AnalogInput voltagePort;
    static OutputPort lowPort, highPort;
    static IPEndPoint remoteEndPoint;
    static double V = 0;

    static void LogSensor(object state)
    {
        Debug.Print("time: " + DateTime.Now);
        Debug.Print("memory available: " + Debug.GC(true));
        try
        {
            connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connection.SendTimeout = interval / 2;
            connection.Connect(remoteEndPoint);
            int rawValue = voltagePort.Read();
            double value = (rawValue * maxVoltage) / maxAdcValue;
            string sample = "voltage," + V.ToString("f");
            V += 0.1;
            Debug.Print("new message: " + sample);
            byte[] contentBuffer = Encoding.UTF8.GetBytes(sample);
            const string CRLF = "\r\n";
            var requestLine = "PUT /v2/feeds/" + feedId + ".csv HTTP/1.1\r\n";
            byte[] requestLineBuffer = Encoding.UTF8.GetBytes(requestLine);
            var headers =
                "Host: api.pachube.com\r\n" +
                "X-PachubeApiKey: " + apiKey + CRLF +
                "Content­Type: text/csv\r\n" +
                "Content­Length: " + contentBuffer.Length + CRLF +
                CRLF;
            byte[] headersBuffer = Encoding.UTF8.GetBytes(headers);
            connection.Send(requestLineBuffer);
            connection.Send(headersBuffer);
            connection.Send(contentBuffer);
            Thread.Sleep(400);
            int a = connection.Available;
            byte[] r = new byte[a];
            int cb = connection.Receive(r, 0, a, SocketFlags.None);
            connection.Close();
        }
        catch
        {
          Debug.Print("connection failed");
        }
    }

    public static void Main()
    {
        voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);
        lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);
        highPort = new OutputPort(Pins.GPIO_PIN_A2, true);
        remoteEndPoint = new IPEndPoint(Dns.GetHostEntry("api.pachube.com").AddressList[0], 80);
        timer = new Timer(LogSensor, null, 0, interval);
        Thread.Sleep(Timeout.Infinite);
    }
}

关于 Pachube 密钥的有效性,我检查了cURL,密钥和提要详细信息是正确的。

【问题讨论】:

  • 如果我没记错的话,那是 C#/.NET……你应该先尝试 C# 主机程序还是在 Arduino + Ethernet Shield 上尝试 C++?我想你也需要重新标记 C# ;)
  • 我也可以建议尝试使用 HTTP 类或 Pachube 提供的普通 TCP 套接字!
  • 它是 C#/.net 的事实隐含在 netduino 标记中。我还没有弄清楚为什么 this 代码不起作用,但我 已经 获得了一些更高级的代码,它确实起作用。当我明白问题出在哪里时,我会写下来(如果没有其他人先回答)。

标签: .net sockets .net-micro-framework netduino cosm


【解决方案1】:

查看http://msdn.microsoft.com/en-us/library/w3xtz6a5.aspx#Y0,您不需要休眠来等待套接字上的响应,因为如果缓冲区为空,接收是一个阻塞操作。

如果您尝试将接收代码简化为:

// Blocks until send returns.
connection.Send(...)

// Get reply from the server.
int byteCount = server.Receive(bytes, 0, connection.Available, 
                               SocketFlags.None);

if (byteCount > 0)
    Console.WriteLine(Encoding.UTF8.GetString(bytes));

或者,您可以显式尝试从套接字读取 256 字节之类的内容,而不是可用的数字。

【讨论】:

  • 你是对的,但不是你想的那样。这是一个跨线程的事情,按照你的方式重写它会让问题消失。我不知道当我写那个糟糕的代码时我在想什么。
猜你喜欢
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2013-10-12
相关资源
最近更新 更多