单片机和虚拟机Linux双向通信

2019-04-15 17:14发布

昨天说好的双向通信的代码,今天终于想通了。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 #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;jlinux端的代码: /* ************************************************* *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 #include #include #include #include #include /* *terminal device header file */ #include #include #include /* *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,'