# TCP Communication with PCDuino

I'm working on a robot that is controlled by an xbox controller connected to a windows computer and commands are sent to a pcduino through a tcp connection. I have it working by sending a string of 1's and 0's to tell the pcduino which motors to turn on. I'm trying to optimize it by just sending an int and using bit masks to make the decisions on the pcduino but I can't get the pcduino to receive the int correctly. I tested the windows function sending the command with sokit and its sending the correct values but the pcduino is receiving the same number even when the commands are changing.

This is what its doing:

Windows -> PCDuino

command = 1 -> sendBuff = 73932

cmdstring = 1 -> n = 1

command = 1025 -> sendBuff = 73932

cmdstring = 1025 -> n = 4

My windows functions are:

bool Client::Send(char * smsg)
{
int iResult = send(ConnectSocket, smsg, strlen(smsg), 0);

if (iResult == SOCKET_ERROR)
{
std::cout << "Sending Message has failed: " << WSAGetLastError() << "\n";
Stop();
return false;
}
return true;
}


    bool sendCommand()
{
cmdbuffer << command;
cmdstring = cmdbuffer.str();

if (!client->Send((char *)cmdstring.c_str()))
{
std::cout << "Disconnected from Server. Press Enter to Exit";
std::cin.ignore();
std::cin.get();
return false;
}
return true;
}


PCDuino Loop Function

void loop()
{
recBuff = 0;
deviceFlag = 0;

/******************************************************************************/

recBuff = atoi(sendBuff);

/******************************************************************************/

//Set Current Device to Receive Instructions From
checkAuto(recBuff, currDevice);

//Find Current Device of Command
deviceFlag = checkDevice(recBuff);

//If Current Device and Set Device are the Same Parse Command
if (deviceFlag == currDevice)
{
parseHex(recBuff);
}
usleep(50000);
}


I have a printf after the read call and that's where I am getting the 73932 number. I think I have everything you guys need but if there's anything else I need to add let me know. I'm stumped...I don't know if its just a casting problem or what.

## Update 1

What I have before everything the setup and loop functions on the PCduino run is:

int listenfd = 0, connfd = 0;
int n;
char sendBuff[1025];
time_t ticks;


Sorry but its not really clear from your code whats going on, but the area drawing my attention is:

   read(connfd, sendBuff, strlen(sendBuff));
recBuff = atoi(sendBuff);


I'm assuming sendBuff is a char* or char[] of a certain size. You are reading into that buffer up to strlen(sendBuff) characters. So lets hope this is running in DEBUG mode you are reading characters into the buffer up to the length of the string that is currently held in the buffer. Unless you have set it elsewhere then the string currently in the buffer in Debug configuration should be zero characters long, meaning you are asking to read zero characters.

IF I have understood what you posted then I think you want something more like this.

    const size_t MaxSize = 16;
char sendBuffer[MaxSize];
{
recBuff = atoi(sendBuff);
}

• See update 1 but I have been using n to see if I'm at least reading the right amount of characters and it seems to be right but the actual value of those characters is not. Another person has said that it may because the string I'm sending is not null terminated and that is making atoi behave badly but when I tested it I had my printf statement right after the read and its incorrect immediately after. May 9 '16 at 17:02
1. Convert the int to a byte buffer
2. Send the byte buffer out via whatever medium (wireless/wired)
3. On recvive at the microcontroller end, convert the byte to int.

Eg.

int a = 2 bytes.
int b = 2 bytes.


char array[4] = to hold both the ints in byte array for wireless sending.

So your output array will have

array[0] = int a_1st_part
array[1] = int a_2nd_part
array[2] = int b_1st_part
array[3] = int b_2nd_part


Now send the output array out (via socket/wireless/uart).

When you receive do the opposite.

array[0] + array[1] //make up for int a.
array[2] + array[3] //make up for int b.