Hallo every body.
I need help.,
I have a program to read gps and accelerometer data from port. The programm work like this : when I send 'a' pros will receive gps data and send to database, when I send 'b', prog will send accelerometer data and save to database. But when I runn the proggram, gps data success to received and send to databse but accelerometer data canot receive and error is "segmentati on fault.
Could you help me to resolve the prblem..?
My script program is :
I need help.,
I have a program to read gps and accelerometer data from port. The programm work like this : when I send 'a' pros will receive gps data and send to database, when I send 'b', prog will send accelerometer data and save to database. But when I runn the proggram, gps data success to received and send to databse but accelerometer data canot receive and error is "segmentati on fault.
Could you help me to resolve the prblem..?
My script program is :
Code:
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <string.h>
#include <mysql/mysql.h>
#include <stdlib.h> // malloc, free, rand, for exit()
void tokenizer(char *str,char *lat,char *lon)
{
int i = 0;
while(str[i]!='|') {
lat[i] = str[i];
i++;
}
lat[i] = '\0';
i++;
int j = 0;
while(str[i]!='\0') {
lon[j] = str[i];
i++;
j++;
}
lon[j] = '\0';
}
void tokenizer_acm(char *str,char *nilai_x,char *nilai_y,char *nilai_z,char *teg)
{
int i = 0;
while(str[i]!='|'){
nilai_x[i] = str[i];
i++;
}
nilai_x[i] = '\0';
i++;
int j = 0;
while(str[i]!='|'){
nilai_y[j] = str[i];
i++;
j++;
}
nilai_y[j] = '\0';
i++;
j++;
int k = 0;
while(str[i]!='|') {
nilai_z[k] = str[i];
i++;
j++;
k++;
}
nilai_z[k] = '\0';
i++;
j++;
k++;
int l = 0;
while(str[i]!='\0') {
teg[l] = str[i];
i++;
j++;
k++;
l++;
}
teg[l] = '\0';
}
int main(void) {
int fd;
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open port ");
} else {
fcntl(fd, F_SETFL, 0);
}
char a[] = "a";
char b[] = "b";
int n,m,cnt;
char in[30];
//char in2[50];
//char *in;
//char in2[100];
//in = (char*) malloc(i+1);
//if (in == NULL) exit(1);
MYSQL *conn;
const char *localhost = "127.0.0.1";
const char *user = "root";
const char *password = "";
const char *database = "arduino1";
conn = mysql_init(NULL);
// Connect to database
if (!mysql_real_connect(conn, localhost, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
for(cnt=0; cnt<5; cnt++)
{
if (cnt < 1) {
sleep(2);
n = write(fd, a, sizeof(a));
printf("Send : %s \n", a);
//terima data gps dari port
sleep(1);
n = read(fd, in, 100);
if (n < 0)
{
perror("read");
break;
}
//query gps
char c_lat[50],c_lon[50];
tokenizer(in,c_lat,c_lon);
//Isi nilai gps ke database
char query[255];
strcat(query,"INSERT INTO gps (latitude, longitude) VALUES (");
strcat(query,c_lat);
strcat(query,",");
strcat(query,c_lon);
strcat(query,")");
if (mysql_query(conn, query));
{
printf("%s\n", query);
}
} else {
m = write(fd, b, sizeof(b));
printf("Send : %s \n", b);
//terima data accelerometer dari port
sleep(1);
m = read(fd, in, 100);
in[m] = '\0';
char str[255];
//query accelerometer
char c_nilai_x[10],c_nilai_y[10],c_nilai_z[10],c_teg[6];
tokenizer_acm(in,c_nilai_x,c_nilai_y,c_nilai_z,c_teg);
//Isi nilai accelerometer ke database
strcat(str,"INSERT INTO highcharts_php (x_axis, y_axis, z_axis, tegangan) VALUES (");
strcat(str,c_nilai_x);
strcat(str,",");
strcat(str,c_nilai_y);
strcat(str,",");
strcat(str,c_nilai_z);
strcat(str,",");
strcat(str,c_teg);
strcat(str,")");
if (mysql_query(conn, str));
{
printf("%s\n", str);
}
}
// Close database connection
mysql_close(conn);
}
return 0;
}
Comment