C 向指定端口写入/读取命令

C 向指定端口写入/读取命令,c,serial-port,usb,linux-device-driver,gpio,C,Serial Port,Usb,Linux Device Driver,Gpio,我用。它使用一些命令从输入/输出通道发送/接收数据。有一个例子解释了numato网站上的命令是如何发送的。Windows上有C语言的应用程序。但我使用fedora,下面是读/写gpio的代码。我看不懂数据 Windows的代码示例 #include "stdafx.h" #include "windows.h" #include "string.h" int _tmain(int argc, _TCHAR* argv[]) { HANDLE hComPort; char cmdBuffer

我用。它使用一些命令从输入/输出通道发送/接收数据。有一个例子解释了numato网站上的命令是如何发送的。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;
} 
#包括“stdafx.h”
#包括“windows.h”
#包括“string.h”
int _tmain(int argc,_TCHAR*argv[]
{
处理hComPort;
字符缓冲区[32];
char-responseBuffer[32];
德沃德写的;
德沃德·纳姆巴特雷德;
/*
查找与GPIO设备关联的端口名并更新
下一行相应。端口名称的格式应为
“\\.\COM”。请注意用于转义斜杠的额外斜杠
他们自己读http://en.wikipedia.org/wiki/Escape_sequences_in_C
更多细节。
*/
wchar\u t PortName[]=L“\\\.\\COM14”;
/*
打开COM端口的句柄。我们需要该句柄来发送命令和
接收结果。
*/
hComPort=CreateFile(端口名,通用读写,0,0,打开现有,0,0);
if(hComPort==无效的句柄值)
{
printf(“错误:无法打开指定的端口\n”);
返回1;
}
/*示例1-通过发送命令操纵GPIO0*/
/************************************************************************** 
发送命令以在GPIO 0处输出逻辑高电平
用于完成此操作的是“gpio集0”。发送
模拟回车的回车字符(ASCII值0x0D)
仅当GPIO模块检测到
回车字符。
**************************************************************************/
/*写一个回车,以确保任何部分命令或垃圾
保留在命令缓冲区中的数据将被清除。此步骤是可选的。
*/
cmdBuffer[0]=0x0D;
if(!WriteFile(hComPort,cmdBuffer,1,&numbyteswrited,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的端口\n”);
返回1;
}
/*将命令复制到命令缓冲区*/
strcpy(cmdBuffer,“gpio集0”);
/*附加0x0D以模拟ENTER键*/
cmdBuffer[10]=0x0D;
/*将命令写入GPIO模块。总共11个字节,包括0x0D*/
printf(“信息:将命令写入GPIO模块\n”);
如果(!WriteFile(hComPort,cmdBuffer,11,&numbyteswrited,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的端口\n”);
返回1;
}
printf(“信息:命令已成功发送\n”);
/*示例2-通过发送命令操纵GPIO10*/
/************************************************************************** 
发送命令以在GPIO 0处输出逻辑高电平
用于完成此操作的是“gpio集0”。发送
模拟回车的回车字符(ASCII值0x0D)
仅当GPIO模块检测到
回车字符。
**************************************************************************/
/*写一个回车,以确保任何部分命令或垃圾
保留在命令缓冲区中的数据将被清除。此步骤是可选的。
*/
cmdBuffer[0]=0x0D;
if(!WriteFile(hComPort,cmdBuffer,1,&numbyteswrited,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的端口\n”);
返回1;
}
/* 
将命令复制到命令缓冲区。GPIO编号10及以上为
在命令中使用以A开头的字母引用。例如
GPIO10将是A,GPIO11将是B,依此类推。请注意,这是
不打算使用十六进制表示法,因此字母表可以
超越F。
*/
strcpy(cmdBuffer,“gpio集A”);
/*附加0x0D以模拟ENTER键*/
cmdBuffer[10]=0x0D;
/*将命令写入GPIO模块。总共11个字节,包括0x0D*/
printf(“信息:将命令写入GPIO模块\n”);
如果(!WriteFile(hComPort,cmdBuffer,11,&numbyteswrited,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的端口\n”);
返回1;
}
printf(“信息:命令已成功发送\n”);
/*示例3-读取ADC 1*/
/************************************************************************** 
将“adc read 1”命令写入设备并读回响应。它是
需要注意的是,设备会回显发送到的每个字符
因此,当您读取响应时,所读取的数据将
包括命令本身、回车符和您需要的响应
对“>”字符和另一个回车符感兴趣。您
将需要从这组数据中提取响应。
/*************************************************************************/
/*写一个回车,以确保任何部分命令或垃圾
保留在命令缓冲区中的数据将被清除。此步骤是可选的。
*/
cmdBuffer[0]=0x0D;
if(!WriteFile(hComPort,cmdBuffer,1,&numbyteswrited,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的端口\n”);
返回1;
}
/*刷新串口的接收缓冲区。这是一个非常重要的步骤*/
睡眠(10);
PurgeComm(hComPort,PURGE_RXCLEAR | PURGE_RXABORT);
/*将命令复制到命令缓冲区*/
strcpy(cmdBuffer,“adc读取1”);
/*附加0x0D以模拟ENTER键*/
cmdBuffer[10]=0x0D;
/*将命令写入GPIO模块。总共11个字节,包括0x0D*/
printf(“信息:将命令写入GPIO模块\n”);
如果(!WriteFile(hComPort,cmdBuffer,11,&numbyteswrited,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的端口\n”);
返回1;
}
printf(“信息:命令已成功发送\n”);
/*读回回复*/
if(!ReadFile(hComPort,responseBuffer,16,&numBytesRead,NULL))
{
关闭手柄(hComPort);
printf(“错误:无法写入指定的
#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
#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';
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);
strcpy(bufferSend, "\rgpio set 0\r");
...
strcpy(bufferSend, "\rgpio read 0\r");