参考链接: http://blog.****.net/zydlyq/article/details/50963360
#include "../include/CommUart.h"
#include "ComCommon.h"
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h> #include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h> using namespace std; #define USR_DEBUG static struct termios ori_attr, cur_attr; static __inline int tty_reset(void)
{
if (tcsetattr(STDIN_FILENO, TCSANOW, &ori_attr) != )
return -; return ;
} static __inline int tty_set(void)
{ if ( tcgetattr(STDIN_FILENO, &ori_attr) )
return -; memcpy(&cur_attr, &ori_attr, sizeof(cur_attr) );
cur_attr.c_lflag &= ~ICANON;
// cur_attr.c_lflag |= ECHO;
cur_attr.c_lflag &= ~ECHO;
cur_attr.c_cc[VMIN] = ;
cur_attr.c_cc[VTIME] = ; if (tcsetattr(STDIN_FILENO, TCSANOW, &cur_attr) != )
return -; return ;
} static __inline int kbhit(void)
{ fd_set rfds;
struct timeval tv;
int retval; /* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = ;
tv.tv_usec = ; retval = select(, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */ if (retval == -) {
perror("select()");
return ;
} else if (retval)
return ;
/* FD_ISSET(0, &rfds) will be true. */
else
return ;
return ;
} int main(int argc, char*argv[]) { //非阻塞getChar初始化
int tty_set_flag;
tty_set_flag = tty_set(); char com_num[] = {};
if (argc < ) {
printf("please enter com num:\n");
scanf("%c", &com_num);
} else {
// com_num = *argv[1];
strcpy(com_num, argv[]);
} #ifdef USR_DEBUG
printf("main entry\n");
#endif // USR_DEBUG
CComCommon *m_pComUart = new CCommUart;
#ifdef USR_DEBUG
printf("create CCommUart\n");
#endif // USR_DEBUG char sendStr[] = {};
strcpy(sendStr, "/dev/ttyS100");
strcat(sendStr, com_num);
strcat(sendStr, ",115200,8,1,n");
// m_pComUart->InitComComm("/dev/ttyS1001,115200,8,1,n");
m_pComUart->InitComComm(sendStr); // strcat(sendStr,"kkkkkkkkkkkkkkkkkk"); // 写入文件 FILE * pFile;
char fileName[] = {};
sprintf(fileName,"readData_dir/%s.txt",com_num);
pFile = fopen (fileName, "w"); char Rbuff[] = {};
int nsize = ;
int i = ;
int nCnt = ;
while () { /*********************发送测试*******************************/
// m_pComUart->SendBuff(buff,sizeof(buff));
// m_pComUart->SendStr("1234567887654321abcdefgh");
// m_pComUart->SendStr("kkkkkkkkkkkkkkkkkk"); if (kbhit()) {
const int key = getchar();
// printf("%c pressed\n", key);
if (key == 'q')
break;
} /*else {
fprintf(stderr, "<no key detected>\n");
}*/ /*********************接收的测试案例*************************/
memset(Rbuff, , );
nsize = m_pComUart->GetData(Rbuff, , );
if (nsize > )
{
printf("nsize:%d\n", nsize); // if(nCnt==10){
for (i = ; i < nsize; i++)
{ // printf("%02x ", (unsigned char)Rbuff[i]); fprintf(pFile, "%02x", (unsigned char)Rbuff[i]);
if (nCnt == ) {
fprintf(pFile, "\n");
nCnt = ;
} else {
nCnt++;
} } // 二进制输入
// fwrite (Rbuff , sizeof(char), sizeof(Rbuff), pFile); // nCnt = 0;
// }else{
// nCnt++;
// }
printf("\n"); // printf("%s\n", Rbuff); } // printf("rec buff data:%s\n", Rbuff);
// printf("get char: %d\n", m_pComUart->GetChar(10)); // sleep(1);
// usleep(1000);
}
delete m_pComUart; fclose (pFile); // 线程的操作
if (tty_set_flag == )
tty_reset(); }