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
“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.