int WriteCommand(uint16_t Addr, uint16_t Value) { extern uint64_t MessageNumber; extern struct MessagePortType MessageOutPortList[]; extern struct MessagePortType MessageInPortList[]; uint64_t TSC; struct ElekMessageType Message; rdtscll(TSC); Message.MsgID=MessageNumber++; Message.MsgTime=TSC; Message.MsgType=MSG_TYPE_WRITE_DATA; Message.Addr=Addr; Message.Value=Value; SendUDPData(&MessageOutPortList[ELEK_ELEKIO_OUT],sizeof(struct ElekMessageType), &Message); // printf("ID: %d send req on %4x wait for data....", Message.MsgID, Addr); RecieveUDPData(&MessageInPortList[ELEK_ELEKIO_IN], sizeof(struct ElekMessageType), &Message); // printf("ID: %d %4x %d\n",Message.MsgID,Message.Value, Message.MsgType); return(Message.Value); } /* WriteCommand */
int ReadCommand(uint16_t Addr) { extern uint64_t MessageNumber; extern struct MessagePortType MessageOutPortList[]; extern struct MessagePortType MessageInPortList[]; uint64_t TSC; struct ElekMessageType Message; rdtscll(TSC); Message.MsgID=MessageNumber++; Message.MsgTime=TSC; Message.MsgType=MSG_TYPE_READ_DATA; Message.Addr=Addr; Message.Value=0; SendUDPData(&MessageOutPortList[ELEK_ELEKIO_OUT],sizeof(struct ElekMessageType), &Message); // printf("send req on %4x wait for data....", Addr); RecieveUDPData(&MessageInPortList[ELEK_ELEKIO_IN], sizeof(struct ElekMessageType), &Message); // printf("got %4x\n",Message.Value); return(Message.Value); } /* ReadCommand */
// Send Message to ElekIOServ to move a given mirror axis to a given position int MirrorGoTo(int Mirror, int Axis, int32_t Position) { extern uint64_t MessageNumber; extern struct MessagePortType MessageOutPortList[]; extern struct MessagePortType MessageInPortList[]; uint64_t TSC; struct ElekMessageType Message; #ifdef RUNONPC rdtscll(TSC); #endif Message.MsgID=MessageNumber++; Message.MsgTime=TSC; Message.MsgType=MSG_TYPE_MIRROR_MOVE; Message.Addr=(Mirror << 8) + (Axis & 0x00FF); Message.Value=Position; SendUDPData(&MessageOutPortList[ELEK_ELEKIO_OUT],sizeof(struct ElekMessageType), &Message); // printf("ID: %d send req on %4x wait for data....", Message.MsgID, Addr); RecieveUDPData(&MessageInPortList[ELEK_ELEKIO_IN], sizeof(struct ElekMessageType), &Message); // printf("ID: %d %4x %d\n",Message.MsgID,Message.Value, Message.MsgType); // return(Message.Status); }
// send given Message to ElekIOServ int SetStatusCommand(uint16_t MsgType, uint16_t Addr, uint64_t Value) { extern uint64_t MessageNumber; extern struct MessagePortType MessageOutPortList[]; extern struct MessagePortType MessageInPortList[]; uint64_t TSC; struct ElekMessageType Message; #ifdef RUNONPC rdtscll(TSC); #endif Message.MsgID=MessageNumber++; Message.MsgTime=TSC; Message.MsgType=MsgType; Message.Addr=Addr; Message.Value=Value; SendUDPData(&MessageOutPortList[ELEK_ELEKIO_OUT],sizeof(struct ElekMessageType), &Message); //printf("ID: %d send %d on %4x wait for data....", Message.MsgID, Value, Addr); RecieveUDPData(&MessageInPortList[ELEK_ELEKIO_IN], sizeof(struct ElekMessageType), &Message); //printf("ID: %d %4x %d\n",Message.MsgID,Message.Value, Message.MsgType); return(Message.Value); }
int ResetDev(Communicator& comm,int macNo /*=255*/) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0xe8; int dataLen=0; BYTE tempBuffer[16]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode=0; // CommProcess(comm,SRBuffer,packetLen,retCode,SRBuffer,recLen); if(COMM_UDP==comm.m_nCommtype) retCode=SendUDPData(comm.udpConn->s,comm.udpConn->dstIP,comm.udpConn->dstPort, SRBuffer,packetLen); else if(COMM_COM==comm.m_nCommtype) retCode=SendBytes(comm.comConn->comPort,SRBuffer,packetLen); else if(COMM_TCPCLIENT==comm.m_nCommtype||COMM_TCPSERVER==comm.m_nCommtype) retCode=SendData(comm.tcpClientConn->s,SRBuffer,packetLen); if(retCode<0) return -1; return 0; }
//处理通信 void CommProcess(Communicator&comm,BYTE* buf,int bufLen,int nSendLen,int& sendRetCode,int& recLen) { int j=0; BYTE sendBuf[1024]; memcpy(sendBuf,buf,nSendLen); if(comm.m_nCommtype==COMM_UDP) { j=0;//如果通信不成功,试3次 int index=comm.csIndex; do{ j++; if(j>3) break; while(true) //控制在同一时间向同一工作站发送数据的线程不大于8个 { EnterCriticalSection(&Connections::udpCommManagerVector[index].cs_workstation); if(Connections::udpCommManagerVector[index].sendingThreadNum+1<=8) { Connections::udpCommManagerVector[index].sendingThreadNum++; LeaveCriticalSection(&Connections::udpCommManagerVector[index].cs_workstation); break; } LeaveCriticalSection(&Connections::udpCommManagerVector[index].cs_workstation); Sleep(5); } EnterCriticalSection(&comm.udpConn->cs_rw); sendRetCode=SendUDPData(comm.udpConn->s,comm.udpConn->dstIP,comm.udpConn->dstPort, sendBuf,nSendLen); Sleep(1); recLen=RecvUDPData(comm.udpConn->s,comm.udpConn->waitSTX2Time,comm.udpConn->normalWaitTime ,buf,bufLen); LeaveCriticalSection(&comm.udpConn->cs_rw); EnterCriticalSection(&Connections::udpCommManagerVector[index].cs_workstation); Connections::udpCommManagerVector[index].sendingThreadNum--; LeaveCriticalSection(&Connections::udpCommManagerVector[index].cs_workstation); //如果数据接收格式错误,清除接收缓冲 if(recLen==-9||recLen==-10) { int temp=0; do{ temp=RecvUDPData(comm.udpConn->s,10,10,buf,bufLen); }while(temp!=-4); } }while(recLen<0); return; } else if(comm.m_nCommtype==COMM_TCPCLIENT||comm.m_nCommtype==COMM_TCPSERVER) { //对socket加锁 sendRetCode=SendData(comm.tcpClientConn->s,buf,nSendLen); recLen=ReceiveData(comm.tcpClientConn->s,comm.tcpClientConn->waitSTX2Time,comm.tcpClientConn->normalWaitTime ,buf); } else if(comm.m_nCommtype==COMM_COM) { j=0; do { j++; if(j>3) break; EnterCriticalSection(&comm.comConn->cs_rw); sendRetCode=SendBytes(comm.comConn->comPort,sendBuf,nSendLen); Sleep(1); recLen=ReceiveBytes(comm.comConn->comPort,comm.comConn->waitSTX2Time,comm.comConn->normalWaitTime ,buf); if(-2==recLen||-9==recLen) { int temp=0; do { temp=ReceiveBytes(comm.comConn->comPort,100,100,buf); } while (temp!=-4&&temp!=-6); } LeaveCriticalSection(&comm.comConn->cs_rw); } while (recLen<0); } }