我需要你的帮助。我有一个程序可以从端口读取 gps 和加速度计数据。程序是这样工作的:当我发送“a”时,prog 将接收 gps 数据并发送到数据库,当我发送“b”时,prog 将发送加速度计数据并保存到数据库。但是当我运行程序时,gps 数据成功接收并发送到数据库,但加速度计数据无法接收,错误是“分段错误。你能帮我解决这个问题吗?
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;
}
谢谢您的帮助。但我仍然收到同样的错误。我的改变是这样的: 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[50];
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, 20);
if (n < 0)
{
perror("read");
break;
}
//query gps
char c_lat[10],c_lon[9];
tokenizer(in,c_lat,c_lon);
//Isi nilai gps ke database
char query[255] = "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, 30);
in[m] = '\0';
//query accelerometer
char c_nilai_x[8],c_nilai_y[8],c_nilai_z[8],c_teg[4];
tokenizer_acm(in,c_nilai_x,c_nilai_y,c_nilai_z,c_teg);
//Isi nilai accelerometer ke database
char str[255] = "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;
}
我运行程序时的结果是:
发送:a INSERT INTO gps(纬度,经度)值(-6.889760,107.619659)发送:b 分段错误(核心转储)
我需要帮助....谢谢