昨天说好的双向通信的代码,今天终于想通了。linux通过串口向单片机返回一个信号量,单片机通过这个信号量,启动led灯。
下面把代码贡献出来:
单片机端CC2530:
/*
************************************************
*Name : serial *
*Date : 2015-04-30 *
*Author : Sniper *
*Aim : serial port send the message to linux *
* and linux return the signal to turn on *
* led. *
************************************************
*/
#include "ioCC2530.h"
#include <string.h>
#define uchar unsigned char
#define led P1_1
uchar receive_message;
uchar Recdata[]="Hello Linux";
void initclk(void)
{
SLEEPCMD&= ~0X04;
CLKCONCMD = 0X10;
while(CLKCONSTA!=0X10);
SLEEPCMD = 0X04; //关闭不用的RC振荡器
}
void inituart(void)
{
IEN0|=0x84;//总中断,接收中断使能
U0CSR|=0xc0;//UART模式,允许接收
U0GCR=10;
U0BAUD=59;
}
void initio(void)
{
P0SEL|=0x0c;
}
/*
*Uart Send the message
*/
void UartTX_Send_String(uchar *Data,int len)
{
int j;
for(j=0;j<len;j++)
{
U0DBUF = *Data++;
while(UTX0IF == 0);
UTX0IF = 0;
}
}
/*
*send the message and receive the signal to turnning on the led
*/
void main(void)
{
initio();
initclk();
inituart();
UartTX_Send_String(Recdata,30);
if(receive_message != ' ')
{
P1SEL &=~0x02;
P1DIR |= 0x02;
while(1)
led=0;
}
while(1)
{
}
}
/*
*interupt receive the signal
*/
#pragma vector=URX0_VECTOR
__interrupt void UART0_IRQ(void)
{
receive_message = U0DBUF;
URX0IF=0;
//U0DBUF = receive_message;
while(!UTX0IF);
UTX0IF=0;
}
linux端的代码:
/*
*************************************************
*Name : Serial_communication.c *
*Date : 2015-04-29 *
*Author : Sniper *
*Aim : make the CC2530 single chip and *
* Linux (CentOS) communication. *
* Linux receive and print the message*
*************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/*
*terminal device header file
*/
#include <termios.h>
#include <errno.h>
#include <string.h>
/*
*struct termios
*
struct termios{
unsigned short c_iflag; //输入模式标志
unsigned short c_oflag; //输出模式标志
unsigned short c_cflag; //控制模式标志
unsigned short c_lflag; //区域模式标志或本地模式标志或局部模式
unsigned char c_line; //行控制line discipline
unsigned char c_cc[NCC]; // 控制字符特性
};
*/
#define SERIAL_PORT "/dev/ttyUSB0" //串口地址
#define PORT_SPEED 38400 //串口波特率
#define DATABITS 8 //数据位
#define STOPBITS 1 //停止位
#define PARITY 'n' //校验方式 (不校验)
int setSpeed(int, int, struct termios*);
int setParity(int, int, int, int, struct termios);
int openPort(void);
int init(void);
void readPort(int);
/*
*main function
*/
int main()
{
char *quit = (char*)malloc(sizeof(char*));
int fd;
char write_buf[256];
memset(write_buf,'\0',sizeof(write_buf));
fd = init();//初始化端口设置
printf("configure complete\n");
printf("start send and receive data...\n");
while(1)
{
readPort(fd);
}
close(fd);
}
/*
*receive the message from the single chip and send the signal to single chip
*/
void readPort(int fd)
{
int i;
int len;
int n;
char read_buf[256];
char return_message[100];
memset(read_buf,'\0',sizeof(read_buf));
memset(return_message,'\0',sizeof(return_message));
strcpy(return_message,"Hello CC2530");
while(1)
{
bzero(read_buf, sizeof(read_buf));
while((n = read(fd, read_buf, sizeof(read_buf))) > 0)
printf("%s\n", read_buf);
/*
*return message and start the buzzer
*/
if(strcmp(read_buf,"Signal") == 0)
{
n=write(fd,return_message,sizeof(return_message));
if(n>0)
printf("Message has been written!\n");
}
}
}
/*
*init serial port
*1 failure, 0 success
*/
int init(void)
{
int fd;
struct termios opt; //定义termios结构
//打开串口
fd = openPort();
//设置波特率
if(setSpeed(fd, PORT_SPEED, &opt) == 1)
{
printf("setSpeed failed!\n");
exit(1);
}
//设置数据位、停止位和校验位
if(setParity(fd, DATABITS, STOPBITS, PARITY, opt) == 1)
{
printf("setParity failed!\n");
exit(1);
}
if(tcsetattr(fd, TCSANOW, &opt) != 0) //TCSANOW:不等数据传输完毕就立即改变属性。
{ //TCSADRAIN:等待所有数据传输结束才改变属性。
perror("serial error");
return -1;
}
return fd;
}
/*
*openPort function
*1 failure, 0 success
*/
int openPort()
{
int fd;
/*
*serial address is /dev/ttyUSB*, open the serial address
*/
fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY |O_NDELAY);
if(fd == -1)
{
perror("open serial failed!\n");
exit(1);
}
return fd;
}
/*
*setSpeed
* SetSpeed(38400, 19200, 9600, 4800, 2400, 1200, 300)
* 1 failure, 0 success
*/
int setSpeed(int fd, int speed, struct termios *Opt)
{
int i;
if(tcgetattr(STDIN_FILENO, &Opt) != 0)
{
perror("tcgetattr fd\n");
return 1;
}
/*
*set speed
*/
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed);
cfsetospeed(&Opt, speed);
//set ways of receive
if(tcsetattr(fd, TCSANOW, &Opt) != 0)
{
perror("tcsetattr fd");
return 1;
}
tcflush(fd, TCIOFLUSH);
return 0;
}
/*
*setParity
*set the data bit,stop bit and check bit
* 1 failure, 0 success
*/
int setParity(int fd, int databits, int stopbits, int parity, struct termios Opt)
{
if(tcgetattr(fd, &Opt) != 0)
{
perror("tcgetattr fd");
return 1;
}
Opt.c_cflag |= (CLOCAL | CREAD);
/*
*Data bit
*/
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= CS8;
/*
*check bit
*/
Opt.c_cflag &= ~PARENB;
Opt.c_iflag &= ~INPCK;
/*
*stop bit
*/
Opt.c_cflag &= ~CSTOPB;
Opt.c_cflag |= (CLOCAL | CREAD);
Opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Opt.c_oflag &= ~OPOST; //OPOST :启用具体实现自行定义的输出处理。
Opt.c_oflag &= ~(ONLCR | OCRNL); //OCRNL :将输出中的回车映射为新行符
//ONLCR :(XSI) 将输出中的新行符映射为回车-换行。
Opt.c_iflag &= ~(ICRNL | INLCR); //ICRNL :将输入中的回车翻译为新行 (除非设置了 IGNCR)(否则当输入信号有 CR 时不会终止输入)。
//INLCR :将输入中的 NL 翻译为 CR。(将收到的换行符号转换为Return)
Opt.c_iflag &= ~(IXON | IXOFF | IXANY); //IXON :启用输出的 XON/XOFF 流控制。
//IXOFF :启用输入的 XON/XOFF 流控制。
//IXANY :(不属于 POSIX.1;XSI) 允许任何字符来重新开始输出。
tcflush(fd, TCIFLUSH); //清空输入缓存
//MIN = 0 , TIME =0; 有READ立即回传否则传回 0,不读取任何字元
Opt.c_cc[VTIME] = 0;
Opt.c_cc[VMIN] = 0;
if(tcsetattr(fd, TCSANOW, &Opt) != 0) //设置数据接收方式
{
perror("tcsetattr fd");
return 1;
}
return 0;
}
其实可以看出,我只需要向/dev/ttyUSB0中写入数据,单片机的硬件部件就可以自动读取,这个过程是硬件自己完成的。今天才反应过来这一点,有点慢啊!~具体的使用过程和上一篇一样!