我用C ++编写程序,与没有提供GUI的DLP Lightcrafter(德州仪器)建立通信,我已经使用这些指令建立连接:
#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() for 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 to Establish Connection */
if (connect(s,(sockaddr*) &sa,sizeof(sa))!=-1){
cout<<"Connected."<<endl;
cout<<" "<<endl;
usleep(1000000);
/* #################################### */
/* Request to 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 from 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 from 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;
}
当我尝试连接到主板时,它成功了,但是当我发送命令时,我只会收到错误。我无法在发送命令或命令本身的方式中发现错误,因为我并不真正理解套接字的工作方式,我想也许就是这样。这个问题......有什么想法吗?
由于
编辑:
我编辑了代码以包含一组指令的示例。
与具有8位组件的DLP向量通信被发送。
第一个位置定义它是什么类型的数据包;
第二和第三位置是要给出的命令的MSB和LSB;
第四个位置是一个标志,告诉数据包中的数据是否完整,或者是否要用其他数据包中的数据进行编译;
Fith和第六个位置是数据长度的LSB和MSB;
第七至第N位置用于数据;
(N + 1)th用于校验和[(sum through the N elements)&0xFF]
。
我知道我收到错误,因为当我收到DLP的响应时,数据包类型为1(错误),并且数据与文档中提供的错误代码一致。
编辑2: 问题解决了,问题与变量类型有关,而不是套接字。