ok so now im trying to port all of my code logic (coming from an arduino that can use the ethernet library) to the linux side of things and i found a quite perplexing behavior:
I'm running a packet grabbing server on the linux side:
import socket
import sys
from time import sleep
import subprocess
import threading
import string
import fcntl, os
import errno
import array
# For bridge communication
sys.path.insert(0, '/usr/lib/python2.7/bridge')
from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('192.168.0.1', 0)) #change to your router's ip
address=s.getsockname()[0]
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind the socket to the port
server_address = (address, 161) # replace with YOUR IP
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
i = 0
while True:
print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(4096)
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
print >>sys.stderr, data
print >>sys.stderr, 'sending to mcu'
value.put("recv",data)
I then proceed to recieve a packet and it outputs:
recieved 182 bytes from 192.168.2.33
30590201000403706475A04F020204D20201000201003043300A06066970416464720500300A06067375626e65740500300B060767617465776179050030070603646e730500300906056964656e74050030080604646863700500
sending to mcu
Then I grab the data using the microcontroller code:
...
void loop(){
char buff[512];
byte hexBuffer[512]={0};
Bridge.get("recv",buff,sizeof(buff));
Serial.println(buff);
strToHexArray(hexBuffer,buff,sizeof(buff));
}
void strToHexArray(byte outBuffer[], char inBuffer[], int nBytes){ //utility function
unsigned int u;
int i;
char * src = inBuffer;
Serial.print(src);
for(i=0;i<nBytes;i++){
sscanf(src,"%2x",&u);
src+=2;
outBuffer[i]=u;
Serial.print(src[i]);
Serial.print(u);
Serial.print(":");
}
Serial.println();
}
and this prints:
30590201000403706475A04F020204D20201000201003043300A06066970416464720500300A06067375626e65740500300B060767617465776179050030070603646e730500300906056964656e74050030080604646863700500
48:89:2:1:0:4:3:112:100:117:160:79:2:2:4:210:2:1:0:2:1:0:48:67:48:10:6:6:105:112:65:100:100:114:5:0:48:10:6:6:115:117:98:110:101:116:5:0:48:11:6:7:103:97:116:101:119:97:121:5:0:48:7:6:3:100:110:115:5:0:48:9:6:5:105:100:101:110:116:5:0:48:8:6:4:100:104:99:112:5:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:
the hex values of which are what I've intended them to be (those were the bytes I sent from the other computer, printed here in decimal form separated by ':' )
my question now is:
why does python print it as 30590201000403706475A04F020204D2020100020..... , the arduino prints it the same, but when I use the strToHexArray() it prints it as 48:89:2:1:0:4:3:112:100:117:160:79:2:2:4:210:2:1...... when the string 30590201000403706475A04F020204D2020100020..... is obviously not equivalent to 48:89:2:1:0:4:3:112:100:117:160:79:2:2:4:210:2:1......
I'm asking this because I wanted to port the whole strToHexArray() to the python side (which doesn't have pointers I can utilize to achieve the same result)
any help is greatly appreciated!