linux 串口TIOCM_DTR,linux c++串口回顯輸出

In my program which sends bytes to a serial port, I receive bytes which I send. I don't want to receive bytes which I send, and I don't know how to do this?

在我的程序中,將字節發送到串行端口,我接收我發送的字節。我不想接收我發送的字節,我不知道怎么做?

#include

#include

#include

#include

#include

#include

#include

#include

#include

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;

}

1 个解决方案

#1

2

If you are receiving the chars that you sent, then the remote's serial port is apparently echoing its input.

如果您正在接收所發送的字符,那么遠程程序的串行端口顯然是在響應其輸入。

You will either have to disable input echo at the other device, or perform "echo cancellation" in your receive logic. I.E. create an "echo FIFO". Each byte output to the serial port is also written to the "echo FIFO". Set a flag indicating that an echo is expected. As input is received, compare it to the "echo FIFO", and remove chars when it matches, and toss the receive char. If no match, then accept receive char. Clear the echo flag when the "echo FIFO" is empty.

您將不得不在另一個設備上禁用輸入echo,或者在接收邏輯中執行“echo cancel”。創建一個“echo FIFO”。串行端口的每個字節輸出也被寫入“echo FIFO”。設置一個標志,指示期望出現回音。當接收到輸入時,將其與“echo FIFO”進行比較,並在匹配時刪除chars,並拋出接收char。如果沒有匹配,則接受接收字符。當“echo FIFO”為空時清除echo標志。

BTW the code appears to set the serial port to raw or non-canonical mode. The preferred method would be to use

順便說一下,代碼似乎將串行端口設置為原始模式或非規范模式。首選的方法是使用

cfmakeraw(&tio);

rather than

而不是

tio.c_iflag=IGNBRK;

tio.c_iflag &= ~(IXON|IXOFF|IXANY);

tio.c_lflag=0;

tio.c_oflag=0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值