#include "serial.h"
static char assic_uppercase_to_lowercase(char ch)
{
if(ch >= 'A' && ch <= 'Z')
{
return (ch + 32);
}
else
{
return ch;
}
};
static int diff_buf(char *buf,char *getbuf[3])
{
int i = 0,j = 0,n = 0;
char *ptr_buf = getbuf[j];
while(buf[i] != '\0')
{
ptr_buf[n] = assic_uppercase_to_lowercase(buf[i]);
if(buf[i] == ' ')
{
j ++;
ptr_buf[n] = '\0';
ptr_buf = getbuf[j];
i ++;
n = 0;
}
else
{
i ++;
n ++;
}
}
ptr_buf[n] = '\0';
ptr_buf = NULL; //free ptr_buf
return 0;
}
int serial_open(teminate_message *si)
{
int fd;
int err;
struct stat status;
struct termios curr_term;
if(NULL == si->uart_port)
{
Debug( "port = NULL \n");
return ERROR;
}
if(strlen(si->uart_port ) < 4 )
{
Debug("strlen( port ) < 4\n");
return ERROR;
}
if((err = stat( si->uart_port,&status)) == -1)
{
Debug("get stat faild\n");
return ERROR;
}
fd = open( si->uart_port, O_RDWR|O_NOCTTY|O_NONBLOCK);
if ( fd <= 0 )
{
Debug("serial open(%s) = %d, errno %d\r\n", si->uart_port, fd, errno );
return ERROR;
}
tcflush( fd, TCIOFLUSH );
if (0 != (err = tcgetattr( fd, &curr_term )))
{
Debug("serial_open: tcgetattr(%d) = %d, errno %d\r\n", fd, err, errno );
close(fd);
return ERROR;
}
curr_term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
curr_term.c_oflag &= ~OPOST;
curr_term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
curr_term.c_cflag &= ~(CSIZE | PARENB);
curr_term.c_cflag |= CS8;
if (si->hw)
{
curr_term.c_cflag |= CRTSCTS;
}
else
{
curr_term.c_cflag &= ~CRTSCTS;
}
tcsetattr( fd, TCSANOW, &curr_term );
tcflush( fd, TCIOFLUSH );
tcsetattr( fd, TCSANOW, &curr_term );
tcflush( fd, TCIOFLUSH );
tcflush( fd, TCIOFLUSH );
switch( si->baud_data)
{
case 4800:
si->baud_data = B4800;
break;
case 9600:
si->baud_data = B9600;
break;
case 19200:
si->baud_data = B19200;
break;
case 38400:
si->baud_data = B38400;
break;
case 57600:
si->baud_data = B57600;
break;
case 115200:
si->baud_data = B115200;
break;
case 230400:
si->baud_data = B230400;
break;
default:
close( fd );
return ERROR ;
}
if (cfsetispeed( &curr_term, si->baud_data ))
{
close( fd );
return ERROR;
}
if (cfsetospeed( &curr_term, si->baud_data ))
{
close( fd );
return ERROR;
}
tcsetattr(fd, TCSANOW, &curr_term );
return fd;
}
static int count_char_lenth(char *ch)
{
int i = 0;
while(*ch != '\0')
{
i ++;
ch ++;
}
return i;
}
static int write_uart_data(int fd,teminate_message *send_data)
{
return write( fd, send_data->message, count_char_lenth(send_data->message));
}
static int read_uart_data(int fd,teminate_message *get_data)
{
memset(get_data->message,0,count_char_lenth(get_data->message));
return read(fd,get_data->message,1024);
}
static int usage_instruction(int fd,teminate_message *cmd)
{
printf("********************************************************\n");
printf("*** Debian Uart usgeage instruction\n");
printf("***[Method One]\n");
printf("*** Step 1:./Serial uartNo baud message\n");
printf("*** Step 2:RX get message from TX\n");
printf("***[Method Two]\n");
printf("*** Step 1:./Serial\n");
printf("*** Step 2:open /dev/ttyS1 baud \n");
printf("*** Step 3:TR Message\n");
printf("*** Step 4:RX\n");
printf("********************************************************\n");
}
static int uart_cmd_type1_process(teminate_message *cmd_msg)
{
return serial_open(cmd_msg);
}
static int uart_cmd_type2_process(int cmd_seq,int re_value,teminate_message *cmd_msg)
{
switch(cmd_seq)
{
case CMD_RUM_UART_TX:
{
if(re_value == ERROR)
{
DEBUG_INFO("TX ERROR\n");
}
else
{
DEBUG_INFO("TX SUCCESSFUL\n");
}
}
break;
case CMD_RUN_UART_RX:
{
if(re_value == ERROR)
{
DEBUG_INFO("RX NO DATA\n");
}
else
{
DEBUG_INFO("RX:%s\n",cmd_msg->message);
}
}
break;
case CMD_RUN_UART_OPEN:
{
DEBUG_INFO("aaaaaaaa");
}
break;
case CMD_RUN_UART_EXIT:
{
return SUCCESS;
};
default:
break;
}
}
static int lib_strcmp(const char *str,const char *dest)
{
int i;
while(*str != '\0' || *dest != '\0')
{
if(*str == *dest)
{
str ++;
dest ++;
}
else
{
return ERROR;
}
}
return 0;
}
static int uart_baud_chek_data(int baud_data)
{
int i;
const int baud_str[] =
{
4800,
9600,
19200,
38400,
57600,
115200,
230400,
};
for(i = 0;i < sizeof(baud_str)/sizeof(int);i ++)
{
if(baud_data == baud_str[i])
{
return SUCCESS;
}
}
return ERROR;
}
static int open_process_func(int fd,teminate_message *cmd_msg)
{
return serial_open(cmd_msg);
}
static int lib_atoi_check(char *msg)
{
while(*msg != '\0')
{
if((*msg >= 'A' && *msg <= 'Z') || (*msg >= 'a' && *msg <= 'z'))
{
return ERROR;
}
msg ++;
}
return SUCCESS;
}
int main(int argc,char **argv)
{
char ch[1024];
char *bufcmd[3] ;
int i = 0;
int uart_fd = 0;
int re_value;
process_cmd_type_attr cmd_type;
teminate_message *ptr_msg = (teminate_message *)malloc(sizeof(teminate_message));
func_attr cmd_list[] =
{
{"tx" , write_uart_data },
{"rx" , read_uart_data },
{"open", open_process_func },
{"help", usage_instruction },
{"exit", open_process_func } //NO Process Any DATA
};
usage_instruction(0,NULL);
for(i = 0;i < sizeof(bufcmd)/sizeof(char *);i ++)
{
bufcmd[i] = (char *)malloc(sizeof(char *));
}
memset(ptr_msg,0,sizeof(ptr_msg));
//.serial ttys1 9500 message
if(argc > 4)
{
printf("parameter must be less more 4,Please input again \n");
return ERROR;
}
if(argc == 4)
{
strcpy(ptr_msg->message,argv[3]);
strcpy(ptr_msg->uart_port,argv[1]);
ptr_msg->baud_data = atoi(argv[2]);
uart_fd = uart_cmd_type1_process(ptr_msg);
if(uart_fd == ERROR)
{
printf("%s is not exist ,Open Failed \n",argv[1]);
}
else if(uart_baud_chek_data(ptr_msg->baud_data) == ERROR)
{
printf("Baud is not correct \n");
}
else
{
cmd_type = PROCESS_CMD_TYPE_ONE;
}
}
printf("###:");
while(gets(ch))
{
diff_buf(ch,bufcmd);
for(i =0;i < sizeof(cmd_list)/sizeof(func_attr);i ++)
{
if(!lib_strcmp((const char *)bufcmd[0],(const char *)cmd_list[i].cmd_name))
{
//uart_cmd_type2_process(i,re_value);
if(uart_fd)
{
if(*bufcmd[1] == '\0')
{
printf("Please input send message\n");
}
else
{
memset(ptr_msg->message,0,1024);
re_value = cmd_list[i].ptr_func(uart_fd,ptr_msg);
uart_cmd_type2_process(i,re_value,ptr_msg);
}
}
else
{
if(i == CMD_RUN_UART_EXIT)
{
return 0;
}
else if(i == CMD_RUN_UART_OPEN)
{
//open /dev/ttys1 115200
memset(ptr_msg->message,0,1024);
strcpy(ptr_msg->uart_port,bufcmd[1]);
if(lib_atoi_check(bufcmd[2]) == SUCCESS )
{
ptr_msg->baud_data = atoi(bufcmd[2]);
if(uart_baud_chek_data(ptr_msg->baud_data) == ERROR)
{
printf("Baud is not correct ,Please input again\n");
break;
}
}
else
{
printf("Baud has afphabet ,Please input again \n");
break;
}
re_value = cmd_list[i].ptr_func(uart_fd,ptr_msg);
Debug("re_value = %d \n",re_value);
if(re_value == ERROR)
{
printf("Open %s Faild\n",ptr_msg->uart_port);
}
else
{
printf("Open Successfully !\n");
}
}
else
{
if(!uart