int can_send_message(can_message_t* message) { static CPC_CAN_MSG_T cmsg = {0x00L, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; int i, retval = 0; cmsg.id=message->id; //put the can id of the device in cmsg cmsg.length=8; //put the lenght of the message in cmsg for(i = 0; i < cmsg.length; i++) { cmsg.msg[i] = message->content[i]; //put the message in cmsg } SELECT_WR; //enable write en read if (FD_ISSET(cpcfd, &writefds)) { //if chanel open and writable (???) //send the message of lenght 8 to the device of id can_id while ((retval = CPC_SendMsg(handle, 0, &cmsg)) == CPC_ERR_CAN_NO_TRANSMIT_BUF) { usleep(10); } if (retval == 0) { //wait for the reply PDEBUG("Sent CAN message, now waiting for reply...\n"); can_read_message(); PDEBUG("Received CAN reply\n"); } else { PDEBUG_ERR("%s\n", CPC_DecodeErrorMsg(retval)); return -1; } } return 0; }
int can_cpc_send(can_cpc_device_p dev, can_message_p message) { CPC_CAN_MSG_T msg = {0x00L, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; struct timeval time; fd_set set; int i, error; msg.id = message->id; msg.length = message->length; memcpy(msg.msg, message->content, message->length); time.tv_sec = 0; time.tv_usec = dev->timeout*1e6; FD_ZERO(&set); FD_SET(dev->fd, &set); error = select(dev->fd+1, NULL, &set, NULL, &time); if (error == 0) return CAN_CPC_ERROR_TIMEOUT; while ((error = CPC_SendMsg(dev->handle, 0, &msg)) == CPC_ERR_CAN_NO_TRANSMIT_BUF) usleep(10); if (!error) ++dev->num_sent; else return CAN_CPC_ERROR_SEND; return CAN_CPC_ERROR_NONE; }