- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我在一个项目中使用两个 Xbee ZB (S2B) 将数据从一个传输到另一个。它是一个8数据位,无奇偶校验,1停止位系统(8N1)。
我有两个问题。
1. 由于我在笔记本电脑上将 RS232(DB9 连接器)接口(interface)连接到 USB 适配器,以 B230400 的波特率将系统调用 fwrite/fread/fopen/fclose 比 write/read/open/close 更好用吗?(我假设 fread() 不会正常工作,因为它没有波特率据我所知,速率配置。)
2. 我让一台计算机运行一个程序(发射器程序)和另一个程序(接收器程序)。这两个程序都需要能够读写,因为我希望能够从发送方和接收方了解不同的信息集。
像接收方(如果我看到字母 Q,停止并关闭端口)或发送方(等待读取方发送字母 G 开始写入并在写入完成后关闭)。
在非规范输入处理下使用 ( http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html ) 中的一些代码,我尝试设置一个基本模板,用于从任一侧的 USB 端口写入和读取。我的问题是:
*是否需要对传输和接收程序的 termios 结构 (newtio) 进行不同的配置设置(c_cflag、c_iflag、c_oflag、c_lflag)?*
我的代码:
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
int open_port(char *path, int modes)
{
int fd; // File descriptor for port
fd = open(path, modes); // Open the path
if(fd == -1) return -1; // Could not be opened
printf("Port %s opened successfully.\n", path);
return fd; // Return fd
}
int setBaud(struct termios *termios_p, int baud)
{
int ires = 0; int ores = 0;
switch(baud)
{
0:
ires = cfsetispeed(&termios_p, B0);
ores = cfsetispeed(&termios_p, B0);
break;
50:
ires = cfsetispeed(&termios_p, B50);
ores = cfsetispeed(&termios_p, B50);
break;
75:
ires = cfsetispeed(&termios_p, B75);
ores = cfsetispeed(&termios_p, B75);
break;
110:
ires = cfsetispeed(&termios_p, B110);
ores = cfsetispeed(&termios_p, B110);
break;
134:
ires = cfsetispeed(&termios_p, B134);
ores = cfsetispeed(&termios_p, B134);
break;
150:
ires = cfsetispeed(&termios_p, B150);
ores = cfsetispeed(&termios_p, B150);
break;
200:
ires = cfsetispeed(&termios_p, B200);
ores = cfsetispeed(&termios_p, B200);
break;
300:
ires = cfsetispeed(&termios_p, B300);
ores = cfsetispeed(&termios_p, B300);
break;
600:
ires = cfsetispeed(&termios_p, B600);
ores = cfsetispeed(&termios_p, B600);
break;
1200:
ires = cfsetispeed(&termios_p, B1200);
ores = cfsetispeed(&termios_p, B1200);
break;
1800:
ires = cfsetispeed(&termios_p, B1800);
ores = cfsetispeed(&termios_p, B1800);
break;
2400:
ires = cfsetispeed(&termios_p, B2400);
ores = cfsetispeed(&termios_p, B2400);
break;
4800:
ires = cfsetispeed(&termios_p, B4800);
ores = cfsetispeed(&termios_p, B4800);
break;
9600:
ires = cfsetispeed(&termios_p, B9600);
ores = cfsetispeed(&termios_p, B9600);
break;
19200:
ires = cfsetispeed(&termios_p, B19200);
ores = cfsetispeed(&termios_p, B19200);
break;
38400:
ires = cfsetispeed(&termios_p, B38400);
ores = cfsetispeed(&termios_p, B38400);
break;
57600:
ires = cfsetispeed(&termios_p, B57600);
ores = cfsetispeed(&termios_p, B57600);
break;
115200:
ires = cfsetispeed(&termios_p, B115200);
ores = cfsetispeed(&termios_p, B115200);
break;
230400:
ires = cfsetispeed(&termios_p, B230400);
ores = cfsetispeed(&termios_p, B230400);
break;
default:
ires = cfsetispeed(&termios_p, B9600);
ores = cfsetispeed(&termios_p, B9600);
break;
}
if(ires == -1 | ores == -1) return -1; // ISpeed or OSpeed could not be set
else { printf("Baud set successfully to %d\n", baud); return 0; }
}
char* readIt(int fd)
{
char buf;
char *tmp;
tmp = calloc(2, sizeof(char);
if( read(fd, buf, 1) == 1 ) { tmp[0] = buf; tmp[1] = 0; } // Return char in tmp[0]
else { tmp[0] = 0x45; tmp[1] = 0x52; } // Return ER in char meaning error
return tmp;
}
int writeIt(int fd, char *str, int bytes)
{
if( write(fd, str, bytes) == bytes ) return 0; // Write success
else return -1; // Failed write
}
int main (int argc, char **argv)
{
int usb;
volatile int STOP = 0;
if(argc == 1) { printf("Must enter usb port path.\n"); exit(EXIT_FAILURE); } // No second argument
else if (argc > 1) { usb = open_port(argv[1], O_RDWR | O_NOCTTY ); }
struct termios oldterm, newterm;
tcgetattr(fd, &oldtio); // Save old terminal settings
bzero(&newtio, sizeof(newtio)); // Clear out struct
if( setBaud(&newtio, 230400) != 0 ) { printf("Baud could not be set\n"); exit (EXIT_FAILURE); } // Set up baud rate
newtio.c_cflag = CS8 | CREAD | CLOCAL; // 8 data bits, enable receiver, local line
newtio.c_cflag &= ~PARENB | ~CSTOPB; // No parity, one stop bit
newtio.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable software flow control
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; // No timeout clock for read
newtio.c_cc[VMIN] = 1; // Wait for one character before reading another
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio); // Set fd with new settings immediately
/* Do writing or reading from port in here - readIt or writeIt*/
tcsetattr(fd, TCSANOW, &oldtio); // Set fd with old settings immediately
close(fd); // Close fd
return 0;
}
如果可能,我目前正在寻找没有数据损坏处理的最佳写入和读取速率。通过优化,我说的是 termios 结构的配置标志的最佳设置。
感谢您的帮助。
最佳答案
关于第一点,使用 fdopen
从文件描述符中获取 FILE*
很容易.但是,您必须记住 FILE*
默认情况下是缓冲的,因此您写入的内容可能不会立即发送。
关于c - 串行编程 : Sender and Receiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21014662/
我对 Objective-C 很陌生,如果这对你们中的许多人来说非常明显,我很抱歉,但我正在尝试弄清楚以下代码是如何实际工作的: - (IBAction)chooseColour:(UIButton
我有一个 UITableView,它从代表标题、摘要和目标 URL 的字符串数组中获取数据。当用户选择一个单元格时,应用程序会转到包含 WebView 的 View Controller 。 问题是无
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: C# “as” cast vs classic cast 这两个表达式有什么区别? (ListView)发送
目前正在学习objetive-c。浏览了各种书籍以及 lynda 网站上的一些教程,现在找到了我认为适合我学习风格的斯坦福类(class)。我一直在一遍又一遍地看前 3 个讲座,基本上是在做,直到我
当我为我的业务对象编写公共(public)事件时,除了额外的特定参数外,我已经习惯了始终将实例作为“发送者作为对象”传递。我现在只是问自己为什么我不指定类? 所以对于有更多经验的你; 您是否曾经在事件
在 Objective-C 中,“(UIButton *)sender”是什么意思,为什么不是“UIButton *sender”?或者一些 NSObject 代替 UIButton。这更多是关于星号
我正在尝试学习用于 iPhone 开发的 Objective-C,并且在浏览有关操作的示例时,我经常看到如下内容: - (IBAction)sliderMoved:(id)sender { U
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: Objective C: what is a “(id) sender”? in 的 (id)sender 是
我已经查看了我能找到的关于这个问题的所有文档,但无法弄清楚这一点。 一定有人在使用 Sendgrid 作为 SMTP 用于话语博客,但我不知道如何做到。 这是我的 app.yml 设置: env:
当我将按钮链接到源代码(在本例中为 .h 文件)并选择 NSButton 作为类型时,Xcode 将写入: - (IBAction)pushClear:(id)sender; 据我了解应该是 - (I
我有一个代码可以使用 randomLabel 作为触发事件的 sender。在此代码中,我尝试调整 sender 的字体属性,但它似乎给出了编译错误。有什么办法可以解决这个问题吗? priva
我有一个包含 16 个 TCheckBox 和 32 个 TEditBox 的表单。每 2 个 TEditBox 的启用取决于复选框的状态。所以我使用了这段太长的代码: //T1 procedure
我正在编写一个 Chrome 扩展,其中内容脚本向后台脚本发送消息。 chrome.runtime.sendMessage({greeting: "hello"}, function(response
我对 Winform 控件中 sender 参数的用途感到困惑,例如: Private Sub Form1_Load(sender As System.Object, e As System.Even
我有以下代码用当前时间更新文本字段的值。我的问题是:为什么在代码的底部发送 nil 作为发件人? [self showCurrentTime:nil]; CurrentTimeViewControll
我编写了一个 Java 线程(可运行)来基本上存储 DatagramPackets 列表,如果列表的大小 > 0,它将发送列表中的第一项。 看来,除非我在 run 方法中放置大量 sleep ,否则线
我正在使用位于 github here 上的 pjjanak 的 chromecast-hello-world 应用程序测试我的 chromecast . 他向 message 事件添加了一个监听器,
当我在 Laravel 5.1 中重置密码时,我收到了电子邮件,但标题中显示“无发件人”。 有没有办法从某处指定密码重置电子邮件的发件人?除了发件人,我假设 Laravel 应该自动使用配置文件中指定
我总是收到此错误,而且我不知道还要输入什么。 你们知道这是怎么回事吗? @IBAction func loginAction(sender: AnyObject) { let email =
我想制作一个按钮,使其在点击后保持按下状态。所以我制作了一个插槽 make_pushed ,我试图将其用于该目的。单击的按钮由 QObject::sender() 方法标识。但是出了点问题,因为它不起
我是一名优秀的程序员,十分优秀!