初始化异常。
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define FALSE -1
#define TRUE 0
int speed_arr[] = { B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, //
B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
unsigned char ascii[300] = {0};
unsigned char to_mobile[12] = {"to_mobile : "};
unsigned char huiche[1] = {0x0d};
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt; //定义一个结构
tcgetattr(fd, &Opt);//用来得到机器原端口的默认设置
for(i = 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if(speed == name_arr[i])//判断传进来是否相等
{
tcflush(fd, TCIOFLUSH);//刷新输入输出缓冲
//cfsetispeed(&Opt, speed_arr);//这里分别设置
//cfsetospeed(&Opt, speed_arr);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);//立刻把波特率设置真正写到串口中去
if(status != 0)
{
perror("tcsetattr fd1");//设置错误
}
return;
}
tcflush(fd, TCIOFLUSH);//得到机器设置
}
}
int set_Parity(int fd, int databits, int stopbits, int parity)
{
struct termios options;
if(tcgetattr(fd, &options) != 0)//首先读取系统默认设置,必须
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;//设置c_cflag选项不按位数据位掩码
switch(databits)//数据位
{
case 7:
options.c_cflag |= CS7; //设置c_cflag选项数据位为7位
break;
case 8:
options.c_cflag |= CS8;//8 bits
break;
default:
fprintf(stderr, "Unsupported data size/n");
return(FALSE);
}
switch(parity)//设置奇偶校验,c_cflag c_iflag有效
{
case 'n':
case 'N'://no parity
options.c_cflag &= ~PARENB; //clear parity enable
options.c_iflag &= ~INPCK; //enable parity checking
break;
case 'o':
case 'O': //奇校验,INPCK检查校验
options.c_cflag |= (PARODD | PARENB);//设置奇校验
options.c_iflag | INPCK; //disnable parity checking
break;
case 'e':
case 'E'://偶校验
options.c_cflag |= PARENB;//enable parity
options.c_cflag &= ~PARODD;//转换为偶校验
options.c_iflag |= INPCK;//disnable parity checking
break;
default:
fprintf(stderr, "Unsupported parity/n");
return(FALSE);
}
switch(stopbits)//设置停止位,影响表示位是c_cflag
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr, "Unsupported stop bits/n");
return(FALSE);
}
if(parity != 'n')//设置输入是否进行校验
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150;//15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);//刷新和立刻写进去
if(tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR );
//| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
void turn_to_ascii(char dec[], int lenth)
{
unsigned int i;
unsigned char temp = 0;
for(i = 0; i < lenth; i++)
{
temp = ((dec[i] >>4) & 0x0f);
if(temp <= 9)
ascii[2 * i] = temp + 0x30;
else if((temp >= 0x0a) && (temp <= 0x0f))
ascii[2 * i] = temp + 0x37;
temp = dec[i] & 0x0f;
if(temp <= 9)
ascii[2 * i + 1] = temp + 0x30;
else if((temp >= 0x0a) && (temp <= 0x0f))
ascii[2 * i + 1] = temp + 0x37;
}
}
void clean_ascii()
{
int i, j;
j = strlen(ascii);
for(i = 0; i < j; i++)
{
ascii[i] = 0;
}
}
int show_ascii(char *buf, int length)
{
turn_to_ascii(buf,length);
printf("%s from the mobile!/n", ascii);
clean_ascii();
return 0;
}
int main(int arge, char **argv)
{
unsigned char sim_restart[]={0x3b,0x9e,0x11,0x80,0x1f,0xc7,0x80,0x31,0xa0,0x73,0xbe,0x21,0x1b,0x66,0x44,0x4d,0x54,0x17,0x00,0x19,0x04,0x0d};
//unsigned char sim_restart[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16};
int fd;
int fd_2;
int nread;
char buff[512];
int loopi;
int loopj;
//char *dev = "/dev/ttyS0";
//char *dev="/dev/ttySAC1";
char *dev="/dev/ttySAC2";
fd = OpenDev(dev);
if(fd >= 0)
{
set_speed(fd, 4800);
}
else
{
printf("Can't Open Serial Port!/n");
exit(0);
}
if (set_Parity(fd,8,1,'N')== FALSE) //设置8,1,n 注意,这里和上面要和下位机相符才可能通信
{
printf("Set Parity Error/n");
exit(1);
}
char *dev_2="/dev/ttySAC0";
fd_2 = OpenDev(dev_2);
if(fd_2 >= 0)
{
set_speed(fd_2, 115200);
}
else
{
printf("Can't Open Serial Port!/n");
exit(0);
}
if (set_Parity(fd_2,8,1,'N')== FALSE) //设置8,1,n 注意,这里和上面要和下位机相符才可能通信
{
printf("Set Parity Error/n");
exit(1);
}
while(1)
{
/*while((nread = read(fd, buff, 512)) > 0)
{
printf("/nLen %d/n", nread);
buff[nread + 1] = '/0';
printf("/n%s",buff);
for(loopi = 0; loopi < 1000; loopi++)
for(loopj = 0; loopj < 10000; loopj++);
write(fd,buff,nread);
}*/
write(fd, sim_restart, 21);
/*usleep(10000);
write(fd_2, to_mobile, 12);
usleep(10000);
turn_to_ascii(sim_restart, 21);
usleep(10000);
write(fd_2, ascii, 42);
usleep(10000);
clean_ascii();
usleep(10000);
write(fd_2, huiche, 1);*/
usleep(10000);
nread = read(fd, buff, 512);
if(nread > 0)
{
/*show_ascii(buff, nread);
usleep(10000);
for(loopi = 0; loopi < 512; loopi++)
buff[loopi] = 0;*/
for(loopi = 0; loopi < nread; loopi++)
{
printf("%02x", buff[loopi]);
buff[loopi] = 0;
}
printf("/n");
}
sleep(1);
}
}