📄 testimagegrab.cpp
字号:
}
/*通过当前的事件得到板卡编号*/
nBoardIndex = FindBoardIndex(g_EventArrayForWait[nWaitEventIndex]);
nWaitEventIndex++;
nWaitEventIndex %= g_nSocknumAccepted;
WSAEnumNetworkEvents(g_sockRecvSock[nBoardIndex], g_EventArray[nBoardIndex], &g_NetworkEvents[nBoardIndex]);
WSAResetEvent(g_EventArray[nBoardIndex]);
if (g_NetworkEvents[nBoardIndex].lNetworkEvents & FD_READ)
{
if (nFileSize[nBoardIndex] == 0)
{
/*已经获得接收帧头的同步, nSyncFlag 等于100*/
if (nSyncFlag[nBoardIndex] == 100)
{
int nReceived = 0;
//尚未获得文件信息,处于接收帧头的阶段
if (nReceiveTotal[nBoardIndex] < 8)
{
/*进行数据的头接收*/
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex][0] + nReceiveTotal[nBoardIndex]), 8 - nReceiveTotal[nBoardIndex] ,0);
nReceiveTotal[nBoardIndex] += nReceived;
}
/*如果收齐了文件头,那么做文件长度的解读*/
if (nReceiveTotal[nBoardIndex] == 8)
{
/*纯数据的长度,不包括文件头和文件尾*/
nFileSize[nBoardIndex] = ((PICTUREINFO *)((unsigned int)&bufHead[nBoardIndex][0]))->m_nTotalSize;
/*判断帧头标志*/
if ( ((PICTUREINFO *)(u32)&bufHead[nBoardIndex][0])->strLogo[0] != 'c' )
{
/*导致程序进入失同步状态,复位状态标志*/
nFileSize[nBoardIndex] = 0;
nSyncFlag[nBoardIndex] = 0;
nReceiveTotal[nBoardIndex] = 0;
continue;
}
/*收完帧头,进行nReceiveTotal的复位*/
nReceiveTotal[nBoardIndex] = 0;
/*进行一次回应*/
//send(g_sockRecvSock[nBoardIndex],(char*)(unsigned int)&nReceiveTotal[nBoardIndex], 4, 0);
/*分配buffer,用来存图像数据*/
buf[nBoardIndex] = (char*)calloc((nFileSize[nBoardIndex] + 3)/4 ,4);
}
else /* End of if (nReceiveTotal == 8)*/
{
/*没有收齐帧头,那么继续收*/
continue;
} /* End of if (nReceiveTotal == 8) else .....*/
}
else /*End of if (nSyncFlag == 100) */
{
int nReceived = 0;
/*还没有获得接收帧头的同步*/
/*接收c*//*进入此分支,如果socket没有关闭,肯定有数据可以读,可以不考虑读的结果 <= 0的情况*/
if (nSyncFlag[nBoardIndex] == 0)
{
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex][0]), 1, 0);
if (( nReceived != 1) || ( *(char*)(u32)&bufHead[nBoardIndex][0] != 'c'))
{
/*继续收帧头标志'c'*/
nSyncFlag[nBoardIndex] = 0;
continue;
}
else
{
/*进入收帧头图像标志't'的阶段*/
nSyncFlag[nBoardIndex] = 1;
}
}
else if (nSyncFlag[nBoardIndex] == 1)
{
/*读取't'的分支*/
/*进入此分支,肯定有数据可以读,可以不考虑读的结果<= 0的情况*/
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex][0]), 1, 0);
if (( nReceived != 1) || ( *(char*)(u32)&bufHead[nBoardIndex][0] != 't'))
{
/*进行同步继续收帧头标志'c'*/
nSyncFlag[nBoardIndex] = 0;
continue;
}
else
{
/*进入收帧头图像标志'x'的阶段*/
nSyncFlag[nBoardIndex] = 2;
}
}
else if (nSyncFlag[nBoardIndex] == 2)
{
/*读取第一个'x'的分支*/
/*进入此分支,肯定有数据可以读,可以不考虑读的结果<= 0的情况*/
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex][0]), 1, 0);
if (( nReceived != 1) || ( *(char*)(u32)&bufHead[nBoardIndex][0] != 'x'))
{
/*进行同步继续收帧头标志'c'*/
nSyncFlag[nBoardIndex] = 0;
continue;
}
else
{
/*进入收帧头图像标志'x'的阶段*/
nSyncFlag[nBoardIndex] = 3;
}
}
else if (nSyncFlag[nBoardIndex] == 3)
{
/*读取第一个'x'的分支*/
/*进入此分支,肯定有数据可以读,可以不考虑读的结果<= 0的情况*/
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex][0]), 1, 0);
if (( nReceived != 1) || ( *(char*)(u32)&bufHead[nBoardIndex][0] != 'x'))
{
/*进行同步继续收帧头标志'c'*/
nSyncFlag[nBoardIndex] = 0;
continue;
}
else
{
/*进入收图像帧长度的阶段*/
nSyncFlag[nBoardIndex] = 4;
}
}
else if (nSyncFlag[nBoardIndex] == 4)
{
/*收数据长度的阶段*/
//nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex]), 1, 0);
//尚未获得文件信息,处于接收帧头的阶段
if (nReceiveTotal[nBoardIndex] < 4)
{
/*进行数据的头接收, 如果长度不够,继续长度的接收*/
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)&bufHead[nBoardIndex][0] + nReceiveTotal[nBoardIndex]), 4 - nReceiveTotal[nBoardIndex] ,0);
nReceiveTotal[nBoardIndex] += nReceived;
}
if ( nReceiveTotal[nBoardIndex] == 4)
{
nFileSize[nBoardIndex] = *(u32*)(u32)&bufHead[nBoardIndex][0];
printf("nFileSize[%d] = %d\n", nBoardIndex,nFileSize[nBoardIndex]);
if (nFileSize[nBoardIndex] > 0x100000)
{
/*进入失同步 状态*/
nSyncFlag[nBoardIndex] = 0;
nFileSize[nBoardIndex] = 0;
nReceiveTotal[nBoardIndex] = 0;
continue;
}
/*nReceiveTotal复位*/
nReceiveTotal[nBoardIndex] = 0;
/*设置同步标志*/
nSyncFlag[nBoardIndex] = 100;
/*进行一次回应*/
//send(g_sockRecvSock[nBoardIndex],(char*)(unsigned int)&nReceiveTotal[nBoardIndex], 4, 0);
/*分配buffer,用来存图像数据*/
buf[nBoardIndex] = (char*)calloc((nFileSize[nBoardIndex] + 3)/4 ,4);
}
}
else
{
/*nSyncFlag != 0,1,2,3,4, 100*/
/*for later handle*/
}
}/*End of if (nSyncFlag == 100) else ......*/
}
else if ((nFileSize[nBoardIndex] -nReceiveTotal[nBoardIndex] ) >= 1024)
{
int nReceived = 0;
nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)buf[nBoardIndex] + nReceiveTotal[nBoardIndex]),1024,0);
/*Recv total 计数加*/
nReceiveTotal[nBoardIndex]+=nReceived;
/*max Byte用来进行回应的计数*/
nMaxByte[nBoardIndex]+=nReceived;
while(nMaxByte[nBoardIndex] >= 1024)
{
//send(g_sockRecvSock[nBoardIndex],(char*)(unsigned int)&buf[nBoardIndex],4,0);
nMaxByte[nBoardIndex] -= 1024;
}
if (nFileSize[nBoardIndex] == nReceiveTotal[nBoardIndex])
{
//if (nMaxByte[nBoardIndex] > 0)
//{
send(g_sockRecvSock[nBoardIndex],(char*)(unsigned int)buf[nBoardIndex],4,0);
//}
// 如果数据全部接受到
/*收到数据往buffer里放指针*/
g_u32BufferLen[nBoardIndex][g_u32WriteIndex[nBoardIndex]] = nFileSize[nBoardIndex];
g_u32BufferPointer[nBoardIndex][g_u32WriteIndex[nBoardIndex]] = (u32)buf[nBoardIndex];
g_u32WriteIndex[nBoardIndex]++;
g_u32WriteIndex[nBoardIndex] %= IMAGE_CAMERA_ONE_PATH_BUFS;
ReleaseSemaphore(g_handleSemphore2WriteThread[nBoardIndex], 1, NULL);
nFileSize[nBoardIndex] = 0;
nReceiveTotal[nBoardIndex] = 0;
/*收另外一路数据*/
//nWaitEventIndex++;
//nWaitEventIndex %= g_nSocknumAccepted;
}
}
else if ((nFileSize[nBoardIndex] -nReceiveTotal[nBoardIndex])>0)
{
int nReceived = recv(g_sockRecvSock[nBoardIndex],(char*)((unsigned int)buf[nBoardIndex] + nReceiveTotal[nBoardIndex]),nFileSize[nBoardIndex] - nReceiveTotal[nBoardIndex] ,0);
//printf("(nFileSize-nReceiveTotal)>0 :\n nReceived=%d : nReceiveTotal=%d : nFileSize=%d\n",
//nReceived,nReceiveTotal,nFileSize);
//printf("Last error == 0x%x\n", WSAGetLastError());
nReceiveTotal[nBoardIndex] +=nReceived;
nMaxByte[nBoardIndex] += nReceived;
while(nMaxByte[nBoardIndex] >= 1024)
{
//send(g_sockRecvSock[nBoardIndex],(char*)(unsigned int)&buf[nBoardIndex],4,0);
nMaxByte[nBoardIndex] -= 1024;
}
if (nFileSize[nBoardIndex] == nReceiveTotal[nBoardIndex])
{
//if (nMaxByte[nBoardIndex] > 0)
//{
send(g_sockRecvSock[nBoardIndex],(char*)(unsigned int)buf[nBoardIndex],4,0);
//}
/*收到数据往buffer里放指针*/
g_u32BufferLen[nBoardIndex][g_u32WriteIndex[nBoardIndex]] = nFileSize[nBoardIndex];
g_u32BufferPointer[nBoardIndex][g_u32WriteIndex[nBoardIndex]] = (u32)buf[nBoardIndex];
g_u32WriteIndex[nBoardIndex]++;
g_u32WriteIndex[nBoardIndex] %= IMAGE_CAMERA_ONE_PATH_BUFS;
ReleaseSemaphore(g_handleSemphore2WriteThread[nBoardIndex], 1, NULL);
nFileSize[nBoardIndex] = 0;
nReceiveTotal[nBoardIndex] = 0;
/*收另外一路数据*/
//nWaitEventIndex++;
//nWaitEventIndex %= g_nSocknumAccepted;
}
}
}
if (g_NetworkEvents[nBoardIndex].lNetworkEvents & FD_CLOSE)
{
if (closesocket(g_sockRecvSock[nBoardIndex])==0)
{
printf("%d %d\n",nReceiveTotal,nMaxByte);
printf("close socket\n");
}
else
{
printf("Close socket failed\n");
}
WSACloseEvent(g_EventArray[nBoardIndex]);
g_EventArray[nBoardIndex] = NULL;
g_nSocknumAccepted--;
if (g_nSocknumAccepted == 0)
{
break;
}
}
}
#endif
#ifdef DEBUG_PRINT_OUT
printf("End receive socket!\n");
#endif
_endthread();
}
void MonitorImagesSocketUDP(void *arg)
{
DWORD dwCommand = 0;
STRU_IMAGE_DOWN_MACHINE_STAT struRecvUdpCommand;
WSAEVENT EventArray[1];
WSANETWORKEVENTS NetworkEvents;
SOCKET RecvSocket = WSASocket(AF_INET,
SOCK_DGRAM,
IPPROTO_UDP,
NULL,
0,
WSA_FLAG_OVERLAPPED);
if (INVALID_SOCKET == RecvSocket)
{
printf("Could not create socket,%x\n",WSAGetLastError());
_endthread();
}
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = ADDR_ANY;
service.sin_port = htons(4100);
//----------------------
// Bind the socket.
if (bind( RecvSocket, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) {
printf("bind() failed.\n");
closesocket(RecvSocket);
_endthread();
}
//-------------------------
// Create new event
EventArray[0] = WSACreateEvent();
//-------------------------
// Associate event types FD_ACCEPT and FD_CLOSE
// with the listening socket and NewEvent
WSAEventSelect( RecvSocket, EventArray[0], FD_READ);
sockaddr_in DeviceAddr;
int sizeDeviceAddr=sizeof(DeviceAddr);
//----------------------
// Accept the connection.
while(1) {
if (WSAWaitForMultipleEvents(1,EventArray,TRUE,1000000,FALSE)==WAIT_TIMEOUT)
{
printf("timeout\n");
//break;
}
else
{
WSAEnumNetworkEvents(RecvSocket,EventArray[0],&NetworkEvents);
WSAResetEvent(EventArray[0]);
if (NetworkEvents.lNetworkEvents&FD_READ)
{
recvfrom(RecvSocket,(char *)&struRecvUdpCommand,sizeof(STRU_IMAGE_DOWN_MACHINE_STAT),0,(SOCKADDR *)&DeviceAddr,&sizeDeviceAddr);
//printf("\n%d\n",struRecvUdpCommand.struCmdHeader.u32CmdType);
if (struRecvUdpCommand.struCmdHeader.u32CmdType == 11)
{
printf("\nDeviceAddr = %d.%d.%d.%d \n",DeviceAddr.sin_addr.S_un.S_un_b.s_b1,
DeviceAddr.sin_addr.S_un.S_un_b.s_b2,
DeviceAddr.sin_addr.S_un.S_un_b.s_b3,
DeviceAddr.sin_addr.S_un.S_un_b.s_b4);
printf("struRecvUdpCommand.u32FpgaRecvPicNum = %d\n ", struRecvUdpCommand.u32FpgaRecvPicNum);
printf("struRecvUdpCommand.u32Write2BufNum = %d\n ", struRecvUdpCommand.u32Write2BufNum);
printf("struRecvUdpCommand.u32SockSendPicNum = %d\n ", struRecvUdpCommand.u32SockSendPicNum);
printf("struRecvUdpCommand.u32ProcessingFullBufNum = %d\n ", struRecvUdpCommand.u32ProcessingFullBufNum);
printf("struRecvUdpCommand.u32DiscardFpgaFrameNum = %d\n ", struRecvUdpCommand.u32DiscardFpgaFrameNum);
printf("struRecvUdpCommand.u32BuferFulTimes = %d\n ", struRecvUdpCommand.u32BuferFulTimes);
}
}
}
} // while(1)
printf("OK\n");
closesocket(RecvSocket);
_endthread();
}
void GrabImagesContinuous(void *arg)
{
ZXG_COMMAND cmdPacket;
SOCKET SendSocket;
sockaddr_in RecvAddr;
int Port = 0;
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (INVALID_SOCKET == SendSocket)
{
printf("Could not create socket,%x\n",WSAGetLastError());
}
for (int i=0;i<1000;i++)
{
cmdPacket.u32CmdType = 10;
cmdPacket.u32ExposeTime = 500;
cmdPacket.u32Gain = 400;
//---------------------------------------------
// Set up the RecvAddr structure with the IP address of
// the receiver (in this example case "123.456.789.1")
// and the specified port number.
Port = 4099;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("169.254.0.13");
//---------------------------------------------
// Send a datagram to the receiver
printf("Sending a datagram to the receiver...\n");
sendto(SendSocket,
(char *)&cmdPacket,
sizeof(cmdPacket),
0,
(SOCKADDR *) &RecvAddr,
sizeof(RecvAddr));
//---------------------------------------------
// When the application is finished sending, close the socket.
Sleep(20);
}
closesocket(SendSocket);
}
/*
Param *pParam=(Param *)arg;
SOCKET socket = pParam->s;
WSAEVENT EventArray[1];
WSANETWORKEVENTS NetworkEvents;
int nReceiveTotal = 0;
int nReceiveType = 0;
int nMaxByte = 0;
int nFileSize = 0;
int nReceiveBytes = 0;
FILE *f=NULL;
int nFileIndex = 1;
char strfilename[200];
char strdirectory[200];
int i=0;
int nCommandType = 0;
DWORD dwFileSize=0;
EventArray[0] = WSACreateEvent();
if (WSAEventSelect( socket, EventArray[0], FD_READ | FD_CLOSE)!=0)
{
printf("Event Select failed\n");
}
while (1)
{
if (WSAWaitForMultipleEvents(1,EventArray,TRUE,10000,FALSE)==WAIT_TIMEOUT)
{
continue;
}
WSAEnumNetworkEvents(socket,EventArray[0],&NetworkEvents);
// 清除事件
WSAResetEvent(EventArray[0]);
if (NetworkEvents.lNetworkEvents&FD_READ)
{
char buf[1000000];
DWORD ack=0;
// 尚未获得文件信息
if (nCommandType ==0)
{
char strFlag[4];
int nReceived = recv(socket,strFlag,3,0);
strFlag[3] ='\0';
// 取得图像标志"ctx"
if ((nReceived==3)&&(strcmp(strFlag,"ctx")==0))
{
//send(socket,(char *)&ack,sizeof(DWORD),0);
//printf("\nctx\n");
nCommandType = 1;
}
else
{
WSAResetEvent(EventArray[0]);
//printf("\n%s\n",strFlag);
continue;
}
}
else if (nCommandType == 1)
{
// 取得图像大小
int nReceived = recv(socket,(char *)&dwFileSize,sizeof(DWORD),0);
if ((dwFileSize<1000000)&&(nReceived==sizeof(DWORD)))
{
//send(socket,(char *)&ack,sizeof(DWORD),0);
//printf("\nSize=%d\n",dwFileSize);
nCommandType = 2;
}
else
{
WSAResetEvent(EventArray[0]);
//printf("\nError nReceived=%d\n",nReceived);
continue;
}
}
else if (nCommandType == 2)
{
// 取得图像数据
int nReceived = 0;
int nTotalReceived = 0;
while (nTotalReceived<dwFileSize)
{
nReceived = recv(socket,buf+nTotalReceived,dwFileSize-nTotalReceived,0);
if (nReceived==-1) continue;
nTotalReceived+=nReceived;
//printf("\nnReceived=%d nTotalReceived=%d\n",nReceived,nTotalReceived);
}
//send(socket,(char *)&ack,sizeof(DWORD),0);
//printf("Received %d BYTES total.\n",nTotalReceived);
sprintf(strfilename,"d:\\data\\%d-%d.jpg",pParam->nIndex,nFileIndex++);
FILE *f = fopen(strfilename,"wb");
if (f)
{
fwrite(buf,dwFileSize,1,f);
fclose(f);
}
nCommandType = 0;
}
} // if (NetworkEvents.lNetworkEvents&FD_READ)
if (NetworkEvents.lNetworkEvents&FD_CLOSE)
{
if (closesocket(socket)==0)
{
printf("%d %d\n",nReceiveTotal,nMaxByte);
printf("close socket\n");
}
else
{
printf("Close socket failed\n");
}
WSACloseEvent(EventArray[0]);
delete pParam;
_endthread();
}
}
delete pParam;
_endthread();
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -