Created
October 13, 2013 15:19
-
-
Save dagon666/6963468 to your computer and use it in GitHub Desktop.
slip binary IO in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int main(int argc, char const *argv[]) { | |
| g_fd = 0x00; | |
| struct response *resp; | |
| struct timer_data *data; | |
| uint8_t buff[8] = {0x00}; | |
| uint16_t crc = 0x00; | |
| if (argc <= 3) { | |
| fprintf(stderr, "host_slip <prescaler> <ocr> <st_max>\n"); | |
| return -1; | |
| } | |
| if (0 > (g_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_SYNC))) { | |
| fprintf(stderr, "Unable to open device [%s]\n", SERIAL_PORT); | |
| return 0; | |
| } | |
| tty_attrib_conf(g_fd, BAUD_9600, 0); | |
| tty_block_conf(g_fd, 1); | |
| data = (struct timer_data *)buff; | |
| data->crc16 = 0x0000; | |
| data->prescaler = atoi(argv[1]); | |
| data->ocr = atoi(argv[2]); | |
| data->st_max = atoi(argv[3]); | |
| // insert the CRC | |
| crc = crc16(0x00, buff, sizeof(struct timer_data)); | |
| data->crc16 = crc; | |
| // wait for the board to reset itself | |
| sleep(4); | |
| printf("Sending: %d/%d/%d, CRC: 0x%04x\n", | |
| data->prescaler, | |
| data->ocr, | |
| data->st_max, | |
| data->crc16); | |
| slip_send(buff, sizeof(struct timer_data)); | |
| slip_recv(buff, sizeof(buff)); | |
| resp = (struct response *)buff; | |
| // zero the CRC to perform verification | |
| buff[6] = buff[0]; | |
| buff[7] = buff[1]; | |
| buff[0] = 0x00; | |
| buff[1] = 0x00; | |
| // we've got the calculated CRC now | |
| crc = crc16(0x00, buff, sizeof(struct response)); | |
| // restore the CRC | |
| buff[0] = buff[6]; | |
| buff[1] = buff[7]; | |
| printf("CRC/CRC_CALCD/STATUS: 0x%04x/0x%04x/%s\n", | |
| resp->crc16, | |
| crc, | |
| (resp->status == 0x10 ? "ACK" : "NACK")); | |
| close(g_fd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment