EasyVR串行连接C linux
我正在使用EasyVR设备(语音识别系统),并尝试使用串行连接与我的计算机通信。如果我发送一个EasyVR串行连接C linux,c,linux,serial-port,serial-communication,C,Linux,Serial Port,Serial Communication,我正在使用EasyVR设备(语音识别系统),并尝试使用串行连接与我的计算机通信。如果我发送一个b我应该会收到一个o 我写了一个C程序来实现这一点,我能够发送一个b,但是读取功能不起作用。 我用示波器看到,我正在发送一个好字符,同时也收到一个o。 我还使用gtkterm进行了测试,我的EasyVR运行良好 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include
b
我应该会收到一个o
我写了一个C程序来实现这一点,我能够发送一个b
,但是读取
功能不起作用。
我用示波器看到,我正在发送一个好字符,同时也收到一个o
。
我还使用gtkterm进行了测试,我的EasyVR运行良好
这是我的密码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
int main(void)
{
int port_serie;
struct termios options;
int reception=1;
/*char buffer[10];*/ char buffer ='m';
char data = 'b'; //message que tu veux envoyer
port_serie = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);//lecture et ecriture | pas controlling terminal | ne pas attendre DCD
//cas d'erreur d'ouverture
if(port_serie < 0)
{
perror("Erreur d'ouverture du port serie");
exit(EXIT_FAILURE);
}
else
{
printf("Port serie numero %d bien ouvert. \n", port_serie);
//chargement des données
tcgetattr(port_serie, &options);
//9600 bauds
cfsetospeed(&options, B9600);
cfsetispeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD );//programme propriétaire du port
//structure en 8N1 !!
options.c_cflag &= ~PARENB; //pas de parité
options.c_cflag &= ~CSTOPB; // 1 bit de stop
options.c_cflag &= ~CSIZE; //option a 0
options.c_cflag |= CS8; //8 bits
options.c_cflag &= ~CRTSCTS;// pas de flow de controle
tcsetattr(port_serie, TCSANOW, &options); //enregistrement des valeurs de configuration
printf("Configuration OK strcuture en 8N1 !. \n");
//while(buffer !='o'){
// Envoie de donnees.
printf("Envoie des donnees en cours ... \n");
if (write(port_serie,&data, 1) < 0){perror("ERROR\n");}
printf("Envoie Termine. \n");
// Lecture des donnees.
printf("Lecture des donnees en cours ... \n");
fcntl(port_serie,F_SETFL,1);//mode bloquant pour la fonction read() si aucun caractere dispo, programme attend
reception=read(port_serie,buffer,sizeof(buffer));
printf("message recu : %c\n",buffer);
if (reception == -1){
printf("Erreur lecture port serie\n");
printf("Reception finie.\n" );sleep(1);
//}
}
close(port_serie);//fermeture du port serie
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
国际港口;
结构termios选项;
int接收=1;
/*字符缓冲区[10];*/char buffer='m';
char data='b';//消息que tu veux envoyer
端口系列=打开(“/dev/ttyUSB0”,O|RDWR | O|NOCTTY | O|NDELAY);//讲座和报告| pas控制终端| ne pas Attender DCD
//奥维图尔城堡酒店
如果(端口系列<0)
{
perror(“serie港的出口”);
退出(退出失败);
}
其他的
{
printf(“端口序列号%d bien ouvert.\n”,端口序列号);
//多涅斯费用
tcgetattr(端口系列和选项);
//9600波特
cfsetospeed(和选项,B9600);
cfsetispeed(和选项,B9600);
options.c|cflag |=(CLOCAL | CREAD);//港口规划
//结构en 8N1!!
options.c_cflag&=~PARENB;///pas de parité
options.c_cflag&=~CSTOPB;//1位停止
options.c_cflag&=~CSIZE;//选项a 0
options.c_cflag |=CS8;//8位
options.c\u cflag&=~CRTSCTS;///pas de flow de controle
tcsetattr(端口系列、TCSANOW和选项);//配置注册
printf(“配置正常结构en 8N1.\n”);
//while(缓冲区!='o'){
//唐尼大使馆。
printf(“课程中的唐尼环境…\n”);
if(write(port_serie,&data,1)<0{perror(“ERROR\n”);}
printf(“环境终端。\n”);
//唐尼讲座。
printf(“课程讲座…\n”);
fcntl(port_serie,F_SETFL,1);//模式bloquant pour la fonction read()是一名演员,参加节目
接收=读取(端口系列、缓冲区、大小(缓冲区));
printf(“消息重现:%c\n”,缓冲区);
如果(接收==-1){
printf(“系列端口错误”);
printf(“接收结束”。\n”);睡眠(1);
//}
}
关闭(系列港口);//系列港口费
返回0;
}
代码编辑:01/22 10:50am定义
不工作
两点。您发送1个字符,期望返回1个字符,但不终止将应答视为字符串的缓冲区。接下来,如果您未启用阻塞模式,请记住,在9600波特时,接收应答至少需要2毫秒,因此,在调用read()
时,您可能会等待0个字符。您的read(…,&buffer,…)
语句具有指向第二个参数指针的指针,但它应该只是一个指针,即read(…,buffer,…)
。正如@WeatherVane所指出的,一旦该字符存储在正确的缓冲区中(或者read()
只是返回),用于打印它的代码有缺陷。总体而言,您的程序应该检查所有系统调用的返回值,并且在编译期间,您应该启用所有警告以捕获错误read()之类的问题
语句。感谢您的快速响应。我修改了我的代码,就像在我尝试很多事情之前一样。当我启用阻止模式时,程序将保持在读取模式。您的代码在正确检查系统调用返回代码方面仍然存在严重问题/缺陷。如果只接收到一个字母字符,而没有输入字符,则读取调用将被阻止y线终端,因为串行端口配置为规范模式。