标签:
实验目的:
实现远程点灯,通过gprs 自带调试软件发送信息,完成智能家具远程点灯实验。
1.定制arm系统
使用uboot资料自带的!在内核中添加pl2303串口驱动,进行u口转串口的使用,因为我使用的arm板子,串口不够用,还需要一个串口来支持下面的430单片机,通过430去控制家具的灯。
上操作:
内核修改:
到linux内核目录下 make menucofig
Device Drivers-->USB support-->USB Serial Converter support--->【*】USB CP2101 UART Bridge Controller
然后退出保存!
make clean
make 完成!
制作文件系统,设置开机自启动程序
修改/etc/profile文件,加运行我程序的命令(./start)
将交叉编译好的程序,放到指定的文件夹下,开机自启动就好。
上网关的代码:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#include<stdlib.h>
int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop)
{
struct termios newtio,oldtio;
if(tcgetattr(fd,&oldtio) != 0)
{
perror("SetupSerial 1");
return -1;
}
bzero(&newtio,sizeof(newtio));//鍏ㄩ儴璁句负0
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch(nBits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch(nEvent)
{
case ‘O‘:
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |=(INPCK |ISTRIP);//鍋舵牎楠? break;
case ‘E‘:
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag |= ~PARODD;//濂囨牎楠? case ‘N‘:
newtio.c_cflag &= ~PARENB;
break;//涓嶆牎楠? }
switch(nSpeed)
{
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 115200:
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
break;
default:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
}
if(nStop ==1)
newtio.c_cflag &= ~CSTOPB;
else if(nStop ==2)
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return (-1);
}
printf("set done!\n");
return 0;
}
int Open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyUSB0","/dev/ttySAC1","/dev/ttySAC2","/dev/ttySAC3"};
long vdisable;
if(comport==1)
{
fd = open("/dev/ttyUSB0",O_RDWR);
if(-1==fd)
{
perror("Can‘t Open Serial Port");
return (-1);
}
else
printf("Open ttySAC0 >>>>>..\n");
}
else if(comport==2)
{
fd = open("/dev/ttySAC1",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1 == fd)
{
perror("Can‘t Open Serial Port");
return (-1);
}
else
printf("Open ttySAC1klldkkfjkasdjfakl\n");
}
else if(comport==3)
{
fd = open("/dev/ttySAC2",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1 == fd)
{
perror("Can‘t Open Serial Port");
return (-1);
}
else
printf("Open ttySAC2 ..............\n");
}
else if(comport == 4)
{
fd = open("/dev/ttySAC3",O_RDWR | O_NOCTTY|O_NDELAY);
if(fd == -1)
{
perror("Can‘t Open serial Port");
return (-1);
}
else
printf("Open ttySAC3.......................\n");
}
if(fcntl(fd,F_SETFL,0)<0)
printf("fcntl falled !\n");
else
printf("fcntl =%d\n",fcntl(fd,F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}
int main(void)
{
int fd,fd1;
int nread,i,nwrite;
char buff[1];
if((fd=Open_port(fd,1))<0)
{
perror("open_port error");
return ;
}
if((i=set_opt(fd,9600,8,‘N‘,1))<0)
{
perror("set_opt error");
return ;
}
if((fd1=Open_port(fd1,2))<0)
{
perror("open_port ttySAC2 error");
return ;
}
if((i=set_opt(fd1,9600,8,‘N‘,1))<0)
{
perror("ser_opt 2 error");
return ;
}
sleep(3);
printf("fd = %d\n",fd);
printf("fd1 = %d\n",fd1);
/* for(i=0;i<100;i++)
{
nread = write(fd,buff,8);
printf("nread = %d,%s\n",nread,buff);
sleep(1);
}*/
while(1)
{
sleep(1);
nread=read(fd1,buff,1);
if(nread != 0)
{
nwrite = write(fd,buff,1);
if(nwrite != nread)
{
perror("error!!!!!!!!!!!");
return ;
}
}
}
close(fd);
close(fd1);
return ;
}
2.430 单片机通过uart接收信息,上代码
#include <msp430x14x.h>
char i;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P2DIR=0xff;
P2OUT=0xff;//初始化端脚
P3SEL |= 0x30;//引脚服用
ME1 |= UTXE0 +URXE0;//使能
UCTL0 |= CHAR;//8位数据
UTCTL0 |= SSEL0;//选择晶振
UBR00=0x03;
UBR10 = 0x00;
UMCTL0 = 0x4A;
UCTL0 &= ~SWRST;
IE1 |= URXIE0 + UTXIE0;
IFG1 &= ~UTXIFG0;
_BIS_SR(LPM3_bits + GIE);
}
#pragma vector = UART0RX_VECTOR
__interrupt void usart0_rx(void)
{
i = RXBUF0;
switch(i)
{
case ‘1‘:P2OUT ^= 0x01;break;
case ‘2‘:P2OUT ^= 0x02;break;
case ‘3‘:P2OUT ^= 0x04;break;
case ‘4‘:P2OUT ^= 0x08;break;
case ‘5‘:P2OUT ^= 0x10;break;
case ‘6‘:P2OUT ^= 0x20;break;
case ‘7‘:P2OUT ^= 0x40;break;
case ‘8‘:P2OUT ^= 0x80;break;
case ‘9‘:P2OUT |= 0xff;break;
default:P2OUT |= 0xff;break;
}
}
3.按照我写的GPRS的串口,设置gprs属性!
通过所带资料调试,成功!
标签:
原文地址:http://www.cnblogs.com/wang1994/p/5093650.html