c++ - Application of socket() from sys/socket.h -


i'm writing program in c++ establish communication dlp lightcrafter (texas instruments) without provided gui, , i've used these instructions establish connection:

#include <stdlib.h> #include <string.h> #include <iostream> /* cout */ #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> /* socket struct */ #include <arpa/inet.h> /* ip, etc */ #include <unistd.h> /* read() write() socket */ #include <cstdint> #include "pkt.h" /* calcchecksum() */  using namespace std;  /* packet format  * ------------------------------------------------------------------------------------  * |                        header                       |       data      | checksum |  * ------------------------------------------------------------------------------------  * | byte 0 | byte 1 | byte 2 | byte 3 | byte 4 | byte 5 | byte 6...byte n | byte n+1 |  * ------------------------------------------------------------------------------------  * |pkt type| cmd 1  | cmd 2  | flags  | payload length  |  data payload   | checksum |  * ------------------------------------------------------------------------------------ */  int main () {   enum type{busy, error, write, write_resp, read, read_resp};   enum flag{single, first, mid, last};   enum curdispmode{static,internal_pat,hdmi,reserved,pattern_seq};   enum curtestpat{checkerboard_14x8,solid_black,solid_white,solid_green,solid_blue,solid_red,vert_lines_1w7b,horiz_lines_1w7b,vert_lines_1w1b,horiz_lines_1w1b,diag_lines,vert_gray_ramps,horiz_gray_ramps,checkerboard_4x4};   enum pkt_size{header=6, max=0xffff, check=1};    char ip[] = "192.168.1.100";   int *pkt=new int [header+max+check];   unsigned int data_length, pkt_length;   unsigned int cmd_curdispmode=0x0101;    unsigned int cmd_curtestpat=0x0103;    /* socket structure */       struct sockaddr_in sa;   sa.sin_family=af_inet;   sa.sin_addr.s_addr=inet_addr(ip);   sa.sin_port = htons(0x5555);   int s = socket(af_inet,sock_stream,0);    /* attempt establish connection */   if (connect(s,(sockaddr*) &sa,sizeof(sa))!=-1){     cout<<"connected."<<endl;     cout<<" "<<endl;      usleep(1000000);      /* #################################### */     /* request display solid blue screen */     /* #################################### */        data_length=1;      if(data_length>max)       return -1;      /* first instruction - set test pattern mode */     pkt[0]=write;     pkt[1]=(cmd_curdispmode >> 8) & 0xff;     pkt[2]=(cmd_curdispmode) & 0xff;     pkt[3]=single;     pkt[4]=data_length & 0xff;     pkt[5]=(data_length >> 8) & 0xff;     pkt[6]=internal_pat;     pkt[header+data_length]=calcchecksum(header+data_length, pkt);      pkt_length=header+data_length+check;      if(send(s,(const void *)pkt,(size_t) pkt_length,0) != pkt_length){       cout<<"pkt writing failed."<<endl;       return -1;}     cout<<pkt[0]<<" "<<pkt[1]<<" "<<pkt[2]<<" "<<pkt[3]<<" "<<pkt[4]<<" "<<pkt[5]<<" "<<pkt[6]<<" "<<pkt[7]<<endl;      /* retrieves answer dlp */     if(recv(s,(void *)pkt,(size_t)header,0) != header){       cout<<"cmd reading failed."<<endl;       return -1;}      /* new data length */     data_length = pkt[4] | pkt[5] << 8;      /* retrieves data & checksum */     if(recv(s,(void *)(pkt+header),(size_t)(data_length+1),0) != data_length+1){       cout<<"payload reading failed."<<endl;       return -1;}     cout<<pkt[0]<<" "<<pkt[1]<<" "<<pkt[2]<<" "<<pkt[3]<<" "<<pkt[4]<<" "<<pkt[5]<<" "<<pkt[6]<<" "<<pkt[7]<<endl;      /* checksum */     if(pkt[data_length+header] != calcchecksum(data_length+header,pkt)){       cout<<"checksum error"<<endl;       return -1;}      data_length=1;     /* second instruction - display pattern 0x05 */     pkt[0]=write;     pkt[1]=(cmd_curtestpat >> 8) & 0xff;     pkt[2]=(cmd_curtestpat) & 0xff;     pkt[3]=single;     pkt[4]=data_length & 0xff;     pkt[5]=(data_length >> 8) & 0xff;     pkt[6]=solid_blue;     pkt[header+data_length]=calcchecksum(header+data_length, pkt);      if(send(s,(const void  *)pkt,(size_t)pkt_length,0) != pkt_length){       cout<<"pkt writing failed."<<endl;       return -1;}      /* retrieves answer dlp */     if(recv(s,(void *)pkt,(size_t)header,0) != header){       cout<<"cmd reading failed."<<endl;       return -1;}      /* new data length */     data_length = pkt[4] | pkt[5] << 8;      /* retrieves data */     if(recv(s,(void *)(pkt+header),(size_t)(data_length+1),0) != data_length+1){       cout<<"payload reading failed."<<endl;       return -1;}     cout<<pkt[header]<<" "<<pkt[header+1]<<endl;      /* checksum */     if(pkt[data_length+header] != calcchecksum(data_length+header,pkt)){       cout<<"checksum error"<<endl;       return -1;}      if(pkt[0] != write_resp){       cout<<"cmd failed."<<endl;       return -1;}      if(pkt[3]==last)       cout<<"cmd success."<<endl;     /* ############## */     /* end of request */     /* ############## */      usleep(9000000);      if(!close(s))       cout<<"disconnected."<<endl;   }    else     cout<<"connection failed."<<endl;    delete [] pkt;    return 0;  } 

when try connect board it's successful when send commands error returned. can't find mistakes in way i'm sending commands nor in commands themselves, , since i'm don't understand way socket works thought maybe that's issue...any thoughts?

thanks

edit:

i have edited code include example of set of instructions. communicate dlp vectors 8bit components sent. first position defines type of packet is; second , third position msb , lsb of command given; fourth position flag tells wether data in packet complete or if it's compiled data other packets; fith , sixth positions lsb , msb of length of data; seventh through nth positions data; (n+1)th checksum [(sum through n elements)&0xff].

i know i'm getting error because when response dlp packet type 1 (error), , data coherent error codes available on documentation.

edit 2: issue solved, problem related variable types , not socket.


Comments

Popular posts from this blog

Add new key value to json node in java -

javascript - Highcharts Synchronized charts with missing data points -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -