Discuss Scratch
- Discussion Forums
- » Connecting to the Physical World
- » Remote Sensors Protocol with C++
- elsep2002
-
1 post
Remote Sensors Protocol with C++
I am working on a project with the raspberry Pi and Scratch. I need to use the Remote Sensors Protocol with C++. I have tried porting the Python code across but i cannot get C++ to return the null values.
The original Python code looks like this:
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')
My Attempt in C++ looks like this:
char* scratchencode(string cmd)
{
int cmdlength;
cmdlength = cmd.length();
char* combind = new char;
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;
}
Needles to say it doesn't work, Can anyone help with the porting the code, i have tried to miminc the python code and the orgial doument at http://mv.ezproxy.com.ezproxyberklee.flo.org/wiki/Remote_Sensors_Protocol but have had no success.
Chris
The original Python code looks like this:
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')
My Attempt in C++ looks like this:
char* scratchencode(string cmd)
{
int cmdlength;
cmdlength = cmd.length();
char* combind = new char;
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;
}
Needles to say it doesn't work, Can anyone help with the porting the code, i have tried to miminc the python code and the orgial doument at http://mv.ezproxy.com.ezproxyberklee.flo.org/wiki/Remote_Sensors_Protocol but have had no success.
Chris
- RichardDL
-
16 posts
Remote Sensors Protocol with C++
Chris, No-one has given you the exact answer to this, so I will offer this. I have this code written in AutoIt and have been using it for several months.
Func Scratch_Send($socket, $sMsg)
; Send a single message to Scratch.
Local $iLen
Local $iSLen
$iLen = StringLen($sMsg)
; Message starts with 3 zeros and length, then message
; (approximation good for messages up to 255 chars)
$sMsg = Chr(0) & Chr(0) & Chr(0) & Chr($ilen) & $sMsg
$iSLen = TCPSend($socket, StringToBinary($sMsg))
EndFunc
$sMsg = StringFormat('sensor-update “D” %d', $iCount)
Scratch_Send($socket, $sMsg)
$sMsg = StringFormat('broadcast “Ex_AutoIt”')
Scratch_Send($socket, $sMsg)
I hope this helps,
Richard.
Func Scratch_Send($socket, $sMsg)
; Send a single message to Scratch.
Local $iLen
Local $iSLen
$iLen = StringLen($sMsg)
; Message starts with 3 zeros and length, then message
; (approximation good for messages up to 255 chars)
$sMsg = Chr(0) & Chr(0) & Chr(0) & Chr($ilen) & $sMsg
$iSLen = TCPSend($socket, StringToBinary($sMsg))
EndFunc
$sMsg = StringFormat('sensor-update “D” %d', $iCount)
Scratch_Send($socket, $sMsg)
$sMsg = StringFormat('broadcast “Ex_AutoIt”')
Scratch_Send($socket, $sMsg)
I hope this helps,
Richard.
Last edited by RichardDL (March 23, 2014 16:42:58)
- drmcw
-
1000+ posts
Remote Sensors Protocol with C++
A while since I've looked at C++ but surely new char isn't going to allocate a buffer anywhere near big enough? Also append seems a pointless exercise just write the bytes straight to the buffer. I am working on a project with the raspberry Pi and Scratch. I need to use the Remote Sensors Protocol with C++. I have tried porting the Python code across but i cannot get C++ to return the null values.
The original Python code looks like this:
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')
My Attempt in C++ looks like this:
char* scratchencode(string cmd)
{
int cmdlength;
cmdlength = cmd.length();
char* combind = new char;
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;
}
Needles to say it doesn't work, Can anyone help with the porting the code, i have tried to miminc the python code and the orgial doument at http://mv.ezproxy.com.ezproxyberklee.flo.org/wiki/Remote_Sensors_Protocol but have had no success.
Chris
Last edited by drmcw (March 23, 2014 14:29:57)
- Discussion Forums
- » Connecting to the Physical World
-
» Remote Sensors Protocol with C++