0

RESOLVED: Problem was primarily with the simulink blockset that was reading in the UDP packet rather than data transmission.

I am trying to send a 20 byte numerical array out of a c++ program by using winsock. The issue I am running into is data packaging, in the end I want each number in the array to go out as its own byte so that my simulink model that is receiving these values does not need an additional processing script.

My array contains 14 boolean values (0|1) and then 6 values that range from -100 to 100. These are reporting the status of a controller input. for example the array would look like

    int array msgint[20] = [1,0,1,0,0,1,1,0,0,0,0,0,0,0,50,80,-90,40,90,-20];

I have tried using typecasting and sending multiple strings but all just appear to rearrange the gibberish I am getting or cause a socket error. Currently my sendto function looks like

        sendto(sd,message,80,0,(struct sockadd *) &server,server_length)

I know this line works as the packet makes it through it just does not appear as I would like it to. In the send to, message is the formatted string I am trying to create to properly send all of contents of the array. Currently is is arbitrary and has little significance I have it in for debugging purposes essentially.

3
  • What is message? How do you receive the data? Have you just tried e.g. send(sd, (char*) msgint, sizeof(msgint), ...) with corresponding recvfrom(sd, (char*) msgint, sizeof(msgint), ...) on the receiving end (requires both platforms to have the same endianess and int size)? Commented Jul 22, 2014 at 15:48
  • 1
    It might be pretty interesting to see the code that converts msgint to message. Otherwise I'm afraid your question will be harder to answer. Commented Jul 22, 2014 at 15:48
  • message is simply arbitrary at this point in time. Currently it is a place holder in that line, I tried using a few conversion methods i found elsewhere on here and none of which seemed to work. The receiving end is a DSpace MABXII so I am using their software to read what the port is getting If I try using (char*) in the send function I get a compiler error because it is expecting (const char*), and with that I end up having a socket error Commented Jul 22, 2014 at 15:50

3 Answers 3

1

you are starting at the wrong point. Network communications should start with the design of the wire protocol.

How will you represent something on the wire. Binary or text. Most 'modern' protocols use text (json or xml). A few years ago binary was hot (asn1/ber/der). I suggest json

Then how will you wrap up the payload. Do you need to say 'here is a set of xxxs. now here is a set of yyyys'. I dont know what you are doing so its hard to say what you need

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to send 20 bytes, and you know that every integer value in your array will be in the [-100, +100] range, you should not use the int type, which usually contains either 32-bit or 64-bit values on modern platforms.

You might instead want to use the char type, which usually represents a 8-bit value.

For even more certainty, if you can use C++11 features, you should use the <cstdint> header, which defines the int8_t type, guaranteed to be a signed 8-bit type. See http://en.cppreference.com/w/cpp/types/integer.

Your array should look like:

#include <cstdint>
std::int8_t msgint[20] = {1,0,1,0,0,1,1,0,0,0,0,0,0,0,50,80,-90,40,90,-20};

or

char msgint[20] = {1,0,1,0,0,1,1,0,0,0,0,0,0,0,50,80,-90,40,90,-20};

and your sendto will be:

sendto(sd,msgint,20,0,(struct sockadd *) &server,server_length);

1 Comment

Using the char msgint suggestion, It caused me to realize my problem was not in fact on the c++ side of my system ..running a simple server application on a non-PowerPC made me notice where the issue lied and that is with the missuse of the blockset I need to read UDP in simulink. I did not catch that it was autoconcatinating things from unit8 to uint32. With a simple decode function everything began functioning as expected.
0

"in the end I want each number in the array to go out as its own byte"

Then change your array to

char msgint[20] = ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.