串口监控器实现分段故障

时间:2017-10-31 02:50:55

标签: c serial-port

我已经制作了一个代码来从Arduino UNO读取序列打印,并希望用它来移动Linux Ubuntu 17.02中的光标。只要运行1次迭代,代码就可以正常运行。

我的Arduino将打印格式代码     [0-1],xcord,ycord ** 填充星形,使弦长12 xcord和ycord取0到1023之间的值 这些值来自一个操纵杆我希望我可以更具体地说明所述操纵杆的名称或类型,但它不会写在任何地方 此外,我怀疑这个问题与我的Arduino方面有什么关系,而是在我的方面

任何帮助将不胜感激

#define _BSD_SOURC
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <string.h>
#include<unistd.h>

struct cord
{
    int sw;
    int x_axis;
    int y_axis;
 }; 
void mouseMove(struct cord s1)
{
    Display *displayMain = XOpenDisplay(NULL);
    if(displayMain == NULL)
    {
        fprintf(stderr, "Errore nell'apertura del Display !!!\n");
        exit(EXIT_FAILURE);
    }
    //XWarpPointer(displayMain, None, None, 0, 0, 0, 0, s1.x_axis, s1.y_axis);

    XCloseDisplay(displayMain);
}
struct cord decode(char *buffer)
{
    struct cord s1;
   s1.sw=buffer[0]-'0';
   int i=2;
   s1.x_axis=0;
   s1.y_axis=0;
   while(buffer[i]!=',')
   {
       s1.x_axis=s1.x_axis*10+(buffer[i]-'0');
       i++; 
    }

   i++;
   while(buffer[i]!='*'||buffer[i]=='\0')
   {
       s1.y_axis=s1.y_axis*10+(buffer[i]-'0');
       i++;
   }

   s1.x_axis=-s1.x_axis+497;
   s1.y_axis=s1.y_axis-497;
   //printf("%d %d %d\n",s1.sw,s1.x_axis/50,s1.y_axis/50);
   return s1;
 }

char* arread(int fd)
 {
  ssize_t n;
  char* buf=(char *)malloc(128*sizeof(char));
  n = read(fd, buf, 128); 
  buf[n]='\0';
  //printf("%zd bytes got read...\n", n);
  //printf("\n%s\n", buf);
  return buf;
 }
int main()
{
 int fd;
 char *portname = "/dev/ttyACM1";
 if((fd = open(portname, O_RDWR | O_NOCTTY))==-1)
 {
  close(fd);
  printf("error in opening Port");
 }
 else
 {
  struct termios toptions;
  if(tcgetattr(fd, &toptions)==0)
 {
   if(cfsetispeed(&toptions, B9600)==0)
    {
     if(cfsetospeed(&toptions, B9600)==0)
      {
       toptions.c_cflag &= (unsigned int)~PARENB;
       toptions.c_cflag &= (unsigned int)~CSTOPB;
       toptions.c_cflag &= (unsigned int)~CSIZE;
       toptions.c_cflag |= (unsigned int)CS8;
       toptions.c_cflag &= (unsigned int)~CRTSCTS;
       toptions.c_cflag |= (unsigned int)CREAD | (unsigned int)CLOCAL;
       toptions.c_iflag &= (unsigned int)~(IXON | IXOFF | IXANY);
       toptions.c_lflag &= (unsigned int)~(ICANON | ECHO | ECHOE | ISIG);
       toptions.c_oflag &= (unsigned int)~OPOST;
       toptions.c_cc[VMIN] = 12;
       toptions.c_cc[VTIME] = 0;
       if(tcsetattr(fd, TCSANOW, &toptions)==0)
        { 
         //int i=0;
         //while(i<5)
         //{
            mouseMove(decode(arread(fd)));
            //i++;
          //}
         }
       else 
         printf("error 4"); 
       }
   else
       printf("error 3");     
   }
  else printf("error 2");
 }
else 
   printf("error 1");
 }
}

这是更新的代码我不再得到分段错误的错误但是我继续将垃圾值作为输出。此外,我不知道它是如何打印的,因为我阻止了每一个可以做到的printf。

对于缓冲区的值,这是我使用的arduino代码

const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 5; // analog pin connected to X output
const int Y_pin = 4; // analog pin connected to Y output
char buffer[12];
int x,n;
void setup() 
  {
   pinMode(SW_pin, INPUT);
   digitalWrite(SW_pin, HIGH);
   Serial.begin(9600);
  }
void loop() 
  {
   n=sprintf(buffer,"%d,%d,%d",digitalRead(SW_pin),analogRead(X_pin),analogRead(Y_pin));
   for(x=n;x<12;x++)
   buffer[x]='*';
   buffer[12]='\0';
   Serial.println(buffer);
   delay(500);
  }

1 个答案:

答案 0 :(得分:0)

最后代码非常感谢@ user3629249

3) Is it possible to see the url which the shortcut on the home screen is using? When the PWA launches with the X and site information bar it is not possible to grab the url as far as I can tell.

最后导致所有错误的部分是读取功能。我最初不经意地使用了,而没有想到串口显示器现在正在打印什么,所以我得到了垃圾值。然而,在搜索'\ n'后,它就像一个魅力