prog.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //Define Constants---------------------------------------------------
  2. #define USB_PORT /dev/ttyACM3
  3. #define PAGESIZE 64
  4. //Include Header Files-----------------------------------------------
  5. #include<stdlib.h>
  6. #include<stdio.h>
  7. #include<time.h>
  8. #include<errno.h>
  9. #include<unistd.h>
  10. #include<termios.h>
  11. #include<sys/types.h>
  12. #include<sys/stat.h>
  13. #include<fcntl.h>
  14. #include<sys/resource.h>
  15. //Prototypes---------------------------------------------------------
  16. void delay(int delay_time);
  17. void print_attrTermios(struct termios * ptr_attr);
  18. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  19. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  20. //Main-Program
  21. int main(int argc, const char * argv[]){
  22. //initializing USART at USB_PORT-------------------------------------
  23. int USB = open("/dev/ttyACM3", O_RDWR|O_NOCTTY);
  24. if(isatty(USB)==0){
  25. printf("ERROR: No File Descriptor! \n");
  26. return 0;
  27. }
  28. struct termios * ptr_attr = (struct termios *)
  29. malloc(sizeof(struct termios));
  30. if(ptr_attr==NULL){
  31. printf("ERROR: Termios not available \n");
  32. return 0;
  33. }
  34. tcgetattr(USB, ptr_attr);
  35. (*ptr_attr).c_iflag = IGNPAR;
  36. (*ptr_attr).c_oflag = 0;
  37. (*ptr_attr).c_cflag = CS8|CREAD;
  38. (*ptr_attr).c_lflag = 0;
  39. cfsetspeed(ptr_attr, B19200);
  40. tcsetattr(USB, TCSANOW ,ptr_attr);
  41. delay(1000);
  42. //Establishing Connection-------------------------------------------
  43. char req[] = "REQ";
  44. char ack[] = "ACK";
  45. char * ptr_ack = &ack[0];
  46. char * ptr_input = (char *)malloc(3*sizeof(char));
  47. char * ptr_req = &req[0];
  48. while(1){
  49. write(USB, ptr_req, 3*sizeof(char));
  50. // tcflush(USB, TCIFLUSH);
  51. read(USB, ptr_input, 3*sizeof(char));
  52. // tcflush(USB, TCOFLUSH);
  53. printf("%s \n", &ptr_input[0]);
  54. }
  55. //delay(100);
  56. close(USB);
  57. return EXIT_SUCCESS;
  58. }
  59. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  60. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  61. //Delay-Function-----------------------------------------------------
  62. void delay(int delay_time){
  63. clock_t time1 = clock();
  64. while(((clock()-time1)/CLOCKS_PER_SEC)*1000
  65. != (clock_t)delay_time);
  66. }
  67. //Print Termios-Attributes-------------------------------------------
  68. void print_attrTermios(struct termios * ptr_attr){
  69. printf("Input Mode Flag: %d \n", (*ptr_attr).c_iflag);
  70. printf("Output Mode Flag: %d \n", (*ptr_attr).c_oflag);
  71. printf("Control Mode Flag: %d \n", (*ptr_attr).c_cflag);
  72. printf("Local Mode Flag: %d \n", (*ptr_attr).c_lflag);
  73. printf("Baudrate Input: %d \n", (*ptr_attr).c_ispeed);
  74. printf("Baudrate Output: %d \n", (*ptr_attr).c_ospeed);
  75. }