📄 commonitor.c
字号:
/*File Name: ComMonitor.c*/
#include <pthread.h>
#include "Ldb.h"
#include "LocoSystem.h"
#include "LdbCom.h"
#include "ComMonitor.h"
#include "SoundDesc.h"
#include "LocoLog.h"
/**************************************************/
/*串口监视线程*/
pthread_t f_commThrd;
/*录音停止标志*/
char f_commStop;
/*录音线程函数*/
void *com_monitor(void *arg);
/*消息例子*/
char example_msg[40] = {0x39, 0x30, 0x06, 0x46, 0xD3, 0xEA, 0x01, 0x8C, 0x00, 0x00,
0x00, 0x00, 0x0A, 0x04, 0x03, 0x4E, 0xDD, 0x00, 0x4D, 0x00,
0x16, 0x00, 0x44, 0x00, 0x08, 0x4f, 0x06, 0x08, 0x00, 0x00,//0x52, 0xDE,
0x00, 0x00, 0xEF, 0x09, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00 };
/**************************************************/
//消息标志字符
const char f_commMsg[]={COMM_MSG_TEST,
COMM_MSG_RESUME,
COMM_MSG_PAUSE,
COMM_MSG_SAVE,
COMM_MSG_DISCONNECT,
COMM_MSG_ERR
};//COMM_MSG_EXIT
//与消息对应的命令
const int f_commCmd[]={CMD_RCD_TEST,
CMD_RCD_RESUME,
CMD_RCD_PAUSE,
CMD_RCD_SAVE,
CMD_RCD_UDISCONNECT,
CMD_U_SHORT,
CMD_SYS_EXIT};
/*命令个数*/
#define CMD_COUNT sizeof(f_commMsg)/sizeof(char)
/**************************************************/
int loco_commStart()
{
int comfd = -1;
int timeout = 0;
//打开串口
while(comfd == -1)
{
comfd = ldb_comOpen(SYS_COM_NAME);
if (-1 == comfd)
{
LDB_LOG_MSG("Can not open %s, try 2 seconds later.", SYS_COM_NAME);
sleep(2);
timeout += 2;
if (timeout >= 60)
{
log_info("can not open comm");
return -1;
}
}
}
//创建监视线程
if( pthread_create(&f_commThrd, NULL, com_monitor, NULL) != 0)
{
log_info("can not create com monitor thread");
return -1;
}
f_commStop = 0;
return 0;
}
void loco_commStop()
{
//关闭串口
ldb_comClose();
//等待线程结束
f_commStop = 1;
if ( pthread_join(f_commThrd, NULL) != 0)
{
LDB_LOG_MSG("com monitor thread exits abnormally");
}
}
int loco_commIsStop()
{
return f_commStop;
}
int com_getCmd(int msg)
{
int i;
for (i=0; i<CMD_COUNT; i++)
{
if ( msg == f_commMsg[i] )
return f_commCmd[i];
}
return -1;
}
//回复串口命令
int loco_commReply(char msg)
{
//简单回复
return ldb_comPutc(msg);
}
/************************************************
串口监视线程
*************************************************/
void *com_monitor(void *arg)
{
int c, ret;
int cmd;
S_BroadcastMsg msg;
char log[64];
int log_size = 64;
#ifdef _TEST
memset(&msg, 0, sizeof(S_BroadcastMsg));
memcpy(&msg, example_msg, 40);
snd_descSetBroadcastMsg(&msg);
#endif
while(f_commStop != 1)
{
//read char
c = ldb_comGetc();
if (c == -1)
break;
if (c != COMM_MSG_DATA)
{
cmd = com_getCmd(c);
if (cmd == -1)
continue;
memset(log, 0, log_size);
sprintf(log, "COMM received command: %c", c);
log_info(log);
loco_cmdHandler(cmd, NULL);
#ifdef _DEBUG
if (cmd == CMD_SYS_EXIT)
break;
#endif
}
else
{
memset(&msg, 0, sizeof(S_BroadcastMsg));
//读取其余的40字节,然后分析
ret = ldb_comGet(msg.data, 40);
if ( ret == sizeof(S_BroadcastMsg) )
{
//接收成功
snd_descSetBroadcastMsg(&msg);
}//if (ret == ...
}
}
ldb_comClose();
f_commStop = 1;
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -