/** * Danni's Arduino server on PORT 5099 * Serial Port: /dev/ttyACM0 with 9600 baud * */ #include #include #include #include #include #include #include /* Standard types */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ #include #include int serialport_init(const char* serialport, int baud); int serialport_writebyte(int fd, uint8_t b); int serialport_write(int fd, const char* str); void serialport_writeln(int fd, const char* str); int serialport_read_until(int fd, char* buf, char until); void sleep_ms(int ms); main() { int server_socket, new_socket; int l; int n; char buffer[255]; int verbindung_nummer = 1; struct sockaddr_in serverinfo, clientinfo; int run=1; int fd; unsigned short int portnummer = 5099; // char ip_adresse[] = "192.168.178.21"; // You IP address in the local network, if you want to access via the internet char ip_adresse[] = "127.0.0.1"; // 127.0.0.1 is the IP address of your local host char serport[]="/dev/ttyACM0"; fd = serialport_init(serport, B9600); if(fd==-1) { printf("Error opening serial port:%s",serport); return -1; } printf("\nDanni's Arduino Server started ...\n"); printf("IP address is: %s\n",ip_adresse); server_socket = socket(AF_INET, SOCK_STREAM, 0); serverinfo.sin_family = AF_INET; serverinfo.sin_addr.s_addr = inet_addr(ip_adresse); serverinfo.sin_port = htons(portnummer); l = sizeof(serverinfo); bind(server_socket, (struct sockaddr *)&serverinfo, l); listen(server_socket, 3); while (run) { new_socket = accept(server_socket, (struct sockaddr *)&clientinfo, &l); // get the client inet_ntop(AF_INET, &(clientinfo.sin_addr.s_addr), buffer, 255); printf("Client:%s\n",buffer); n = read(new_socket,buffer,255); if (n < 0) error("ERROR reading from socket"); if (n>0) { if (strcmp(buffer,"STOP")==0) { run =0; } else { printf("I will send a: %s to the Arduino.\n",buffer); serialport_writeln(fd,buffer); // Send the buffer to the Arduino sleep_ms(100); // sleep 100 milliseconds and wait for the answer. serialport_read_until(fd, buffer, '\n'); // Get the answer write(new_socket,buffer,strlen(buffer)+1); printf("Arduino's answer: %s\n",buffer); // and print it } } close(new_socket); verbindung_nummer++; } close(server_socket); printf("\nDanni's Arduino Server stopped.\n\n"); return(0); } void sleep_ms(int ms) { usleep(ms * 1000 ); } int serialport_writebyte( int fd, uint8_t b) { int n = write(fd,&b,1); if( n!=1) return -1; return 0; } int serialport_write(int fd, const char* str) { int len = strlen(str); int n = write(fd, str, len); if( n!=len ) return -1; return 0; } void serialport_writeln(int fd, const char* str) { serialport_write(fd, str); serialport_write(fd, "\n"); } int serialport_read_until(int fd, char* buf, char until) { char b[1]; int i=0; do { int n = read(fd, b, 1); // read a char at a time if( n==-1) { buf[0] = 0; // null terminate the string return -1; // couldn't read } if( n==0 ) { usleep( 10 * 1000 ); // wait 10 msec try again continue; } buf[i] = b[0]; i++; } while( (b[0] != until) || (i>250)); buf[i] = 0; // null terminate the string return 0; } // takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1") // and a baud rate (bps) and connects to that port at that speed and 8N1. // opens the port in fully raw mode so you can send binary data. // returns valid fd, or -1 on error int serialport_init(const char* serialport, int baud) { struct termios toptions; int fd; //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n", // serialport,baud); fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("init_serialport: Unable to open port "); return -1; } if (tcgetattr(fd, &toptions) < 0) { perror("init_serialport: Couldn't get term attributes"); return -1; } speed_t brate = baud; // let you override switch below if needed switch(baud) { case 4800: brate=B4800; break; case 9600: brate=B9600; break; #ifdef B14400 case 14400: brate=B14400; break; #endif case 19200: brate=B19200; break; #ifdef B28800 case 28800: brate=B28800; break; #endif case 38400: brate=B38400; break; case 57600: brate=B57600; break; case 115200: brate=B115200; break; } cfsetispeed(&toptions, brate); cfsetospeed(&toptions, brate); // 8N1 toptions.c_cflag &= ~PARENB; toptions.c_cflag &= ~CSTOPB; toptions.c_cflag &= ~CSIZE; toptions.c_cflag |= CS8; // no flow control toptions.c_cflag &= ~CRTSCTS; // toptions.c_cflag |= CRTSCTS; toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw toptions.c_oflag &= ~OPOST; // make raw // see: http://unixwiz.net/techtips/termios-vmin-vtime.html toptions.c_cc[VMIN] = 0; toptions.c_cc[VTIME] = 20; if( tcsetattr(fd, TCSANOW, &toptions) < 0) { perror("init_serialport: Couldn't set term attributes"); return -1; } return fd; }