int read_nonblock( int fd, void *buffer, size_t nbytes ) { size_t i; char *buf; int err; buf = (char*)buffer; for( i = 0; i < nbytes; i++, buf++ ) { // Stop reading if we would block if( !can_recv() ) break; // Get the next byte err = recv_byte( buf ); if( err != 0 ) return err; // Exit when the line ends if( *buf == '\n' || *buf == '\r' || *buf == 0x04 || *buf == 0x1A ) { i += 1; *(++buf) = 0; break; } } if( i == 0 ) { errno = EAGAIN; return -1; } else { return i; } }
/** * Receive gresb realization */ int ReceiveGresb::receive(int interfaceNum, ECI_CTRL_MESSAGE *msg) { int ff, rtr, dlc, id; char buf[8]; // one = 1; /** * FIXME: For clear warning ( interfaceNum ) not used */ interfaceNum = 0; interfaceNum++; int len = can_recv(sock, &ff, &rtr, &id, &dlc, (unsigned char*)&buf[0]); if (len < 0) { qDebug()<<"can_recv"; return -1; } else if (len > 0 ) { msg->u.sCanMessage.u.V0.uMsgInfo.Bits.dlc = dlc; msg->u.sCanMessage.u.V0.abData[0] = buf[0]; msg->u.sCanMessage.u.V0.abData[1] = buf[1]; msg->u.sCanMessage.u.V0.abData[2] = buf[2]; msg->u.sCanMessage.u.V0.abData[3] = buf[3]; msg->u.sCanMessage.u.V0.abData[4] = buf[4]; msg->u.sCanMessage.u.V0.abData[5] = buf[5]; msg->u.sCanMessage.u.V0.abData[6] = buf[6]; msg->u.sCanMessage.u.V0.abData[7] = buf[7]; msg->u.sCanMessage.u.V0.dwMsgId = id; } return 0; }
void can_msg_lookup(void) { short i; can_event_msg_t canMsg; if(can_recv(1, &canMsg) == 0) { // Dispatch the CAN message to the corresponding handler. // NOTE : Suppose there is only one handler per CAN id. printf("msgId= 0x%04x",canMsg.id); for(i = 0; handlers[i].handler != NULL; ++i) { if(handlers[i].canId == canMsg.id) { handlers[i].handler(canMsg); //printf("Message received and node found\n"); break; } } } }
int XSocket::recv_n(void* buf, int len, int timeout_ms) { if (m_sock == INVALID_SOCKET) return -1; if ((buf == NULL) || len <= 0) return 0; int recvsize = 0; do { if (!can_recv(timeout_ms)) return recvsize; int ret = this->recv((char *)buf + recvsize, len - recvsize); if (ret < 0) { if (recvsize > 0) return recvsize; else return -1; } recvsize += ret; } while(recvsize < len); assert(recvsize == len); return recvsize; }
void CFsAdapter::lookupForCanMessages() { can_event_msg_t msg; #ifdef DEBUG CLogger::log(CLogger::LINFO) << "Lookup for CAN messages..." << std::endl; unsigned int msgCnt = 0; #endif while(can_recv(1, &msg) == 0) { // empty the input queue. #ifdef DEBUG CLogger::log(CLogger::LINFO) << "CAN message received with id " << msg.id << "." << std::endl; msgCnt++; #endif for(std::list<CAbstractOffsetFamily*>::iterator it = m_lstFamilies.begin(); it != m_lstFamilies.end(); it++) { if((*it)->matchesCanId(msg.id)) { #ifdef DEBUG std::cout << "Family found." << std::endl; #endif (*it)->setValueFromCanMessage(msg); // Because Id <-> Family link is unique. break; } } } #ifdef DEBUG std::cout << "No more messages (" << msgCnt << " messages treated)." << std::endl; #endif }
int main() { uint8_t do_lpm, is_ext; int i, j, k; uint32_t msgid; WDTCTL = WDTPW | WDTHOLD; DCOCTL = CALDCO_16MHZ; BCSCTL1 = CALBC1_16MHZ; BCSCTL2 = DIVS_1; BCSCTL3 = LFXT1S_2; while (BCSCTL3 & LFXT1OF) ; P1SEL &= ~BIT0; P1SEL2 &= ~BIT0; P1DIR |= BIT0; P1OUT &= ~BIT0; sleep_counter = SLEEP_COUNTER; inbuf[8] = '\0'; can_init(); if (can_speed(500000, 1, 3) < 0) { P1OUT |= BIT0; LPM4; } // LCD init P2SEL &= ~(BIT0 | BIT5); P2SEL2 &= ~(BIT0 | BIT5); P2DIR |= BIT0 | BIT5; P2OUT |= BIT0 | BIT5; msp1202_init(); msp1202_puts("CAN printer\n0x00000080-\n"); can_rx_setmask(0, 0xFFFFFFFF, 1); can_rx_setmask(1, 0xFFFFFFFF, 1); can_rx_setfilter(0, 0, 0x00000080); can_rx_mode(0, MCP2515_RXB0CTRL_MODE_RECV_STD_OR_EXT); can_ioctl(MCP2515_OPTION_LOOPBACK, 0); can_ioctl(MCP2515_OPTION_ONESHOT, 0); can_ioctl(MCP2515_OPTION_ROLLOVER, 1); /* Flowchart for program flow: * * START * | * +---------<-------------------+-----------<-----------------------+ * | | | * \|/ /|\ | * -------------- | | * / \ +------+ | | * < Is IRQ pending? > No->---| LPM4 |->--+ | * \ / +------+ /|\ * ------------- | * Yes | * | | * \_/ | * ----------- ------------- | * / \ / \ | * < Is IRQ_ERROR? > Yes->--< Is IRQ_HANDLED? > Yes->-------------------------+ * \ / \ / | * ----------- ------------- | * No No | * | | /|\ * \_/ \_/ | * -------- -------- +-----------------+ | * / \ / \ | Cancel TX using | | * < Is IRQ_RX? > No->---->---< Is IRQ_TX? > Yes->--| can_tx_cancel() |->----+ * \ / \ / +-----------------+ | * -------- -------- | * Yes No | * | | /|\ * \_/ \_/ | * +----------------+ +-------------------+ | * | Pull data | | Bus error, wait | | * | using can_recv | | 100ms and recheck |->---------------------------+ * +----------------+ | IRQ. | | * | +-------------------+ | * \_/ | * +--------------+ /|\ * | Write buffer | | * | contents to | | * | LCD using |->--------------------------->---------------------------+ * | msp1202_puts | * +--------------+ * * * */ while(1) { do_lpm = 1; if (mcp2515_irq) { irq = can_irq_handler(); if (irq & MCP2515_IRQ_ERROR) { if ( !(irq & MCP2515_IRQ_HANDLED) ) { if (irq & MCP2515_IRQ_TX) { can_tx_cancel(); } else { // Bus error; wait 100ms, recheck P1OUT |= BIT0; __delay_cycles(1600000); P1OUT &= ~BIT0; do_lpm = 0; } } else { // RX overflow, most likely msp1202_puts("RX OVERFLOW\n"); } } if (irq & MCP2515_IRQ_RX) { j = 0; k = can_rx_pending(); do { i = can_recv(&msgid, &is_ext, inbuf+j); if ( !(i & 0x40) ) { j += i; } //msp1202_putc(k + '0', 0); msp1202_putc(' ', 0); } while ( (k = can_rx_pending()) >= 0 ); //inbuf[j++] = '\n'; inbuf[j] = '\0'; inbuf[j] = '\0'; msp1202_puts((char*)inbuf); //msp1202_putc('\n', 1); } else { if (irq & MCP2515_IRQ_TX) { can_tx_cancel(); } } } if ( do_lpm && !(mcp2515_irq & MCP2515_IRQ_FLAGGED) ) { LPM4; } } return 0; }
void handleP2PAndBroadcastMode(int socket_can, int which, u8 *can_control) { int ret,len; T_LinkedCanFrame *linkedCanFrame; T_LinkedCanFrame canFrames[128]; struct can_frame frame; struct can_frame frame_recv; unsigned int head; u8 dev_type; u8 can_id; u8 mode; int i; int high,low = 0; int isWaveFinished = 0; u8 cmd_type; u8 poll_time; char buf[1024]; char file_name[128] = {0}; struct timeval time; fd_set fd_read; //can设备列表 u8 (*dev_list)[A3_LEN]; //波形数据长度 static int waveDataLen = 0; //波形类型 static u8 waveDataType; mode = can_control[0]; can_id = can_control[1]; cmd_type = can_control[2]; poll_time = can_control[3]; /* 每个can_id对应一个dev_type,对应表存储在全局二维数组中; 如果是广播模式,dev_type就填 0x1F; */ if(mode == MODE_P2P){ //p2p mode,查找dev_type dev_type = findDevTypeById(can_id, which); }else if(mode == MODE_BROADCAST){ //广播模式,dev_type为0x1F dev_type = 0x1F; } // 00 dev_type can_id 20 head = dev_type << 16 | can_id << 8 | 0x20; frame.can_id = CAN_EFF_FLAG | head; frame.data[0] = cmd_type; frame.data[1] = 0x00; frame.data[2] = 0x00; frame.data[3] = 0x00; frame.data[4] = 0x00; frame.data[5] = 0x00; frame.data[6] = 0x00; frame.data[7] = 0x00; frame.can_dlc = 8; ret = can_send(&frame, socket_can); if(ret < 0){ DBG_PRINTF("can_send error\n"); return; } parseCanFrame(&frame, buf, BUF_SIZE); DBG_PRINTF("data send to can-bus: %s\n", buf); if(cmd_type == 0x0f) //读取波形数据 { DBG_PRINTF("Reading wave data, waiting...\n"); } if(cmd_type == 0x02) //采集波形数据 { DBG_PRINTF("Collecting wave data, waiting...\n"); } // 等待can设备响应数据 while(1){ FD_ZERO(&fd_read); FD_SET(socket_can, &fd_read); // 3s,等不到数据就返回 time.tv_sec = 3; time.tv_usec = 0; //如果是读取波形数据,等待时间延长为60s if(cmd_type == 0x02 && !isWaveFinished) { time.tv_sec = 60; time.tv_usec = 0; } ret = select(socket_can+1, &fd_read, NULL, NULL, &time); if(ret == 0){ //超时 DBG_PRINTF("Completed...\n"); switch(mode) { case MODE_P2P: //p2p mode if(cmd_type == 0x00){ //呼叫通用数据 len = GetCanFramesById(can_id, canFrames); memset(buf, 0, BUF_SIZE); // 根据canFrames里面的frameNumber字段进行排序 // TODO... formatCommonData(canFrames, len, buf, BUF_SIZE); DBG_PRINTF(buf); memset(file_name, 0, 128); GenerateMacDateTimeFileName(file_name, 128, g_mac, ".dat"); DBG_PRINTF("writeCANData2File()...: %s\n", file_name); //将格式化的数据写入文件 CommonData2file(buf, file_name); }else if(cmd_type == 0x02){ //采集波形数据 len = GetCanFramesById(can_id, canFrames); //取出波形类别,和波形数据长度 waveDataType= canFrames[len - 1].data[0]; high = canFrames[len - 1].data[3] & 0xff; low = canFrames[len - 1].data[2] & 0xff; waveDataLen = low | (high << 8); DBG_PRINTF("waveDataType: %02x, waveDataLen: %d\n", waveDataType, waveDataLen); }else if(cmd_type == 0x0f){ //读取波形数据 //认为采集波形数据只支持P2P模式,所以链表中的所有数据来自同一个设备 //所以,就直接对全局链表进行操作 waveData2file(can_id, waveDataType); } break; case MODE_BROADCAST: //broadcast mode //广播只能呼叫通用数据 if(cmd_type == 0x00) { // 1. 遍历设备列表中取出can_id // 2. 根据can_id,从链表中取出对应的can_frame // 3. 对取出的can_frame进行format if(which == 1){ dev_list = g_ARR_A2; }else if(which == 2){ dev_list = g_ARR_A3; }else{ DBG_PRINTF("unkown which can\n"); continue; } memset(file_name, 0, 128); GenerateMacDateTimeFileName(file_name, 128, g_mac, ".dat"); DBG_PRINTF("writeCANData2File()...: %s\n", file_name); for(i = 1; i <= dev_list[0][0]; i++){ len = GetCanFramesById(dev_list[0][i], canFrames); memset(buf, 0, BUF_SIZE); // 根据canFrames里面的frameNumber字段进行排序 // TODO... formatCommonData(canFrames, len, buf, BUF_SIZE); DBG_PRINTF(buf); //将格式化的数据写入文件 CommonData2file(buf, file_name); } } break; } //清空链表 Linkedlist_clear(); break; }else if(ret > 0){ //有数据到来 memset(buf, 0, BUF_SIZE); ret = can_recv(&frame_recv, socket_can); if(ret < 0){ DBG_PRINTF("can_recv error\n"); continue; } //构造数据,存入链表 linkedCanFrame = (T_LinkedCanFrame *)malloc(sizeof(T_LinkedCanFrame)); linkedCanFrame->can_id = (frame_recv.can_id >> 8) & 0xff; linkedCanFrame->dev_type = (frame_recv.can_id >> 16) & 0xff; linkedCanFrame->frame_number = (frame_recv.can_id) & 0xff; linkedCanFrame->can_dlc = frame_recv.can_dlc; memcpy(linkedCanFrame->data, frame_recv.data, 8); AddCanFrame2LinkedList(linkedCanFrame); parseCanFrame(&frame_recv, buf, BUF_SIZE); //DBG_PRINTF("data from can-bus: %s\n", buf); //收到了采集波形数据响应帧,即采集完毕 if(cmd_type == 0x02 && frame_recv.data[0] == 0x02) isWaveFinished = 1; } }
/********************************************************************************************************************* ** Function name: can1_recv ** Descriptions: 接收数据 ** Input parameters: ** Output parameters: ** Returned value: ==OS_OK : 操作成功 ** !=OS_OK : 操作失败(包含出错信息) **-------------------------------------------------------------------------------------------------------------------- ** Created by: Feng Liang ** Created Date: 2011-12-13 17:19:4 ** Test recorde: **-------------------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: ** Test recorde: *********************************************************************************************************************/ static int can1_recv(CAN_MESSAGE *pMessage, int Timeout) { return can_recv(&can1_feature, pMessage, Timeout); }