2012-09-20 57 views
2

在我發送字節到串口的程序中,我收到了我發送的字節。我不想接收我發送的字節,我不知道該怎麼做?linux C++串口回聲輸出

#include <stdio.h> 
#include <fcntl.h> 
#include <termios.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/time.h> 
#include <sys/select.h> 
#include <sys/ioctl.h> 

int fd; 
struct termios tio, old_tio; 

time_t SubTime_mSec(timeval val1, timeval val2) 
{ 
    timeval tv; 
    if (val1.tv_sec > val2.tv_sec) return (0); 
    if (val1.tv_sec == val2.tv_sec) 
     if (val1.tv_usec > val2.tv_usec) return (0); 
    tv.tv_sec = val2.tv_sec - val1.tv_sec; 
    if (val1.tv_usec < val2.tv_usec) { 
     tv.tv_usec = val2.tv_usec - val1.tv_usec; 
    } else { 
     tv.tv_sec --; 
     tv.tv_usec = 1000000 + val2.tv_usec - val1.tv_usec; 
    } 

    return(tv.tv_sec*1000 + tv.tv_usec/1000); 
} 


void RTUOutMessage(int cnt1, int cnt2) 
{ 
    unsigned char msg[13] = {0x01, 0x10, 0x00, 0x2F, 0x00, 0x02, 0x04, 0x4B, 0x64, 0x3D, 0xD9, 0x36, 0xC6}; 
    int Len = 13; 
    int status, i, j; 
    ioctl(fd, TIOCMGET, &status); 

    status = status | TIOCM_RTS | TIOCM_DTR; 
    ioctl(fd, TIOCMSET, &status); 
    write(fd, msg, Len); 
    for (j = 0; j < cnt1; j ++) 
     for (i = 0; i < cnt2; i ++); 
    ioctl(fd, TIOCMGET, &status); 
    status &= ~TIOCM_RTS; 
    status &= ~TIOCM_DTR; 
    ioctl(fd, TIOCMSET, &status); 

    timeval start_t, curr_t; 
    char Buff[80]; 
    int l; 

    gettimeofday(&start_t, NULL); 
    curr_t = start_t; 
    while (SubTime_mSec(start_t, curr_t) < 1000) { 
     l = read(fd, Buff, 80); 
     if (l > 0) { 
      printf("BUFFER="); 
      for(i = 0; i < l; i++) { 
       printf(" %x", Buff[i]); 
      } 
      //printf("\n"); 
     } 
     gettimeofday(&curr_t, NULL); 
    } 
} 

void InitPort(void) 
{ 
    int StopBits = 2; 
    if ((fd = open("/dev/ttyAM2", O_RDWR | O_NDELAY)) < 0) { 
     printf("Couldn't open //dev//ttyAM2\n"); 
    } 
    tcflush(fd, TCIOFLUSH); 
    int n = fcntl(fd, F_GETFL, 0); 
    fcntl(fd, F_SETFL, n & ~O_NDELAY); 
    tcgetattr(fd, &old_tio); 
    tcgetattr(fd, &tio); 
    cfsetospeed(&tio, (speed_t)B9600); 
    cfsetispeed(&tio, (speed_t)B9600); 
    tio.c_cflag = (tio.c_cflag & ~CSIZE) | CS8; 
    tio.c_cflag |= CLOCAL | CREAD ; 
    tio.c_cflag &= ~OFILL; 
    //parity 
    tio.c_cflag &= ~(PARENB | PARODD); 
    tio.c_cflag &= ~CRTSCTS; 
    if (StopBits == 2) tio.c_cflag |= CSTOPB; 
    else tio.c_cflag &= ~CSTOPB; 
    tio.c_iflag=IGNBRK; 
    tio.c_iflag &= ~(IXON|IXOFF|IXANY); 
    tio.c_lflag=0; 
    tio.c_oflag=0; 
    tio.c_cc[VTIME]=0; 
    tio.c_cc[VMIN]=0; 
    if (tcsetattr(fd, TCSANOW, &tio)!=0) printf("tcsetattr() 1 failed\n"); 

    int mcs=0; 
    ioctl(fd, TIOCMGET, &mcs); 
    mcs |= TIOCM_RTS; 
    ioctl(fd, TIOCMSET, &mcs); 

    if (tcgetattr(fd, &tio)!=0) printf("tcgetattr() 4 failed\n"); 
    tio.c_cflag &= ~CRTSCTS; 
    if (tcsetattr(fd, TCSANOW, &tio)!=0) printf("tcsetattr() 2 failed\n"); 
} 

int main(int argc, char **argv) 
{ 
    InitPort(); 
    int cnt1, cnt2; 
    cnt1 = 3; 
    cnt2 = 20000; 
    cnt1 = atoi(argv[1]); 
    cnt2 = atoi(argv[2]); 
    for(;;) { 
    RTUOutMessage(cnt1, cnt2); 
    usleep(1000000); 
    } 
    tcsetattr(fd, TCSANOW, &old_tio); 
    close(fd); 
    printf("End\n"); 

    return 0; 
} 
+3

,什麼是問題? –

+1

另外,你爲什麼翻倍斜線?它只需要反斜槓。例如。對於斜線(''/''),沒關係,對於反斜線(''\\''),你需要兩個。 –

+1

您需要查看tty設備上的本地回顯設置 - 對'tcsetattr()'的附加標記,例如'ECHO','ECHOE','ECHOK'等。 – twalberg

回答

2

如果您正在接收您發送的字符,那麼遠程的串口顯然會回顯其輸入。

您將不得不在其他設備上禁用輸入回聲,或者在接收邏輯中執行「回聲消除」。 I.E.創建一個「回聲FIFO」。輸出到串行端口的每個字節也寫入「回波FIFO」。設置一個標誌,指示預期有回聲。當接收到輸入時,將它與「回波FIFO」進行比較,並在匹配時移除字符,並拋棄接收字符。如果不匹配,則接受接收字符。當「回顯FIFO」爲空時清除回顯標誌。

順便說一句,代碼似乎將串口設置爲原始或非規範模式。首選的方法是使用

cfmakeraw(&tio); 

而不是

tio.c_iflag=IGNBRK; 
tio.c_iflag &= ~(IXON|IXOFF|IXANY); 
tio.c_lflag=0; 
tio.c_oflag=0;