A-A+

串口控制32路舵机伺服器

2012年10月09日 综合技术 暂无评论 阅读 729 次

/*串口控制32路舵机伺服器
*2012-10-09
*Inewup.com
*/

#include <stdio.h>

#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
#define HOST_PORT 0

int set_port(int fd, int baud_rate, int data_bits, char parity, int stop_bits) {
struct termios newtio,oldtio;
if( tcgetattr(fd,&oldtio) != 0) {
perror("Setup Serial 1");
return -1;
}
bzero(&newtio,sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
/* set baud_speed*/
switch(baud_rate) {
case 2400:
cfsetispeed(&newtio,B2400);
cfsetospeed(&newtio,B2400);
break;
case 4800:
cfsetispeed(&newtio,B4800);
cfsetospeed(&newtio,B4800);
break;
case 9600:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
case 19200:
cfsetispeed(&newtio,B19200);
cfsetospeed(&newtio,B19200);
break;
case 38400:
cfsetispeed(&newtio,B38400);
cfsetospeed(&newtio,B38400);
break;
default:
case 115200:
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
break;
}
/* set data_bits upon 7 or 8*/
switch(data_bits) {
case 7:
newtio.c_cflag |= CS7;
break;
default :
case 8:
newtio.c_cflag |= CS8;
break;
}
/**/
switch(parity) {
default:
case 'N':
case 'n': {
newtio.c_cflag &= ~PARENB;
newtio.c_iflag &= ~INPCK;
}
break;
case 'o':
case 'O':
{
newtio.c_cflag |= (PARODD | PARENB);
newtio.c_iflag |= INPCK;
}
break;
case 'e':
case 'E':
{
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
newtio.c_iflag |= INPCK;
}
break;
case 's': case 'S': {
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
}
break;
}
/*set stop_bits 1 or 2 */
switch(stop_bits) {
default: case 1: { newtio.c_cflag &= ~CSTOPB; }
break; case 2: { newtio.c_cflag |= CSTOPB; }
break;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio)) != 0) {
perror("com set error"); return -1;
}
printf("set UART done!n");
return 0;
}
int open_port(int com_port) {
int fd = 0;
char *dev[] =
{
"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2","/dev/ttyS3", "/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6"
};
if((com_port < 0) || (com_port > 6) ) {
printf("the port is out range");
return -1;
}
fd = open(dev[com_port], O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0)
{
perror("open serial port");
return -1;
}
if(fcntl(fd, F_SETFL,0) < 0) {
perror("fcntl F_SETFL");
return -1;
}
if(isatty(fd) == 0) {
perror("isatty is not a terminal device");
return -1;
}
return fd;
}

int main(void) {
int fd = 0;
char buffer2[]="rn";
char buffer[BUFFER_SIZE] = {0};
if((fd = open_port(HOST_PORT)) == -1){
perror("open port"); return -1;
}
if(set_port(fd,115200,8,'N',1)== -1) {
perror("set port"); return -1;
}
do {
printf("Input some words:n");
memset(buffer,0,BUFFER_SIZE);
if(fgets(buffer,BUFFER_SIZE,stdin) == NULL) {
perror("fgets"); break;
}
write(fd,buffer,strlen(buffer));
write(fd,buffer2,strlen(buffer2));}
while(strncmp(buffer,"quit",4));
close(fd);
return 0;
}

给我留言

Copyright © 2011-2016 零下二十四度Theme By  Ality  京ICP备16007547号   关于本站

用户登录