Python 带有C+的远程传感器协议+;

Python 带有C+的远程传感器协议+;,python,c++,raspberry-pi,mit-scratch,Python,C++,Raspberry Pi,Mit Scratch,我正在和覆盆子Pi和Scratch合作一个项目。我需要使用C++的远程传感器协议。我曾尝试过移植Python代码,但不能获得C++返回空值。 原始Python代码如下所示: import socket from array import array HOST = '192.168.1.101' PORT = 42001 scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) scratchSock.connect((HO

我正在和覆盆子Pi和Scratch合作一个项目。我需要使用C++的远程传感器协议。我曾尝试过移植Python代码,但不能获得C++返回空值。

原始Python代码如下所示:

import socket
from array import array

HOST = '192.168.1.101'
PORT = 42001

scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))

def sendCMD(cmd):
    n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF))
    a.append(chr((n >> 16) & 0xFF))
    a.append(chr((n >>  8) & 0xFF))
    a.append(chr(n & 0xFF))
    scratchSock.send(a.tostring() + cmd)

sendCMD('sensor-update "dave" 201')
char* scratchencode(string cmd)
{
    int cmdlength;

    cmdlength = cmd.length();
    char* combind = new char[20];
    const char * sCmd = cmd.c_str();
    char append[]={(cmdlength >> 24) & 0xFF, (cmdlength >> 16) & 0xFF, (cmdlength >> 8) & 0xFF, (cmdlength & 0xFF)};
    strcpy(combind,append);
    strcpy(combind,sCmd);
    return combind;
}
我在C++中的尝试看起来是这样的:

import socket
from array import array

HOST = '192.168.1.101'
PORT = 42001

scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))

def sendCMD(cmd):
    n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF))
    a.append(chr((n >> 16) & 0xFF))
    a.append(chr((n >>  8) & 0xFF))
    a.append(chr(n & 0xFF))
    scratchSock.send(a.tostring() + cmd)

sendCMD('sensor-update "dave" 201')
char* scratchencode(string cmd)
{
    int cmdlength;

    cmdlength = cmd.length();
    char* combind = new char[20];
    const char * sCmd = cmd.c_str();
    char append[]={(cmdlength >> 24) & 0xFF, (cmdlength >> 16) & 0xFF, (cmdlength >> 8) & 0xFF, (cmdlength & 0xFF)};
    strcpy(combind,append);
    strcpy(combind,sCmd);
    return combind;
}
如果说它不起作用,有人能帮我移植代码吗?我曾经尝试过模仿python代码和原始版本,但并没有成功


克里斯

我已经解决了这个问题,谢谢你帕夫·斯塔瓦兹。您的建议正是我所需要的,我将整个函数转换为使用字符串,第一次就成功了

代码如下:

string scratchencode(string cmd)
{
    int cmdlength; // holds the length of cmd
    string combind; // used to store the concatenated Packet
    string mgsSize; // used to store Message size

    cmdlength = cmd.length(); // length of CMD

    //convert intiger to a  4 byte 32-bit big-Endian number, using bit shifting.
    mgsSize = (cmdlength >> 24);
    mgsSize += (cmdlength >> 16);
    mgsSize += (cmdlength >> 8);
    mgsSize += cmdlength;

    combind = mgsSize + cmd; // concatenate mgsSize and cmd producing a structure of  [size][size][size][size][string CMD (size bytes long)]
    return combind; // return the string
}

您首先将
append
复制到
combined
中,然后用
sCmd
覆盖它。我不确定你是不是想这么做。那是有道理的,我以为这是在连接内容。理想情况下,我希望combind的结果是append+和sCmd的串联。另外,append应该是一个4字节32位的大端数字。类似于这个[size][size][size][size][string CMD(大小字节长)],如果我是你,我会使用
std::string
。然后您可以简单地使用
combid=append+sCmd
。但是因为您使用的是字符数组,所以必须使用strcat()