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 8882419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:34:25+00:00 2026-06-14T20:34:25+00:00

I want to read data from an Arduino micro controller to my pc running

  • 0

I want to read data from an Arduino micro controller to my pc running Mac OS X via serial port with the C programming language and the GCC compiler.

The format of my data is A xxxx xxxx xxxx xxxx xxxx xxxx B, with A is the beginning of the data, B is the end of the data and between the sensor there are 4 space (” “).
The xxxx data vary vary between 0-1023.

I am trying this code:

#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<string.h> 
#include<unistd.h>

char *buf;
int fd; /* File descriptor for the port */
int i,n;
char *sensor1, *sensor2, *sensor3, *sensor4, *sensor5, *sensor6,*header, *footer;

int open_port(void)
{
    fd = open("/dev/tty.usbmodem1d11", O_RDWR | O_NOCTTY | O_NDELAY);      

if (fd == -1)     {
    perror("cannot open");
}
else 
    fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSANOW, &options);
options.c_cflag &= ~CSIZE; 
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//    options.c_cflag |= (IXON | IXOFF | IXANY); // xon & xoff on
return (fd);
}

int main(int argc, char**argv) {
    buf=malloc(4095);
    open_port();
    free(buf);
    while(1){ 
        read(fd,buf,90);          
         printf("%s\n",buf);  
    }
    close(fd);
}

But the result is not consistent, I mean the length of the data is not the same:

A 1023 1023 1023 1023 1023 B

A

10233 023 1023 1023 B

A 1023 1023 1023 1023 1023 B

A
3 023 1023 1023 B

A 1023 1023 1023 1023 1023 B

A

10233 023 1023 1023 B

A 1023 1023 1023 1023 1023 BA

Any suggestions?

  • 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-14T20:34:26+00:00Added an answer on June 14, 2026 at 8:34 pm

    Examine what you are getting back from each read call:

    int nbytes;
    
    while(1) { 
        nbytes = read(fd,buf,90);
        if( nbytes > 0 ) {
            buf[nbytes] = 0;
            printf( "Read %2d bytes: '%s'\n", nbytes, buf );
        }
    }
    

    You will need to gather the result into a buffer, and parse it according to your device’s format specification. You can’t just assume that each read call will give you a whole record.

    To gather the result, you need to keep track of what you have already read – something like this…

    int nbytes, nparsed;
    int npos = 0;
    
    while(1) { 
        nbytes = read(fd, &buf[npos], 90-npos);
        if( nbytes > 0 )
        {
            npos += nbytes;
    
            // Parse a line.  If successful, move remainder of line to
            // start of buffer and continue...
            nparsed = parse_line(buf, npos);
            if( nparsed > 0 ) {
                memmove( buf, &buf[npos], npos-nparsed );
                npos -= nparsed;
            }
        }
    }
    

    Depends on your application. It might be even simpler than that.

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

Sidebar

Related Questions

I want to read messages sent from an Arduino via the FTDI (serial) interface
I want to read data from a weighbridge terminal that is connected via a
I want to read web service data from a website and display it into
Okay, so I want to have a generic method that can read data from
I want to read data about MMORPG characters from a .txt file and then
I want to read both formatted text and binary data from the same iostream.
I want to simply read some JSON data from a URL, then turn that
I'm making chat application and read messages data from SQLite database. I want to
I have Uitable with data read from a AScii file. I want to select
Read about Server push here . I want to push data to client from

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.