- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一台通过 RS485 连接到另一台设备的单板计算机。计算机应向设备发送请求并接收响应(使用设备相关协议(protocol))。我可以毫无问题地发送消息并且设备接收它们(例如,我可以更改设备的参数)。当我想从设备读取参数时出现问题。在这种情况下,我收到了错误的回复(错误的字符、移位的消息、不完整的消息……)。
这是我的初始化代码:
Bool
SerialCommunicator::initPort()
{
if (isInitialized_)
return true;
if (!paramSet())
return false;
bzero( &termIO_, sizeof ( struct termios ));
termIO_.c_iflag |= IGNBRK | IGNPAR;
termIO_.c_cflag |= CREAD | CLOCAL;
termIO_.c_cflag |= CS8;
termIO_.c_oflag |= 0;
termIO_.c_lflag |= 0;
termIO_.c_cc[VTIME] = 0;
termIO_.c_cc[VMIN] = 13; // number of frame characters
String path("/dev/tty" + portSuffix_);
serHandle_ = open(path.c_str(), O_RDWR /*| O_NOCTTY*/);
if (serHandle_ > -1)
{
isInitialized_ = (cfsetispeed(&termIO_, B19200) == 0)
&& (cfsetospeed(&termIO_, B19200) == 0);
isInitialized_ = isInitialized_ && (tcsetattr(serHandle_, TCSANOW, &termIO_) == 0);
return isInitialized_;
}
else
return false;
}
发送代码:
Bool
SerialCommunicator::sendFrame(UByte *_frame, UInt _size)
{
FD_ZERO( &wrFd_ );
FD_ZERO( &rdFd_ );
FD_SET( serHandle_, &wrFd_);
FD_SET( serHandle_, &rdFd_);
Int retVal;
aux_gpio_write_settings();
retVal = select(serHandle_+1, &rdFd_, &wrFd_, NULL, &timeval_);
if (retVal > 0)
{
if( FD_ISSET(serHandle_, &wrFd_) )
{
UInt bytesToSend = _size;
UInt bytesSent = 0;
UInt bytesSentTotal = 0;
while ( bytesToSend > 0 )
{
bytesSent = write( serHandle_, _frame + bytesSentTotal, bytesToSend );
if (bytesSent > 0)
{
bytesToSend -= bytesSent;
bytesSentTotal += bytesSent;
}
}
aux_gpio_read_settings();
tcflush(serHandle_, TCIOFLUSH);
return true;
}
}
usleep(SLEEPTIME);
return false;
}
接收码:
Bool
SerialCommunicator::receiveFrame(UByte *_frame, UInt _size)
{
FD_ZERO( &rdFd_ );
FD_ZERO( &wrFd_ );
FD_SET( serHandle_, &rdFd_ );
FD_SET( serHandle_, &wrFd_ );
Bool retVal;
aux_gpio_read_settings();
retVal = select(serHandle_+1, &rdFd_, &wrFd_, NULL, &timeval_);
if (retVal > 0)
{
if( FD_ISSET(serHandle_, &rdFd_) )
{
UInt bytesToReceive = _size;
UInt bytesReceived = 0;
UInt bytesReceivedTotal = 0;
while ( bytesToReceive > 0 )
{
bytesReceived = read( serHandle_, _frame + bytesReceivedTotal, bytesToReceive );
if (bytesReceived > 0)
{
bytesToReceive -= bytesReceived;
bytesReceivedTotal += bytesReceived;
}
}
return true;
}
}
return false;
}
函数 aux_gpio_write_settings()
和 aux_gpio_read_settings()
用于设置 UART(通过 GPIO)s.t. RS485 可以发送或接收数据。
如果我在我的 linux 台式电脑上使用代码,它工作正常,因为 USB/RS485 适配器会自动在发送和接收模式之间切换。在我的单板计算机上,我必须手动完成。因此,我认为设置 GPIO 和接收响应会导致时序问题。我该如何处理这个问题?
最佳答案
这可能与您在 receiveFrame
中的错误有关读循环。当您收到的数据少于所需的数据量时,您会减少循环中下一次接收的数据量,但是当使用与前一个循环相同的指针时,您会覆盖您读取的第一个数据。您需要增加指针并减小尺寸:
bytesReceived = read( serHandle_, _frame, bytesToReceive );
if (bytesReceived > 0)
{
bytesToReceive -= bytesReceived;
_frame += bytesReceived;
}
您在发送数据时遇到了类似的问题,您一遍又一遍地发送相同的数据,只是每次发送的数据更少。
我还建议您实际检查错误(当 bytesReceived < 0
时),并适本地处理这些情况。
关于c++ - 通过 RS485 通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18082073/
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
有没有办法只允许 https 而不是 http 与 Neo4j 服务器通信?另外,Neo4j Shell 的通信使用哪个 channel ,http 还是 https? 最佳答案 这来自 Neo4j
您好,我有新问题 :) 我正在构建带有面板的简单时事通讯,以向注册用户和时事通讯邮件地址发送邮件。 我有此代码,但时事通讯仅发送给注册用户。谁能告诉我为什么? $zapytanie = mys
第一次发帖,所以可能会有比必要的更多的信息,但我想彻底: 我们的 C 练习之一是创建发送器和接收器程序,通过 RS232 串行通信与零调制解调器交换数据。我们使用了虚拟端口程序(如果你想测试的话,我使
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我有一台通过 RS485 连接到另一台设备的单板计算机。计算机应向设备发送请求并接收响应(使用设备相关协议(protocol))。我可以毫无问题地发送消息并且设备接收它们(例如,我可以更改设备的参数)
我目前正在尝试在我的 Visual Basic 6 应用程序中引用 .NET COM 库。我已经使用 Regasm 注册了它,并且在我的类(class)中将 ComVisible 设置为 true。但
Closed. This question needs to be more focused 。它目前不接受答案。 想改善这个问题吗?更新问题,使其仅通过 editing this post 关注一个
我尝试通过 https 协议(protocol)在 Archiva 和 Jenkins 之间建立通信,但我收到以下错误: [WARNING] Could not transfer metadata .
我完成了一个运行良好的客户端/服务器套接字通信程序。现在我想弄清楚如何做到这一点,以便我可以同时拥有到服务器的多个客户端连接。我环顾四周,似乎有不止几种不同的方法可以做到这一点。所以我来这里是想向你们
我正在 mailchimp 中制作时事通讯,我在使用 outlook 时遇到了这个问题,它一直在干扰我的两个专栏,如图所示: 这是这部分的代码:
我正在创建一份时事通讯,经过大量努力,它在除 android 的 gmail 应用程序之外的任何地方都有效。问题是它似乎有最小字体大小,这会导致我的表格损坏。 有没有办法克服最小字体大小而不是媒体查询
C++ 作为Client端 view plaincopy to clipboardprint? 复制代码 代码如下: // Client.cpp : Defines the entry poi
我创建了 ECM NewsLetter,其中包含一些网站链接(另一个项目)。在在线版本的 NewsLetter 链接中工作正常,但是当我将此 NewsLetter 发送到我的电子邮件并尝试单击我的邮件
无论出于何种原因,我的文本大小调整在 iPhone 上无法正常工作,但在 Android 和其他电子邮件格式中工作正常。似乎看不出这两个文件之间有任何区别。图片也调整了大小,似乎只是文本的问题。
我正在开发一个网站,我需要将 HTML 新闻稿发送到邮件列表。 我构建了一个 html 框架,其中包含“在浏览器中查看”和“取消订阅”链接(原因会有所不同)。在管理模块中,我发布 html 并为 ht
我正在尝试在 B-L072Z-LRWAN(Master) 和 Arduino(Slave) 之间进行 I2C 通信。 我使用以下代码成功将数据从主站发送到从站: B-L072Z-LRWAN 代码: #
我有 ECM NewsLetter,其中包含指向站点(另一个项目)的一些链接。当我通过单击发送按钮将此时事通讯发送到我的电子邮件时 - 当我从电子邮件收件箱中单击它们时,链接不起作用。它显示 404
我是一名优秀的程序员,十分优秀!