|
|
@@ -0,0 +1,83 @@
|
|
|
+//Define Constants---------------------------------------------------
|
|
|
+#define USB_PORT /dev/ttyACM3
|
|
|
+#define PAGESIZE 64
|
|
|
+
|
|
|
+//Include Header Files-----------------------------------------------
|
|
|
+#include<stdlib.h>
|
|
|
+#include<stdio.h>
|
|
|
+#include<time.h>
|
|
|
+#include<errno.h>
|
|
|
+#include<unistd.h>
|
|
|
+#include<termios.h>
|
|
|
+#include<sys/types.h>
|
|
|
+#include<sys/stat.h>
|
|
|
+#include<fcntl.h>
|
|
|
+#include<sys/resource.h>
|
|
|
+
|
|
|
+//Prototypes---------------------------------------------------------
|
|
|
+void delay(int delay_time);
|
|
|
+void print_attrTermios(struct termios * ptr_attr);
|
|
|
+
|
|
|
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+//Main-Program
|
|
|
+int main(int argc, const char * argv[]){
|
|
|
+
|
|
|
+//initializing USART at USB_PORT-------------------------------------
|
|
|
+ int USB = open("/dev/ttyACM3", O_RDWR|O_NOCTTY);
|
|
|
+ if(isatty(USB)==0){
|
|
|
+ printf("ERROR: No File Descriptor! \n");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ struct termios * ptr_attr = (struct termios *)
|
|
|
+ malloc(sizeof(struct termios));
|
|
|
+ if(ptr_attr==NULL){
|
|
|
+ printf("ERROR: Termios not available \n");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ tcgetattr(USB, ptr_attr);
|
|
|
+ (*ptr_attr).c_iflag = IGNPAR;
|
|
|
+ (*ptr_attr).c_oflag = 0;
|
|
|
+ (*ptr_attr).c_cflag = CS8|CREAD;
|
|
|
+ (*ptr_attr).c_lflag = 0;
|
|
|
+ cfsetspeed(ptr_attr, B19200);
|
|
|
+ tcsetattr(USB, TCSANOW ,ptr_attr);
|
|
|
+ delay(1000);
|
|
|
+
|
|
|
+//Establishing Connection-------------------------------------------
|
|
|
+ char req[] = "REQ";
|
|
|
+ char ack[] = "ACK";
|
|
|
+ char * ptr_ack = &ack[0];
|
|
|
+ char * ptr_input = (char *)malloc(3*sizeof(char));
|
|
|
+ char * ptr_req = &req[0];
|
|
|
+ while(1){
|
|
|
+ write(USB, ptr_req, 3*sizeof(char));
|
|
|
+// tcflush(USB, TCIFLUSH);
|
|
|
+ read(USB, ptr_input, 3*sizeof(char));
|
|
|
+// tcflush(USB, TCOFLUSH);
|
|
|
+ printf("%s \n", &ptr_input[0]);
|
|
|
+ }
|
|
|
+ //delay(100);
|
|
|
+ close(USB);
|
|
|
+ return EXIT_SUCCESS;
|
|
|
+}
|
|
|
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+
|
|
|
+
|
|
|
+//Delay-Function-----------------------------------------------------
|
|
|
+void delay(int delay_time){
|
|
|
+ clock_t time1 = clock();
|
|
|
+ while(((clock()-time1)/CLOCKS_PER_SEC)*1000
|
|
|
+ != (clock_t)delay_time);
|
|
|
+}
|
|
|
+
|
|
|
+//Print Termios-Attributes-------------------------------------------
|
|
|
+void print_attrTermios(struct termios * ptr_attr){
|
|
|
+ printf("Input Mode Flag: %d \n", (*ptr_attr).c_iflag);
|
|
|
+ printf("Output Mode Flag: %d \n", (*ptr_attr).c_oflag);
|
|
|
+ printf("Control Mode Flag: %d \n", (*ptr_attr).c_cflag);
|
|
|
+ printf("Local Mode Flag: %d \n", (*ptr_attr).c_lflag);
|
|
|
+ printf("Baudrate Input: %d \n", (*ptr_attr).c_ispeed);
|
|
|
+ printf("Baudrate Output: %d \n", (*ptr_attr).c_ospeed);
|
|
|
+}
|