From b9d1bc38d11322d60531b007ed29891182c71d79 Mon Sep 17 00:00:00 2001 From: Xiao Pan Date: Wed, 23 Apr 2025 16:34:35 -0700 Subject: Remote read can from a file --- remote_plot.c | 80 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 25 deletions(-) (limited to 'remote_plot.c') diff --git a/remote_plot.c b/remote_plot.c index 4318ae3..dbf43e0 100644 --- a/remote_plot.c +++ b/remote_plot.c @@ -27,11 +27,16 @@ #include -// very small also works, just transfter delays, I test accelerometer data is -// like 15782 which is 7 bytes consider newline and \0? -#define MAX_XFER_BUF_SIZE 7 +// voltage max format 8 bytes ,xxx.xxx +// temp max format 5 bytes ,xx.x +// timestamp max 8 bytes 86400000 +// 96 voltage, 32 temp +// 96*8+32*5+8=936 bytes +#define MAX_XFER_BUF_SIZE 936 #define LEN 10 +#define VOLTLEN 96 +#define TEMPLEN 32 // Variables for holding error return info from PLplot static PLINT pl_errcode; @@ -42,7 +47,8 @@ typedef struct { sftp_session sftp; PLINT id1; PLFLT t[LEN]; - PLFLT buffer[LEN]; + PLFLT volt[LEN][VOLTLEN]; + PLFLT temp[LEN][TEMPLEN]; GtkWidget *area; }DATA; @@ -61,7 +67,7 @@ static gboolean sftp_read_sync(gpointer user_data) // This is to represent a loop over time // Let's try a random walk process - file = sftp_open(data->sftp, "/sys/bus/iio/devices/iio:device2/in_accel_z_raw", + file = sftp_open(data->sftp, "/tmp/mycan", access_type, 0); if (file == NULL) { fprintf(stderr, "Can't open file for reading: %s\n", @@ -71,9 +77,16 @@ static gboolean sftp_read_sync(gpointer user_data) } for(int i=0;i<(LEN-1);i++) - data->buffer[i]=data->buffer[i+1]; + { + //data->t[i]=data->t[i+1]; + for(int j=0;jvolt[i][j]=data->volt[i+1][j]; + for(int j=0;jtemp[i][j]=data->temp[i+1][j]; + } - for (;;) { + for(;;) + { nbytes = sftp_read(file, buffer, sizeof(buffer)); if (nbytes == 0) { break; // EOF @@ -85,13 +98,14 @@ static gboolean sftp_read_sync(gpointer user_data) return G_SOURCE_REMOVE; } - //printf("buffer %s",buffer); - //printf("buffer int %d\n",atoi(buffer)); - //printf("id1 %d\n",data->id1); - //printf("t %f\n",data->t[9]); - //printf("before sftp_read_sync plstripa\n"); - data->buffer[LEN-1]=atof(buffer); - //printf("after sftp_read_sync plstripa\n"); + //printf("receive: %s",buffer); + //data->buffer[LEN-1]=atof(buffer); + // would be better if check strtok return NULL or not + strtok(buffer,","); + for(int i=0;ivolt[LEN-1][i]=atof(strtok(NULL,",")); + for(int i=0;itemp[LEN-1][i]=atof(strtok(NULL,",")); } rc = sftp_close(file); @@ -257,7 +271,7 @@ static void draw_function (GtkDrawingArea *area, styline[2] = colline[2] = 4; styline[3] = colline[3] = 5; - legline[0] = "in_accel_z_raw"; // pens legend + legline[0] = "voltage"; // pens legend legline[1] = "not_used"; legline[2] = "not_used"; legline[3] = "not_used"; @@ -306,7 +320,7 @@ static void draw_function (GtkDrawingArea *area, { //printf("t%d %f\n",i,data->t[i]); //printf("buffer%d %f\n",i,data->buffer[i]); - plstripa( data->id1, 0, data->t[i], data->buffer[i]); + plstripa( data->id1, 0, data->t[i], data->volt[i][0]); } //printf("before pl free\n"); @@ -318,14 +332,15 @@ static void draw_function (GtkDrawingArea *area, static void print_data (GtkWidget *widget, gpointer user_data) { DATA *data=user_data; - printf("t:"); - for(int i=0;it[i]); - putchar('\n'); - printf("buffer:"); for(int i=0;ibuffer[i]); - putchar('\n'); + { + printf("%g",data->t[i]); + for(int j=0;jvolt[i][j]); + for(int j=0;jtemp[i][j]); + putchar('\n'); + } } static void activate (GtkApplication *app, gpointer user_data) @@ -355,7 +370,7 @@ static void activate (GtkApplication *app, gpointer user_data) gtk_window_present (GTK_WINDOW (window)); - g_timeout_add(50,sftp_read_sync,user_data); + g_timeout_add(500,sftp_read_sync,user_data); //printf("after g_timeout_add\n"); } @@ -372,9 +387,24 @@ int main (int argc, char **argv) for(int i=0;i