将传感器值从Beaglebone Black发送到Windows上的套接字服务器

时间:2018-12-18 09:53:43

标签: python sockets beagleboneblack

我的Beaglebone Black上有一个python TCP socket客户端代码,该代码从传感器读取数据并将其发送到服务器。我的服务器在Windows中的Visual Studio中,并且用C#编写。使用Putty通过SSH连接的Beaglebone Black和Windows PC。问题是服务器和客户端看不到对方。但是,当我使用Ubuntu PC(用python编写的服务器)而不是Windows时,一切正常。

能否请您帮助我建立连接。我认为问题出在服务器端。

P.S。由于将来的计划,必须使用C#编写服务器。

这是我的C#服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace Server_Tutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress ip = Dns.GetHostEntry("192.168.7.1").AddressList[0];
            TcpListener server = new TcpListener(ip, 7000);
            TcpClient client = default(TcpClient);

            try
            {
                server.Start();
                Console.WriteLine("Server started...");
                Console.Read();
            } catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.Read();
            }

            while (true)
            {
                client = server.AcceptTcpClient();

                byte[] receivedBuffer = new byte[100];
                NetworkStream stream = client.GetStream();


                stream.Read(receivedBuffer, 0, receivedBuffer.Length);

                StringBuilder msg = new StringBuilder();

                foreach (byte b in receivedBuffer)
                {
                    if (b.Equals(00))
                    {
                        break;
                    }
                    else
                    {
                        msg.Append(Convert.ToChar(b).ToString());
                    }
                }

                Console.WriteLine(msg.ToString() + msg.Length);
            }
        }
    }
}

这是python中的客户端代码:

import Adafruit_BBIO.ADC as ADC
import time
import socket
import sys

serverAddr = ('192.168.7.1', 7000)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(serverAddr)

fsr = 'P9_40'

ADC.setup()

while True:
        reading = ADC.read(fsr)*1000
        reading = int(round(reading))
        client.send(str(reading))

client.close()

这是在Ubuntu上可以使用的python服务器代码:

import socket
import sys

HOST = ''
PORT = 7000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')

try:
        s.bind((HOST, PORT))
except socket.error as msg:
        print('Bind failed. ')
        sys.exit()

print('Socket bind complete')

s.listen(10)
print('Socket now listening')

conn, addr = s.accept()
print('Connected to ' + addr[0] + ':' + str(addr[1]))

while True:
        data = conn.recv(1024)
        line = data.decode('UTF-8')    # convert to string (Python 3 only)
        line = line.replace("\n","")   # remove newline character
        print( line )

s.close()

1 个答案:

答案 0 :(得分:0)

您是否尝试过运行: netstat -a -b

在您的PC上?

如果要显示您的端口7000已被另一个TCP连接使用,则可以简单地切换到port 8080或您希望成功连接的另一个端口。