Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:10:27+00:00 2026-06-14T03:10:27+00:00

I have try to access robot via serial port using gcc (i using mac).

  • 0

I have try to access robot via serial port using gcc (i using mac).

I have made my program to send simple command:

HOME (enter) and there are some feedback from the robot: THIS IS THE
HOMING SEQUENCE COMMAND. THE ROBOT WILL SEEK ITS HOMING SWITCHES ON
EACH AXIS AND WILL EXECUTE THE HOMING FUNCTION AT THAT LOCATION. DO
YOU WANT TO PROCEED (Y/N) ?

and I send

Y(enter)

and the robot suppose move.

To access robot now is using modem/terminal Zterm.
This modem use baudrate 38400, 8N1

I use the same baudrate and 8n1

This is my code, I do not know what is wrong why my code cannot make the robot move

Thank you

Daniel

    #include<stdio.h>   /* Standard input/output definitions */
    #include<stdlib.h>
    #include<string.h>  /* String function definitions */
    #include<unistd.h>  /* UNIX standard function definitions */
    #include<fcntl.h>   /* File control definitions */
    #include<errno.h>   /* Error number definitions */
    #include<termios.h> /* POSIX terminal control definitions */
    //#include<conio.h> 

    /*
     * 'open_port()' - Open serial port 1.
     *
     * Returns the file descriptor on success or -1 on error.
     */

    int buf_size;
    char *buf;
    char *buff;
    int fd; /* File descriptor for the port */

    int open_port(void)
    {

       fd = open("/dev/tty.USA28X1a2P2.2", O_RDWR | O_NOCTTY | O_NDELAY);  //      USA28X1a2P2.for keyspan 
    //fd = open("/dev/ttys000", O_RDWR | O_NOCTTY | O_NDELAY);   
    if (fd == -1) {
     /*
      * Could not open the port.
      */
      perror("cannot open");
   }
     else 
        fcntl(fd, F_SETFL, 0);
        struct termios options;
          /* 
           * Get the current options for the port...
           */
          tcgetattr(fd, &options);

          /*
           * Set the baud rates to 38400...
           */

         cfsetispeed(&options, B38400);
         cfsetospeed(&options, B38400);


         /*
          * Enable the receiver and set local mode...
          */

         options.c_cflag |= (CLOCAL | CREAD);

         /*
          * Set the new options for the port...
          */

         tcsetattr(fd, TCSANOW, &options);

         options.c_cflag &= ~CSIZE; /* Mask the character size bits */

         options.c_cflag &= ~PARENB;
         options.c_cflag &= ~CSTOPB;
         options.c_cflag &= ~CSIZE;
         options.c_cflag |= CS8;

     return (fd);
    }

    int main(int argc, char**argv) {
        buf =malloc(20);
        buff=malloc(20);

    //   strcpy(buf,"HOME");
    //    strcpy(buff,"Y");
        open_port();
        printf("type the command using Capital Letter : \n");
        scanf("%s",buf);
        write(fd, buf,20); // 
        write(fd," \r ",5);
        printf("Command = %s\n", buf);
        read(fd, buff,50);
        printf(" %s \n",buff);
        free(buf);
        free(buff);
        printf("type Y/N : \n");
        scanf("%s",buf);
        write(fd, buf,2);
        write(fd,"\r",2);

    //    free(buf);
    //  free (buff);
    //    printf("type Y/N : \n");
    //    write(fd, buf,20);
    //    printf("You choose %s \n",buff);
    //    free(buf);

        close(fd);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:10:28+00:00Added an answer on June 14, 2026 at 3:10 am

    You are reading too much at a time. You allocate your buffers as 20 bytes, buff=malloc(20); But here you read 50, read(fd, buff,50); That can lead to odd behavior at best. Make sure you allocate as much space as you’ll use. You don’t even need to allocate it, you can use an array.

    char buf[1024];
    char buff[1024];
    

    Then you free your memory before using them again.

     free(buf);
     free(buff);
     printf("type Y/N : \n");
     scanf("%s",buf);
    

    Don’t free buf or buff until after you call close(fd).

    Read up on good C style, too. You’re doing a few things that aren’t bugs, but will make your life harder later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario that when I try to access a hash key using
I try to access session variables stored on a different page. When I have
I have NFS installed for Windows 2008 but when I try to access C:/Windows/System32/mount.exe
I have a file foo.bar.1 on my server and when I try to access
I have an object that has properties, but when I try to access them,
I have My Sharepoint Web Service URL, but when i try to access it
I try unsuccessful to access from my portlet the session attribute that I have
I have try { using (var eventWaitHandle = EventWaitHandle.OpenExisting(name)) { eventWaitHandle.Set(); } Environment.Exit(0); }
I have a centralized Mercurial repository which I want to provide access to via
I have this simple code to perdiodically access a pop3 server and check if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.