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

  • Home
  • SEARCH
  • 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 7568527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:56:55+00:00 2026-05-30T14:56:55+00:00

I’ m trying to do a little experiment with communication between an arduino UNO

  • 0

I’ m trying to do a little experiment with communication between an arduino UNO and a C program.

First the arduino role:
My arduino is setted to read every 2 seconds an analog value from a photo resistor and then write the value to serial port ( /dev/ttyACM0 on my system ).

Here is arduino’ s sketch:

/* 
  This program will read analog data from a photo resistor and
  will write it to the serial port.
*/

#define LIGHTPIN 0
#define DELAY 2000

void setup()
{
  Serial.begin(9600);
  Serial.flush();
}

void loop()
{
  int lightvalue = 0;

  lightvalue = map(analogRead(LIGHTPIN), 0, 1023, 1, 100);

  Serial.println(lightvalue);

  delay(DELAY);
}

The arduino’ s sketch works fine; it reads data costantly and write it to the serial port. I can view them fine with the serial monitor.

Then comes the C program.
The program should access /dev/ttyACM0 and read the data. It stores the data in a variable and print it. I’ m on an ArchLinux system so to access the port i thought of opening /dev/ttyACM0 as a file. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>

#define DELAY 2

int main(int argc, char *argv[])
{
    FILE *serialport = NULL;
    uint8_t lightval = 0;
    uint8_t lastval = 0;

    if ( argc < 2 ) {
        fprintf(stderr, "Too few arguments.\n");
        exit(EXIT_FAILURE);
    }

    if ( !(serialport = fopen(argv[1], "r")) ) {
        fprintf(stderr, "Couldn' t open serial port for communication.\n");
        exit(EXIT_FAILURE);
    }

    while ( true ) {
        fscanf(serialport, "%u", &lightval);

        if ( lightval != lastval ) {
           printf("%u\n", lightval);
           lastval = lightval;
        }

        while (getc(serialport) != '\n')
           continue;

        sleep(DELAY);
   }

   fclose(serialport);
}

The problem comes here. When i run the test it reads and print the first value
fine but when reading the second value it gives a segmentation fault.

I actually can’ t understand what I do wrong. It could be very stupid but it seems that I can’ t grasp it.

So my question is: What is wrong with my test?

Thanks in advance to who will help me

  • 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-05-30T14:56:57+00:00Added an answer on May 30, 2026 at 2:56 pm

    “segmentation fault” is something that typically happens only when you’re touching a pointer to invalid memory or passing incorrect types or numbers of parameters to a function.

    The first thing to start with her in every case is compiling with -Wall -Werror and seeing if you get any warnings that you should deal with. (In fact, you should just plain always do that.)

    As a second step, run gdb on the resulting core dump and do a backtrace — look at the exact operation you were performing that caused the segmentation fault. It may then be obvious what variable access was causing the problem.

    In this particular instance I think the problem is with your scanf call — you’re using %u in conjunction with an eight bit value, and it is probably trampling all over parts of memory it shouldn’t as a result. However, the general principle of how to debug these things is far more important than the particular problem you have here — I would pretend I hadn’t told you what it might be and see if turning on warnings and using gdb doesn’t point you at the problem. You’ll need to know how to do that again someday.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.