点击(此处)折叠或打开
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <stdint.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <termios.h>
-
#include <errno.h>
-
#include <limits.h>
-
#include <asm/ioctls.h>
-
#include <time.h>
-
#include <pthread.h>
-
#include <sys/ipc.h>
-
#include <sys/msg.h>
-
-
#include "lib-slip.h"
-
-
#include "util-port.h"
-
-
-
#define MSG printf
-
-
static int gPortFd;
-
static int gMsgId;
-
static int gNumPort;
-
MutilSlipAttr_t gUartSlip = {0};
-
-
UartInfoAttr_t gUartInfo[] = {
-
[0] = {
-
.mPath = UART1_PATH,
-
.mMode = UART_115200_8N1,
-
},
-
[1] = {
-
.mPath = UART2_PATH,
-
.mMode = UART_115200_8N1,
-
},
-
[2] = {
-
.mPath = UART3_PATH,
-
.mMode = UART_115200_8N1,
-
},
-
[3] = {
-
.mPath = UART4_PATH,
-
.mMode = UART_115200_8N1,
-
},
-
};
-
-
int open_serial(int port){
-
int fd;
-
struct termios opt;
-
-
if(!gUartInfo[port].mPath){
-
return -1;
-
}
-
fd = open(gUartInfo[port].mPath, O_RDWR | O_NOCTTY);
-
if(fd < 0){
-
MSG("open uart fail.\n");
-
return -1;
-
}
-
-
tcgetattr(fd, &opt);
-
-
switch(gUartInfo[port].mMode){
-
case UART_9600_8N1:{
-
cfsetispeed(&opt, B9600);
-
cfsetospeed(&opt, B9600);
-
-
opt.c_cflag &= ~PARENB;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag |= CS8;
-
break;
-
}
-
case UART_9600_8O1:{
-
cfsetispeed(&opt, B9600);
-
cfsetospeed(&opt, B9600);
-
-
opt.c_cflag |= PARENB;
-
opt.c_cflag |= ~PARODD;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag |= CS8;
-
break;
-
}
-
case UART_9600_8E1:{
-
cfsetispeed(&opt, B9600);
-
cfsetospeed(&opt, B9600);
-
-
opt.c_cflag |= PARENB;
-
opt.c_cflag &= ~PARODD;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag |= CS8;
-
break;
-
}
-
-
case UART_115200_8N1:{
-
cfsetispeed(&opt, B115200);
-
cfsetospeed(&opt, B115200);
-
-
opt.c_cflag &= ~PARENB;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag |= CS8;
-
-
break;
-
}
-
case UART_115200_8O1:{
-
cfsetispeed(&opt, B115200);
-
cfsetospeed(&opt, B115200);
-
-
opt.c_cflag |= PARENB;
-
opt.c_cflag |= ~PARODD;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag |= CS8;
-
break;
-
}
-
case UART_115200_8E1:{
-
cfsetispeed(&opt, B115200);
-
cfsetospeed(&opt, B115200);
-
-
opt.c_cflag |= PARENB;
-
opt.c_cflag &= ~PARODD;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag |= CS8;
-
break;
-
}
-
-
default:{
-
return -1;
-
//break;
-
}
-
}
-
-
opt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
-
opt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
-
opt.c_oflag &= ~(OPOST);
-
-
opt.c_cc[VMIN] = 200;
-
opt.c_cc[VTIME] = 1;
-
-
if(tcsetattr(fd, TCSANOW, &opt) < 0){
-
printf("set attr error.\n");
-
return -1;
-
}
-
-
tcflush(fd, TCIOFLUSH);
-
-
return fd;
-
}
-
-
int stream_parse( uint8_t *p, int len ){
-
UartMsgInfo_t msg;
-
int ret;
-
while(len--){
-
ret = recv_packet(&gUartSlip, *p++);
-
if(ret == 1){
-
if(gUartSlip.mBuffIdx){
-
//q
-
msg.mType = PORT_MSG_TYPE_RCV; //must not be zero
-
msg.mPayLoad.mLen = gUartSlip.mBuffIdx;
-
memcpy(msg.mPayLoad.mData, gUartSlip.mBuff, gUartSlip.mBuffIdx);
-
if((msgsnd(gMsgId, (void *)&msg, sizeof(UartMsgPayLoad_t), IPC_NOWAIT)) < 0){
-
printf("port%d rcv msg send error.\n", gNumPort+1);
-
}
-
else{
-
printf("port%d post rcv msg ok.\n", gNumPort+1);
-
}
-
}
-
}
-
}
-
return 0;
-
}
-
-
void *port_read_thread(void *arg){
-
int len;
-
int iRet;
-
fd_set readSet;
-
uint8_t rcvBuff[UART_EACH_BUFF_RCV_MAX];
-
-
-
while(1){
-
FD_ZERO(&readSet);
-
FD_SET (gPortFd, &readSet);
-
-
iRet = select(gPortFd + 1, &readSet, NULL, NULL, NULL);
-
if(iRet == -1){
-
MSG("select error.\n");
-
exit(-1);
-
}
-
else if(iRet == 0){
-
MSG("No data, exit.\n");
-
break;
-
}
-
else{
-
//
-
if(FD_ISSET(gPortFd, &readSet)){
-
MSG("Port %d Rcv Data.\n", gNumPort+1);
-
len = read(gPortFd, rcvBuff, UART_EACH_BUFF_RCV_MAX);
-
#if 0 //test
-
for(j = 0; j < len; j++)
-
MSG(" %02x", rcvBuff[j]);
-
MSG("\n");
-
#endif
-
if(len > 0){
-
stream_parse( rcvBuff, len );
-
}
-
}
-
}
-
}
-
pthread_exit(NULL);
-
}
-
-
-
void *port_write_thread(void *arg){
-
UartMsgInfo_t msg;
-
int i;
-
int ret;
-
while(1){
-
if((ret = msgrcv(gMsgId, (void *)&msg, sizeof(UartMsgPayLoad_t), PORT_MSG_TYPE_SND, 0)) == -1){
-
printf("port%d get snd msg error.\n", gNumPort+1);
-
}
-
else{
-
printf("sMsg: type = %d, payloadLen = %d.\n", msg.mType, msg.mPayLoad.mLen);
-
printf(" data=");
-
for(i = 0; i < msg.mPayLoad.mLen; i++)
-
printf(" %02x", msg.mPayLoad.mData[i]);
-
printf("\n");
-
-
if((ret = write(gPortFd, msg.mPayLoad.mData, msg.mPayLoad.mLen)) < 0){
-
MSG("port%d send error.\n", gNumPort+1);
-
}
-
else{
-
MSG("port%d send ok.\n", gNumPort+1);
-
}
-
}
-
}
-
-
}
-
-
-
int main(int argc, char const *argv[])
-
{
-
/* code */
-
int key;
-
int ret;
-
pthread_t stid,rtid;
-
-
-
if(argc != 2){
-
MSG("argc error.\n");
-
return -1;
-
}
-
-
gNumPort = atoi(argv[1]);
-
if( (gNumPort > UART_NUM_MAX) || (gNumPort == 0)){
-
MSG("serial port not existed.\n");
-
return -1;
-
}
-
gNumPort -= 1;
-
-
if((key = ftok(gUartInfo[gNumPort].mPath, UART_MSG_KEY)) == -1){
-
MSG("port%d msg key error.\n", gNumPort+1);
-
return -1;
-
}
-
-
if((gMsgId = msgget(key, IPC_CREAT|0600)) == -1){
-
MSG("prot%d msg error.\n", gNumPort+1);
-
return -1;
-
}
-
printf("port%d msg ok. qid = %d\n", gNumPort+1, gMsgId);
-
-
-
-
-
if((gPortFd = open_serial(gNumPort)) <= 0){
-
MSG("open serial port%d error.\n", gNumPort+1);
-
return -1;
-
}
-
MSG("open serial %d ok.[fd=%d].\n",gNumPort+1, gPortFd);
-
-
ret = pthread_create(&rtid, NULL, port_read_thread, NULL);
-
if(ret != 0){
-
MSG("port%d create port_read_thread error.\n", gNumPort+1);
-
return -1;
-
}
-
-
ret = pthread_create(&stid, NULL, port_write_thread, NULL);
-
if(ret != 0){
-
MSG("port%d create port_write_thread error.\n", gNumPort+1);
-
return -1;
-
}
-
-
-
pthread_join(rtid, NULL);
-
pthread_join(stid, NULL);
-
-
if(msgctl(gMsgId, IPC_RMID, NULL) < 0){
-
MSG("port%d msg ctl error.\n", gNumPort+1);
-
}
-
-
return 0;
- }