Linux串口初始化流程demo
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/ioctl.h>
#include <sys/ioctl.h>
#include <semaphore.h>
#include <termios.h>
#define HI_SUCCESS 0
#define HI_FAILURE (-1)
int UART_Read(int s32Fd, char * pData, int s32Len)
{
if (pData == NULL)
{
return HI_FAILURE;
}
if (s32Fd < 0)
{
return HI_FAILURE;
}
return read(s32Fd, pData, s32Len);
}
int UART_Write(int s32Fd, const char * pData, int s32Len)
{
int s32WriteLen = 0;
if (pData == NULL)
{
return HI_FAILURE;
}
s32WriteLen = write(s32Fd, pData, s32Len);
if (s32WriteLen < 0 && errno != EAGAIN)
{
printf("write failed - errno=%d (%s)\n", errno, strerror(errno));
}
return s32WriteLen;
}
static int UART_Config(int s32Fd, int s32BaudRate, unsigned char u8DataBit, unsigned char u8StopBit, signed char u8OeBit)
{
struct termios newtio;
bzero(&newtio,sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
switch(s32BaudRate)
{
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 38400:
cfsetispeed(&newtio, B38400);
cfsetospeed(&newtio, B38400);
break;
case 57600:
cfsetispeed(&newtio, B57600);
cfsetospeed(&newtio, B57600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
printf("No this baudrate set !!!\n");
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
switch(u8DataBit)
{
case 7: newtio.c_cflag |= CS7; break;
case 8: newtio.c_cflag |= CS8; break;
default: newtio.c_cflag |= CS8; break;
}
switch(u8StopBit)
{
case 1: newtio.c_cflag &= ~CSTOPB; break;
case 2: newtio.c_cflag |= CSTOPB; break;
default: newtio.c_cflag &= ~CSTOPB; break;
}
switch(u8OeBit)
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARODD;
break;
case 'E':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= (INPCK | ISTRIP);
newtio.c_cflag &= ~PARODD;
break;
case 'N':
default:
newtio.c_cflag &= ~PARODD;
break;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(s32Fd, TCIFLUSH);
if(0 != tcsetattr(s32Fd, TCSANOW, &newtio))
{
printf("uart config error!\n");
return HI_FAILURE;
}
return HI_SUCCESS;
}
int UART_Close(int s32Fd)
{
close(s32Fd);
return HI_SUCCESS;
}
int UART_Open()
{
int s32Fd = 0;
int s32Ret = 0;
s32Fd = open("/dev/ttyS4", O_RDWR | O_NOCTTY | O_NONBLOCK);
if(s32Fd < 0)
{
printf("Open error!\n");
return HI_FAILURE;
}
s32Ret = UART_Config(s32Fd,115200, 8,1, 'N');
if (HI_SUCCESS != s32Ret)
{
UART_Close(s32Fd);
return HI_FAILURE;
}
return s32Fd;
}
unsigned char MCU_CalcCheckSum(unsigned char *dat, unsigned char len) {
unsigned char checkSum = 0xFF;
while (len--) {
checkSum ^= (*dat);
dat++;
}
return checkSum;
}
int main()
{
int s32Fd = 0;
s32Fd = UART_Open();
char msg[8] = {0x55, 0xaa};
msg[2] = 0xf5;
msg[3] = 0x01;
msg[4] = 0x00;
msg[5] = 0x00;
msg[6] = 0x00;
msg[7] = MCU_CalcCheckSum(msg, 7);
UART_Write(s32Fd,msg,8);
UART_Close(s32Fd);
return 0;
}