如何编写用于在环回模式下建立串行通信的C代码。 [英] How do I write a C-code for establishing serial communication in loopback mode.

查看:78
本文介绍了如何编写用于在环回模式下建立串行通信的C代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写交流代码,从笔记本电脑中串行传输数据并在同一台笔记本电脑上接收。



我最终想从文件中读取(.dat文件)并将其写回新文件(.dat文件)。



后来使其实时工作:通过一些外部源我得到了我的数据(之前存储在一个dat文件中)并直接将我的数据流式传输到环回,而无需将其存储在文件中并从中读取。



我尝试了什么:



我有一个usb到串行电缆,我已将发送器和接收器引脚连接在一起进行环回。我还需要更多连接吗?



我的代码似乎传输数据很好(我怎么能确定?)。

但是不读它。



我的程序流程:

1)开放端口(读取写):fd = open(/ dev / ttyUSB0,O_RDWR | O_NOCTTY | O_NDELAY)

2)设置termios的属性

3)将数据写入Tx。 (代码本身预定义的数据)

4)读取数据:

而(1)

{bytes_read = read(fd,& read_buffer,10);

if(bytes_read == -1)

switch(errno){

case EAGAIN:continue;

默认:goto退出;

}

}

程序停留在此循环中。



谢谢。

I want to write a c code to transfer data serially out from my laptop and receive it back on the same laptop.

I eventually want to read from a file (.dat file) and write it back to a new file (.dat file) .

And later make it work in real time that is : Through some external source i get my data (which is stored in a dat file previously) and directly stream my data to loopback without having the need to store it in a file and read from it.

What I have tried:

I have a usb to serial cable and I've connected the transmitter and receiver pins together for loopback. Do i need any more connection?

My code seems to transmit data fine (how can i be sure of that? ).
But is not reading it.

The flow of my program :
1) open port (read write): fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY)
2) Set attributes of termios
3) Write data to Tx . (Data predefined in the code itself)
4) Read data :
while(1)
{ bytes_read =read(fd,&read_buffer,10);
if (bytes_read == -1)
switch(errno) {
case EAGAIN : continue;
default : goto quit;
}
}
The program is stuck in this loop.

Thank You.

推荐答案

请参阅本手册: POSIX操作系统的串行编程指南



-SA


我还需要更多连接吗?

取决于你如何配置端口(有时是硬件):用于正确的环回,将RTS连接到CTS(9pin串行连接器上为7到8),将DTR连接到DSR和DCD(9针上的1,4,6)以绕过任何硬件握手,并将RTS和DTR设置为高电平,这是一个好主意。

该计划陷入困境这个循环

这可能是因为你坚持一次读取10个字节 - 所以如果你只发送9个,它将永远不会超过读取呼叫。

我会逐字节地读取(特别是在开头)并将响应缓冲到一个完整的命令中,而不是假设它始终是完整和正确的。

但是......由于您在打开端口时指定了O_NDELAY,如果没有足够的数据(即所有十个字节),它将立即返回EAGAIN。由于Serial通常很慢(9600波特大约是每秒960个字符)并且处理器速度很快,所以在写操作之后所有(甚至任何)数据都不可能立即回送。



首先检查你的环回是否适用于终端程序,然后通过逐字符发送和接收来检查你的代码。
"Do i need any more connection?"
Depends on how you have configured the port (and sometimes the hardware): for a "proper" loopback, it's a good idea to connect RTS to CTS (7 to 8 on 9pin serial connector), and DTR to DSR and DCD (1, 4, 6 on 9pin) to bypass any hardware handshaking, and to set RTS and DTR high.
"The program is stuck in this loop"
That may be because you are insisting it reads 10 bytes at a time - so if you only send 9, it will never get past the Read call.
I'd read byte-by-byte (especially in the beginning) and buffer up responses into a complete "command" rather than assuming it will be complete and correct at all times.
But...since you specify O_NDELAY when you open the port, if sufficient data isn't available (i.e all ten bytes) it will return EAGAIN immediately. Since Serial is generally slow (9600 baud is around 960 characters per second) and processors are fast, it's unlikely that all (or even any) of the data has looped back immediately after the write operation.

Start by checking your loopback works with a terminal program, then check your code by sending and receiving character by character.


这篇关于如何编写用于在环回模式下建立串行通信的C代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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