fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/ioctl.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <unistd.h>
  9.  
  10. #define SERIAL_PORT "/dev/tty.usbmodemfa1311"
  11. #define BAUDRATE B9600
  12. #define BUFSIZE 256
  13.  
  14. #define TRUE 1;
  15. #define FALSE 0;
  16.  
  17. int main(int argc, char* argv[])
  18. {
  19. printf("program start\n");
  20. int fd = open(SERIAL_PORT, O_RDWR);
  21. printf("open");
  22. if (fd == -1) {
  23.  
  24. perror("Unable to open serial port\n");
  25. return -1;
  26. }
  27.  
  28. printf("opened\n");
  29. // 既存のシリアルの設定を取得
  30. struct termios old_options;
  31. int ret;
  32. ret = tcgetattr(fd, &old_options);
  33.  
  34. /*
  35.  
  36.   // 新しいシリアルの設定を適用
  37.   memset(&new_options, 0, sizeof(new_options));
  38.   new_options.c_cflag = CS8 | CLOCAL | CREAD;
  39.   new_options.c_cc[VTIME] = 0;
  40.   new_options.c_lflag = ICANON;
  41.   new_options.c_iflag = IGNPAR | ICRNL;
  42.   cfsetispeed(&new_options, BAUDRATE);
  43.   cfsetospeed(&new_options, BAUDRATE);
  44.   tcsetattr(fd, TCSANOW, &new_options);
  45.  
  46.   // 通信処理
  47.   int count;
  48.   volatile int STOP = 0;
  49.   char readbuf[BUFSIZE];
  50.   printf("start\n");
  51.   while(STOP == 0){
  52.   memset(readbuf, 0, sizeof(readbuf));
  53.   count = (int)read(fd, &readbuf, BUFSIZE);
  54.   if(count < 0){
  55.   fprintf(stdout, "end\n");
  56.   STOP = 1;
  57.   }else{
  58.   fprintf(stdout, "recv:%s(%d)\n", readbuf, count);
  59.   }
  60.   }
  61.   */
  62.  
  63. // シリアルの設定を既存のものに戻して終了
  64. ret = tcsetattr(fd, TCSANOW, &old_options);
  65. close(fd);
  66.  
  67. printf("end\n");
  68. return 0;
  69. }
  70.  
Runtime error #stdin #stdout 0.02s 1676KB
stdin
Standard input is empty
stdout
program start
open