send_serial.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Simple binary sender for nebos serial_recv.
  2. Protocol: receiver sends 'R', then sender sends:
  3. 4 bytes: size (big-endian)
  4. N bytes: data
  5. 1 byte: checksum (sum of data bytes mod 256)
  6. Usage: send_serial <device> <baud> <file> */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <termios.h>
  13. #include <stdint.h>
  14. int main(int argc, char *argv[])
  15. {
  16. int fd;
  17. FILE *fp;
  18. unsigned char *data;
  19. long size;
  20. unsigned int cksum;
  21. unsigned char buf[5];
  22. unsigned char r;
  23. struct termios tty;
  24. if (argc != 4) {
  25. fprintf(stderr, "Usage: %s <device> <baud> <file>\n", argv[0]);
  26. return 1;
  27. }
  28. fd = open(argv[1], O_RDWR | O_NOCTTY);
  29. if (fd < 0) {
  30. perror("open");
  31. return 1;
  32. }
  33. /* Configure serial port. */
  34. memset(&tty, 0, sizeof(tty));
  35. if (tcgetattr(fd, &tty) != 0) {
  36. perror("tcgetattr");
  37. return 1;
  38. }
  39. cfsetispeed(&tty, atoi(argv[2]));
  40. cfsetospeed(&tty, atoi(argv[2]));
  41. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  42. tty.c_cflag |= CLOCAL | CREAD;
  43. tty.c_cflag &= ~(PARENB | PARODD | CRTSCTS | CSTOPB);
  44. tty.c_iflag &= ~(IXON | IXOFF | IXANY | IGNBRK | BRKINT | ICRNL | INLCR);
  45. tty.c_oflag &= ~OPOST;
  46. tty.c_lflag = 0;
  47. tty.c_cc[VMIN] = 1;
  48. tty.c_cc[VTIME] = 0;
  49. if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  50. perror("tcsetattr");
  51. return 1;
  52. }
  53. /* Read file. */
  54. fp = fopen(argv[3], "rb");
  55. if (!fp) {
  56. perror("fopen");
  57. return 1;
  58. }
  59. fseek(fp, 0, SEEK_END);
  60. size = ftell(fp);
  61. fseek(fp, 0, SEEK_SET);
  62. data = malloc(size);
  63. if (!data) {
  64. perror("malloc");
  65. return 1;
  66. }
  67. if (fread(data, 1, size, fp) != (size_t)size) {
  68. perror("fread");
  69. return 1;
  70. }
  71. fclose(fp);
  72. /* Compute checksum. */
  73. cksum = 0;
  74. for (long i = 0; i < size; i++)
  75. cksum += data[i];
  76. cksum &= 0xFF;
  77. /* Wait for 'R' from receiver. */
  78. fprintf(stderr, "Waiting for receiver...\n");
  79. if (read(fd, &r, 1) != 1 || r != 'R') {
  80. fprintf(stderr, "No 'R' received\n");
  81. return 1;
  82. }
  83. fprintf(stderr, "Got 'R', sending %ld bytes...\n", size);
  84. /* Send size (4 bytes big-endian). */
  85. buf[0] = (size >> 24) & 0xFF;
  86. buf[1] = (size >> 16) & 0xFF;
  87. buf[2] = (size >> 8) & 0xFF;
  88. buf[3] = size & 0xFF;
  89. if (write(fd, buf, 4) != 4) {
  90. perror("write size");
  91. return 1;
  92. }
  93. tcdrain(fd);
  94. /* Send data. */
  95. if (write(fd, data, size) != size) {
  96. perror("write data");
  97. return 1;
  98. }
  99. tcdrain(fd);
  100. /* Send checksum. */
  101. buf[0] = cksum;
  102. if (write(fd, buf, 1) != 1) {
  103. perror("write checksum");
  104. return 1;
  105. }
  106. tcdrain(fd);
  107. fprintf(stderr, "Done.\n");
  108. free(data);
  109. close(fd);
  110. return 0;
  111. }