串行终端上的二进制数据 [英] Binary data over serial terminal

查看:22
本文介绍了串行终端上的二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与嵌入式设备的唯一通信方式是串行端口.默认情况下,嵌入式 Linux 将此端口用作终端.如何禁用此终端并使用串行链接传输二进制数据?我听说过 rx 和 tx 之类的命令,但我找不到它们.

My only way of communication with my embedded device is a serial port. By default, embedded Linux uses this port as a terminal. How do I disable this terminal and use the serial link to transfer binary data? I heard of commands like rx and tx but i cannot find them.

我想我可以从/dev/tty 读取()和写入()内容,但我想确保没有错误消息或我的数据流出现任何混乱.

I think I can just read() from and write() stuff to /dev/tty but I want to make sure no error messages or whatever mess with my data stream.

推荐答案

您可以使用 xmodem 等应用程序通过任何终端传输文件.您说的串行端口是终端,还是内核控制台.

You can use an application like xmodem to transfer file over any terminal. Is the serial port you speak off a terminal, or is it also the kernel console.

如果您的内核不嘈杂,那么您可以使用您当前的连接使 xmodem 像传输一样.在主机端,您可以使用 kermit,它非常好且可编写脚本.

If you're kernel is not noisy, then you can use your current connection to make xmodem like transfer. On the host side, you can use kermit, which is nice AND scriptable.

如果你想让你的串口原始,并且你打开了文件描述符 ttyfd,这是一种方法:

If you want to make your serial port raw, and you have file descriptor ttyfd opened, here is one way to do it :

struct termios tty, orig_tty;

...

if(tcgetattr(ttyfd, &tty) < 0)
{
    // error checking
}
// backup tty, make it raw and apply changes
orig_tty = tty;
cfmakeraw(&tty);
if(tcsetattr(ttyfd, TCSAFLUSH, &tty) < 0)
{
    // error checking
}

...
//end of program or error path :
tcsetattr(ttyfd, TCSAFLUSH, &orig_tty)

如果您仍然想要一个行为良好的终端,请不要忘记在程序结束时恢复设置.

Don't forget to restore the setting at the end of your program if you still want a good behaved terminal.

这篇关于串行终端上的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆