- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用this usb gpio device 。它使用一些命令从输入/输出 channel 发送/接收数据。有一个guide这解释了如何在 numato 网站上发送命令。有some sample code适用于 Windows 上的 C。但我使用 fedora,我的代码如下用于读/写 gpio。我无法读取数据。
Windows 代码示例
#include "stdafx.h"
#include "windows.h"
#include "string.h"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hComPort;
char cmdBuffer[32];
char responseBuffer[32];
DWORD numBytesWritten;
DWORD numBytesRead;
/*
Lookup the port name associated to your GPIO device and update the
following line accordingly. The port name should be in the format
"\\.\COM<port Number>". Notice the extra slaches to escape slashes
themselves. Read http://en.wikipedia.org/wiki/Escape_sequences_in_C
for more details.
*/
wchar_t PortName[] = L"\\\\.\\COM14";
/*
Open a handle to the COM port. We need the handle to send commands and
receive results.
*/
hComPort = CreateFile(PortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hComPort == INVALID_HANDLE_VALUE)
{
printf("Error: Unable to open the specified port\n");
return 1;
}
/* EXAMPLE 1 - MANIPULATE GPIO0 BY SENDING COMMAND */
/**************************************************************************
Send a command to output a logic high at GPIO 0. The command that is
used to accomplish this acton is "gpio set 0". It is important to send
a Carriage Return character (ASCII value 0x0D) to emulate the ENTER
key. The command will be executed only when the GPIO module detects
Carriage Return character.
**************************************************************************/
/* Write a Carriage Return to make sure that any partial commands or junk
data left in the command buffer is cleared. This step is optional.
*/
cmdBuffer[0] = 0x0D;
if(!WriteFile(hComPort, cmdBuffer, 1, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/* Copy the command to the command buffer */
strcpy(cmdBuffer, "gpio set 0");
/* Append 0x0D to emulate ENTER key */
cmdBuffer[10] = 0x0D;
/* Write the command to the GPIO module. Total 11 bytes including 0x0D */
printf("Info: Writing command <gpio set 0> to the GPIO module\n");
if(!WriteFile(hComPort, cmdBuffer, 11, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
printf("Info: <gpio set 0> Command sent successfuly\n");
/* EXAMPLE 2 - MANIPULATE GPIO10 BY SENDING COMMAND */
/**************************************************************************
Send a command to output a logic high at GPIO 0. The command that is
used to accomplish this acton is "gpio set 0". It is important to send
a Carriage Return character (ASCII value 0x0D) to emulate the ENTER
key. The command will be executed only when the GPIO module detects
Carriage Return character.
**************************************************************************/
/* Write a Carriage Return to make sure that any partial commands or junk
data left in the command buffer is cleared. This step is optional.
*/
cmdBuffer[0] = 0x0D;
if(!WriteFile(hComPort, cmdBuffer, 1, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/*
Copy the command to the command buffer. GPIO number 10 and beyond are
referenced in the command by using alphabets starting A. For example
GPIO10 willbe A, GPIO11 will be B and so on. Please note that this is
not intended to be hexadecimal notation so the the alphabets can go
beyond F.
*/
strcpy(cmdBuffer, "gpio set A");
/* Append 0x0D to emulate ENTER key */
cmdBuffer[10] = 0x0D;
/* Write the command to the GPIO module. Total 11 bytes including 0x0D */
printf("Info: Writing command <gpio set A> to the GPIO module\n");
if(!WriteFile(hComPort, cmdBuffer, 11, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
printf("Info: <gpio set A> Command sent successfuly\n");
/* EXAMPLE 3 - READ ADC 1 */
/**************************************************************************
Write "adc read 1" comamnd to the device and read back response. It is
important to note that the device echoes every single character sent to
it and so when you read the response, the data that is read will
include the command itself, a carriage return, the response which you
are interested in, a '>' character and another carriage return. You
will need to extract the response from this bunch of data.
/*************************************************************************/
/* Write a Carriage Return to make sure that any partial commands or junk
data left in the command buffer is cleared. This step is optional.
*/
cmdBuffer[0] = 0x0D;
if(!WriteFile(hComPort, cmdBuffer, 1, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/* Flush the Serial port's RX buffer. This is a very important step*/
Sleep(10);
PurgeComm(hComPort, PURGE_RXCLEAR|PURGE_RXABORT);
/* Copy the command to the command buffer */
strcpy(cmdBuffer, "adc read 1");
/* Append 0x0D to emulate ENTER key */
cmdBuffer[10] = 0x0D;
/* Write the command to the GPIO module. Total 11 bytes including 0x0D */
printf("Info: Writing command <adc read 1> to the GPIO module\n");
if(!WriteFile(hComPort, cmdBuffer, 11, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
printf("Info: <adc read 1> Command sent successfuly\n");
/*Read back the response*/
if(!ReadFile(hComPort, responseBuffer, 16, &numBytesRead, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/* Add a null character at the end of the response so we can use the buffer
with string manipulation functions.
*/
responseBuffer[numBytesRead] = '\0';
printf("Info: ADC value read from the device = %.*s", 4, responseBuffer + 12);
/* Close the comm port handle */
CloseHandle(hComPort);
return 0;
}
我的 Ubuntu 代码
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#define MAXBUFFER 32
int main(int argc, char* argv[])
{
struct termios serialSettings;
char *deviceName = "/dev/ttyACM0";
char bufferRecv[MAXBUFFER], bufferSend[MAXBUFFER];
int readInt, sendInt;
int fd = open(deviceName, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) {
printf("\n %s\n", deviceName);
perror("unable to open port");
}
else {
printf("port is opened!\n");
bufferSend[0] = 0x0D; /* clear buffer */
strcpy(bufferSend, "gpio set 0"); /* */
sendInt = write(fd, bufferSend, strlen(bufferSend));
if(sendInt <= 0){
printf("Unable to write to the port\n");
return -1;
}
printf("<gpio set 0> : Command sent successfuly\n");
strcpy(bufferSend, "gpio read 0"); /* */
sendInt = write(fd, bufferSend, strlen(bufferSend));
if(sendInt <= 0){
printf("Unable to write to the port\n");
return -1;
}
printf("<gpio read 0> : Command sent successfuly\n");
readInt = read(fd, bufferRecv, sizeof(bufferRecv));
if(readInt < 0){
printf("Unable to read to the port\n");
return -1;
}
bufferRecv[strlen(bufferRecv)] = '\0';
printf("read=%c-\n", bufferRecv[0]);
}
close(fd);
return 0;
}
输出
port is opened!
<gpio set 0> : Command sent successfuly
<gpio read 0> : Command sent successfuly
Unable to read to the port
最佳答案
“成功”的 write() 是误报。数据已输出,但设备未正确接收。
您的程序在open()之后和任何write()或之前没有使用termios正确配置串行端口读取()。您应该将端口配置为规范模式而不是原始模式。您还需要配置波特率、字符长度、奇偶校验和停止位数。
使用Serial Programming Guide for POSIX Operating Systems 。
可行的(?)配置代码可能如下所示(对于 115200 波特率、8N1 和规范输入,即读取在 NL 字符处终止):
#include <termios.h>
struct termios serialSettings;
speed_t spd;
int fd;
int rc;
fd = open(deviceName, O_RDWR | O_NOCTTY);
if (fd == -1) {
printf("\n %s\n", deviceName);
perror("unable to open port");
return -1;
}
rc = tcgetattr(fd, &serialSettings);
if (rc < 0) {
perror("unable to get attributes");
return -2;
}
spd = B115200;
cfsetospeed(&serialSettings, spd);
cfsetispeed(&serialSettings, spd);
serialSettings.c_cflag &= ~CSIZE;
serialSettings.c_cflag |= CS8;
serialSettings.c_cflag &= ~PARENB;
serialSettings.c_cflag &= ~CSTOPB;
serialSettings.c_cflag &= ~CRTSCTS; /* no HW flow control? */
serialSettings.c_cflag |= CLOCAL | CREAD;
serialSettings.c_iflag &= ~(PARMRK | ISTRIP | IXON | IXOFF | INLCR);
serialSettings.c_iflag |= ICRNL;
serialSettings.c_oflag &= ~OPOST;
serialSettings.c_lflag &= ~(ECHO | ECHONL | ISIG | IEXTEN);
serialSettings.c_lflag |= ICANON;
rc = tcsetattr(fd, TCSANOW, &serialSettings);
if (rc < 0) {
perror("unable to set attributes");
return -2;
}
/* serial port is now configured and ready */
...
<小时/>对您的代码的其他注释:
bufferRecv[strlen(bufferRecv)] = '\0';
这是不合逻辑的代码。如果您实际上可以确定 bufferRecv 中内容的字符串长度,那么该文本就已经以 null 结尾,并且不需要进行此分配。
其次,更糟糕的是,read() 不会以空字节终止接收数据,因此 strlen() 可能会扫描到缓冲区末尾。
read() 确实返回缓冲区中存储的字节数,并且该值可用于定位应写入空字节的位置。
可行的代码如下所示(请注意请求的读取长度的减少):
readInt = read(fd, bufferRecv, sizeof(bufferRecv) - 1);
if (readInt < 0){
perror("Unable to read from the port\n");
return -3;
}
bufferRecv[readInt] = '\0';
printf("ADC value read = %s\n", bufferRecv);
<小时/>发送到 USB 设备的字符串应以回车符开头并以回车符结尾,就像 Win 示例一样:
strcpy(bufferSend, "\rgpio set 0\r");
...
strcpy(bufferSend, "\rgpio read 0\r");
关于c - 向指定端口写入/读取命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619517/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!