www.pudn.com > ComMonitor.rar > ComMonitor.c
/*File Name: ComMonitor.c*/ #include#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