Hi Jason (and Michael),
Your example is a real help! I had wandered through the documentation for the functions that Michael had mentioned and saw some that looked interesting to me. The key ones being that all of the I/O to the serial handler is line oriented. So, if it works, this would be a boon for me.
Iâve taken Jasonâs code and modified it as best I could (shows how a little knowledge is a dangerous thing) and came up with the following: (Couldnât figure out how to paste a code section. Sorry.)
#include <gtk/gtk.h>
#include <gio/gio.h>
// Copied from Jason Francis
// With attemmpts to modify for line oriented input
#define PACKET_SIZE 128
static void
on_fifo_input (GInputStream *stream, GAsyncResult *result, gpointer user_data)
{
g_autoptr(GError) error = NULL;
g_autofree char *buf = user_data;
gssize nread;
nread = g_input_stream_read_finish (stream, result, &error);
if (nread <= 0) {
if (nread < 0) {
g_warning ("Read failed: %s", error->message);
}
return;
}
/* TODO parse the packet from buf and dispatch */
//DJP - OK Got that
/* loop and read another packet */
g_data_input_stream_read_line_async(stream,
G_PRIORITY_DEFAULT, NULL,
(GAsyncReadyCallback) on_fifo_input, buf);
/* don't free the buffer */
g_steal_pointer (&buf);
}
static void
fifo_opened (GFile *file, GAsyncResult *result, gpointer unused)
{
g_autoptr(GError) error = NULL;
g_autoptr(GFileInputStream) stream = NULL;
char *buf = NULL;
stream = g_file_read_finish (file, result, &error);
if (!stream) {
g_warning ("Open failed: %s", error->message);
return;
}
void
listen_fifo (const char *path)
{
GFile *file = g_file_new_for_path (path);
g_file_read_async (file, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) fifo_opened, NULL);
}
// End of Jason Francis code
GFileIOStream *OutPut;
GError *FileError;
int main (int argc, char *argv[]) {
// Setup of windows as per Glade here
listen_fifo("/home/pi/Dacq2GUI.fifo");
OutPut = g_file_open_readwrite(g_file_new_build_filename ("/home/pi/GUI2Dacq.fifo", NULL),
NULL,
&FileError);
// Check for errors, etc.
// g_idle_add((GSourceFunc)Background, widgets);
// gtk_window_set_default_size(GTK_WINDOW(window), 800, 400);
// gtk_widget_show(window);
gtk_main();
return 0;
}
// These define what happens hen an active button is pressed
void on_btn_run_clicked(GtkButton *button, app_widgets *app_wdgts) {
int Count;
GError Error;
Count = g_output_stream_write (OutPut, "Command\r", 8, NULL, &Error);
NewState = BTN_RUN;
}
The two things of note:
When I compile, I get a type error on the first parameter to the g_data_input_stream_read_line_async() function. I just donât know how to stitch all the right pieces together.)
Am I handling the sending of data correctly? In this case, in on_btn_run_clicked() I just want to send the string âCommand\râ down to the serial handler.
Thanks for all tour help, Dave