Here is ReadByte.
Code:
BOOL ReadByte(BYTE *res)
{
BOOL bReturn = TRUE;
BYTE rx;
DWORD dwBytesTransferred=0;
if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
{
if (dwBytesTransferred == 1)
{
res=rx;
bReturn = TRUE;
}
else bReturn = FALSE;
}
else bReturn = FALSE;
return bReturn;
}
Here is an overview of my setup. A ZigBee module (equipped with an Atmel microcontroller) sends data to my PC, specifically to a GUI I made using Glade/GTK.
My experimentation has led me to the conclusion that although the module is able to send the string (the data), the GUI program is unable to save the string in the variable str.
I use a serial port monitor to see what is sent and received at the COM port.
Code:
void
on_BT_COLLECT_clicked (GtkButton *button,
gpointer user_data)
{
gchar x;
gchar str[100];
gchar str2[100];
//gchar *p_str;
int i,j=0;
GtkWidget *dialog, *label, *okay_button; // These will be used for the dialog
hPort = ConfigureSerialPort("COM5");
if(hPort == NULL)
{
printf("Com port configuration failed\n");
// return -1;
}
// Call your ReadString and WriteString functions here
WriteString("C\n",1);
sleep(50);
ReadByte(&x);
sleep(50);
WriteString("~",1);
sleep(50);
//ReadByte(&x);
//sleep(50);
//sprintf(str,"tsktsk\n");
//sleep(50);
ReadString(str,&i);
sprintf(str2,"%c%c\n",x,x);
//sleep(50);
/*
while (1)
{
ReadByte(x);
//sleep(5);
//if (x=='~')
// break;
str[j]=x;
}
*/
//sleep(2000);
dialog = gtk_dialog_new();
label = gtk_label_new(str2); // Create the widgets of the dialog box
okay_button = gtk_button_new_with_label("OK");
gtk_signal_connect_object(GTK_OBJECT (okay_button), "clicked", gtk_widget_destroy, GTK_OBJECT(dialog));
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
okay_button);
// Add the Okay Button to the dialog
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label); // Add the label
gtk_widget_show_all (dialog); // Show everything the dialog.
//gtk_entry_set_text(GTK_OBJECT("entry1"),str);
ClosePort();
}