使用C对Linux串行设备进行初始化,读写
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用C对Linux串行设备进行初始化,读写,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3198字,纯文字阅读大概需要5分钟。
内容图文
![使用C对Linux串行设备进行初始化,读写](/upload/InfoBanner/zyjiaocheng/966/0f41803224904176b3faacb2082cb2cf.jpg)
我正在开发一个新项目,我希望与连接到我的debian机器的FTDI建立连接.我打算用C编写代码,而不是C.这就是我的问题.我发现的所有示例都不完整,或者是为c编译器而不是GCC编译器.
目标是与我的连接到FTDI的微控制器通信.为了调试我想开始构建一个能够:
>使用ttyUSB1在启动时初始化串行连接
>发送一个字符串
>当电脑收到字符串时显示字符串
>将通信保存到.txt文件
是否有任何示例代码或教程来实现这一目标?
如果我成功了,我会将代码放在这里,以便新观众可以使用它!
编辑:
就像我说的,如果我拥有它,我会发布代码,这对我有用:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define MODEM "/dev/ttyUSB0"
#define BAUDRATE B115200
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
struct termios old_stdio;
int tty_fd, flags;
unsigned char c='D';
tcgetattr(STDOUT_FILENO,&old_stdio);
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
if((tty_fd = open(MODEM , O_RDWR | O_NONBLOCK)) == -1){
printf("Error while opening\n"); // Just if you want user interface error control
return -1;
}
cfsetospeed(&tio,BAUDRATE);
cfsetispeed(&tio,BAUDRATE); // baudrate is declarated above
tcsetattr(tty_fd,TCSANOW,&tio);
while (c!='q'){
if (read(tty_fd,&c,1)>0){
write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
printf("\n");
}
if (read(STDIN_FILENO,&c,1)>0){
write(tty_fd,&c,1);//if new data is available on the console, send it to serial port
printf("\n");
}
}
close(tty_fd);
tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
return EXIT_SUCCESS;
}
大部分代码来自http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux,但我也使用了下面发布的代码中的一些代码.
解决方法:
使用串行端口处理(适用于Linux OS):
– 要打开通信,您需要一个描述符,它将成为串行端口的句柄.
– 设置标志以控制通信的方式.
– 将命令写入此Handle(确保正确格式化输入).
– 得到答案. (确保你要阅读你想要的信息量)
– 关闭手柄.
它看起来像这样:
int fd; // file descriptor
int flags; // communication flags
int rsl_len; // result size
char message; // message to send
char result; // result to read
flags = O_RDWR | O_NOCTTY; // Read and write, and make the job control for portability
if ((fd = open("/dev/ttyUSB1", flags)) == -1 ) {
printf("Error while opening\n"); // Just if you want user interface error control
return -1;
}
// In this point your communication is already estabilished, lets send out something
strcpy(message, "Hello");
if (rsl_len = write(fd, message, strlen(message)) < 0 ) {
printf("Error while sending message\n"); // Again just in case
return -2;
}
if (rsl_len = read(fd, &result, sizeof(result)) < 0 ) {
printf("Error while reading return\n");
return -3;
}
close(fd);
请注意,您必须关心要写什么和要读什么.
在奇偶校验控制,停止位,波特率等情况下,可以使用更多标志.
内容总结
以上是互联网集市为您收集整理的使用C对Linux串行设备进行初始化,读写全部内容,希望文章能够帮你解决使用C对Linux串行设备进行初始化,读写所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。