|
这是linux操作环境下的一个串口程序:
首先是一个.c的文件,再下面是一个它调用的.h文件
#include "gserial.h"include "serialfunc.h"
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <signal.h>
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
GtkWidget *TV_Info_Display;
GtkWidget *TV_input;
GtkTextIter TV_Info_Disp_Iter;
GtkWidget *entry_input;
GtkWidget *button_send;
GtkWidget *entry_read_count;
GtkWidget *entry_write_count;
GtkWidget *button_open_close_tty;
GtkWidget *button_ignore_unprintable;
GtkWidget *combect_baud;
GtkWidget *combo_o_select_tty;
GtkWidget *combo_selselect_bits;
GtkWidget *combo_select_check;
GtkWidget *AppWindow;
static gboolean InputHex = FALSE;
static gboolean OutputHex = FALSE;
static gboolean DispPaused = FALSE;
static gboolean IgnoreUnprintable = TRUE;
static gulong ReadCount = 0;
static gulong WriteCount = 0;
void create_tags (GtkTextBuffer * buffer)
{
g_assert (GTK_IS_TEXT_BUFFER (buffer));
gtk_text_buffer_create_tag (buffer, "bold","weight", PANGO_WEIGHT_BOLD, NULL);
gtk_text_buffer_create_tag (buffer, "red-foreground", "foreground", "red",NULL);
}
void CloseTheApp(GtkWidget *window, gpointer data)
{
if ( tty_get_fp() != NULL )
{
tty_serial_close();
}
gtk_main_quit();
}
void button_send_clicked(GtkButton *button, gpointer data)
{
GtkTextBuffer *buffer;
GtkTextIter start,end;
gchar *inputdata = NULL;
gchar aline[MAX_LINE_LENGTH];
int writenumber;
int i;
int lineindex,bitsindex;
gboolean blankfinded;
guchar tempchar,tempbit;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TV_input));
gtk_text_buffer_get_iter_at_offset(buffer,&start,0);
gtk_text_buffer_get_iter_at_offset(buffer,&end,-1);
inputdata = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
if (InputHex)
{
lineindex = 0;
bitsindex = 0;
for( i = 0;i<strlen(inputdata);i++)
{
if( isxdigit(inputdata) )
{
if( (inputdata>='0') && (inputdata<='9') )
tempbit = inputdata - '0';
else
tempbit = tolower(inputdata) - 'a' + 10;
if(bitsindex == 0)
{
tempchar = tempbit * 16;
bitsindex = 1;
}
else
{
tempchar += tempbit;
bitsindex = 0;
aline[lineindex++] = tempchar;
if(lineindex == (MAX_LINE_LENGTH-1))
{
aline[lineindex] = '\0';
if ( (writenumber = tty_write_data(aline)) != EOF )
{
WriteCount += writenumber;
update_write_count(WriteCount);
}
lineindex = 0;
}
}
}
else
{
bitsindex = 0;
}
}
aline[lineindex] = '\0';
if ( (writenumber = tty_write_data(aline)) != EOF )
{
WriteCount += writenumber;
update_write_count(WriteCount);
}
}
else
{
if ( (writenumber = tty_write_data(inputdata)) != EOF )
{
WriteCount += writenumber;
update_write_count(WriteCount);
}
}
if( inputdata != NULL )
{
g_free(inputdata);
inputdata = NULL;
}
}
int SaveDispData(const gchar* filename)
{
FILE* fp = NULL;
char *data;
GtkTextBuffer *buffer;
GtkTextIter start;
GtkTextIter end;
GtkWidget *message_dlg;
gint return_value;
if ((fp = fopen(filename,"r")) != NULL)
{
fclose(fp);
message_dlg = gtk_message_dialog_new(GTK_WINDOW(AppWindow),GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION,GTK_BUTTONS_YES_NO,"纭?畾瑕佽?鐩栨枃浠?");
return_value = gtk_dialog_run(GTK_DIALOG(message_dlg));
gtk_widget_destroy(message_dlg);
if( return_value != GTK_RESPONSE_YES)
return -2;
}
if ((fp = fopen(filename,"w")) == NULL)
{
g_warning("error open %s\n",filename);
return -1;
}
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TV_Info_Display));
gtk_text_buffer_get_iter_at_offset(buffer,&start,0);
gtk_text_buffer_get_iter_at_offset(buffer,&end,-1);
data = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
fputs(data,fp);
fclose(fp);
free(data);
data = NULL;
return 0;
}
void SaveFileDlg_OK_Clicked(GtkButton *button,gpointer filedlg)
{
G_CONST_RETURN gchar * SelectedFile;
gchar *filename;
SelectedFile = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filedlg));
filename = strdup(rindex(SelectedFile,'/')+1);
if ( strlen(filename) == 0)
{
g_warning("no filename input\n");
gtk_widget_destroy(GTK_WIDGET(filedlg));
return;
}
SaveDispData(SelectedFile);
free(filename);
filename = NULL;
gtk_widget_destroy(GTK_WIDGET(filedlg));
}
void SaveFileDlg_Cancel_Clicked(GtkButton *button,gpointer filedlg)
{
gchar * SelectedFile;
gtk_widget_destroy(GTK_WIDGET(filedlg));
}
int ReadInputData(const gchar* filename)
{
FILE* fp = NULL;
GtkTextBuffer *buffer;
gchar data[MAX_LINE_LENGTH];
gchar dispdata[MAX_LINE_LENGTH * 3];
gchar achar[3];
gsize readcount,totalcount;
gint i,j;
GtkTextIter iter;
if ((fp = fopen(filename,"r")) == NULL)
{
g_warning("error open %s\n",filename);
return -1;
}
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TV_input));
gtk_text_buffer_set_text(buffer,"",0);
totalcount = 0;
while (!feof(fp))
{
readcount = fread(data,sizeofMAX_LINE_LENG(char),TH-1,fp);
totalcount += readcount;
j=0;
if (InputHex)
{
for(i=0;i<readcount;i++)
{
sprintf(achar,"%02X",data);
dispdata[j++] = achar[0];
dispdata[j++] = achar[1];
dispdata[j++] = ' ';
}
}
else
{
for(i=0;i<readcount;i++)
{
// Judge if the char is printable or is the \r\n char
if ( isprint(data) || (data == 0x0A) || (data == 0x0D) )
{
dispdata[j++] = data;
}
}
}
dispdata[j] = '\0';
gtk_text_buffer_get_iter_at_offset(buffer,&iter,-1);
gtk_text_buffer_insert(buffer,&iter,dispdata,-1);
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(TV_input),
&iter,0.0,FALSE,0.0,1.0);
}
fclose(fp);
return 0;
}
void OpenFileDlg_OK_Clicked(GtkButton *button,gpointer filedlg)
{
G_CONST_RETURN gchar * SelectedFile;
SelectedFile = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filedlg));
ReadInputData(SelectedFile);
gtk_widget_destroy(GTK_WIDGET(filedlg));
}
void OpenFileDlg_Cancel_Clicked(GtkButton *button,gpointer filedlg)
{
gchar * SelectedFile;
gtk_widget_destroy(GTK_WIDGET(filedlg));
}
void button_read_clicked(GtkButton *button, gpointer data)
{
GtkWidget *openfiledlg;
openfiledlg = gtk_file_selection_new("閫夋嫨瑕佽?鍙栫殑鏂囦欢");
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(openfiledlg)->ok_button),
"clicked",GTK_SIGNAL_FUNC(OpenFileDlg_OK_Clicked),openfiledlg);
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(openfiledlg)->cancel_button),
"clicked",GTK_SIGNAL_FUNC(OpenFileDlg_Cancel_Clicked),openfiledlg);
gtk_window_set_modal(GTK_WINDOW(openfiledlg),TRUE);
gtk_widget_show(openfiledlg);
}
void button_save_clicked(GtkButton *button, gpointer data)
{
GtkWidget *savefiledlg;
savefiledlg = gtk_file_selection_new("杈撳叆鏂囦欢鍚?);
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(savefiledlg)->ok_button),
"clicked",GTK_SIGNAL_FUNC(SaveFileDlg_OK_Clicked),savefiledlg);
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(savefiledlg)->cancel_button),
"clicked",GTK_SIGNAL_FUNC(SaveFileDlg_Cancel_Clicked),savefiledlg);
gtk_window_set_modal(GTK_WINDOW(savefiledlg),TRUE);
gtk_widget_show(savefiledlg);
}
void update_read_count(gulong n)
{
char tempdata[15];
sprintf(tempdata,"%lu",n);
gtk_entry_set_text(GTK_ENTRY(entry_read_count),tempdata);
}
void update_write_count(gulong n)
{
char tempdata[15];
sprintf(tempdata,"%lu",n);
gtk_entry_set_text(GTK_ENTRY(entry_write_count),tempdata);
}
void button_clear_read_count_clicked(GtkButton *button, gpointer data)
{
ReadCount = 0;
update_read_count(ReadCount);
}
void button_clear_write_count_clicked(GtkButton *button, gpointer data)
{
WriteCount = 0;
update_write_count(WriteCount);
}
void button_clear_input_clicked(GtkButton *button, gpointer data)
{
gtk_text_buffer_set_text(
gtk_text_view_get_buffer(GTK_TEXT_VIEW(TV_input)),"",0);
}
void button_clear_output_clicked(GtkButton *button, gpointer data)
{
gtk_text_buffer_set_text(
gtk_text_view_get_buffer(GTK_TEXT_VIEW(TV_Info_Display)),"",0);
}
void button_open_close_tty_clicked(GtkButton *button, gpointer data)
{
static gboolean opened = FALSE;
static guint input_handle;
if( opened )
{
gtk_input_remove(input_handle);
tty_serial_close();
gtk_button_set_label(button,"鎵撳紑涓插彛");
opened = FALSE;
}
else
{
if( tty_serial_open() == 0 )
{
input_handle = gtk_input_add_full(fileno(tty_get_fp()),
GDK_INPUT_READ,readserial,NULL,NULL,NULL);
gtk_button_set_label(button,"鍏抽棴涓插彛");
opened = TRUE;
}
}
}
void button_input_hex_toggled(GtkToggleButton *button,gpointer data)
{
InputHex = gtk_toggle_button_get_active(button);
}
void button_output_hex_toggled(GtkToggleButton *button,gpointer data)
{
OutputHex = gtk_toggle_button_get_active(button);
gtk_widget_set_sensitive(button_ignore_unprintable,!OutputHex);
}
void button_ignore_unprintable_toggled(GtkToggleButton *button,gpointer data)
{
IgnoreUnprintable = gtk_toggle_button_get_active(button);
}
void button_pause_clicked(GtkButton *button,gpointer data)
{
if( DispPaused )
{
gtk_button_set_label(button,"鏆傚仠");
DispPaused = FALSE;
}
else
{
gtk_button_set_label(button,"鏄剧ず");
DispPaused = TRUE;
}
}
void combo_select_tty_selectchild(GtkList *list,GtkWidget *child,gpointer data)
{
static gint lastindex=-1;
gint index;
g_assert( (index = gtk_list_child_position(list,child)) != -1);
if( index != lastindex )
{
switch(index)
{
case 0:
tty_set_port("/dev/ttyS0");
break;
case 1:
tty_set_port("/dev/ttyS1");
break;
case 2:
tty_set_port("/dev/ttyS2");
break;
case 3:
tty_set_port("/dev/ttyS3");
break;
default:
g_warning("should no come here\n");
break;
}
lastindex = index;
}
}
void combo_select_baud_selectchild(GtkList *list,GtkWidget *child,gpointer data)
{
static gint lastindex=-1;
gint index;
g_assert( (index = gtk_list_child_position(list,child)) != -1);
if( index != lastindex )
{
switch(index)
{
case 0:
tty_set_baud_rate(B300);
break;
case 1:
tty_set_baud_rate(B600);
break;
case 2:
tty_set_baud_rate(B1200);
break;
case 3:
tty_set_baud_rate(B1800);
break;
case 4:
tty_set_baud_rate(B2400);
break;
case 5:
tty_set_baud_rate(B4800);
break;
case 6:
tty_set_baud_rate(B9600);
break;
case 7:
tty_set_baud_rate(B19200);
break;
case 8:
tty_set_baud_rate(B38400);
break;
case 9:
tty_set_baud_rate(B57600);
break;
case 10:
tty_set_baud_rate(B115200);
break;
default:
g_warning("should no come here\n");
break;
}
lastindex = index;
}
}
void combo_select_bits_selectchild(GtkList *list,GtkWidget *child,gpointer data)
{
static gint lastindex=-1;
gint index;
g_assert( (index = gtk_list_child_position(list,child)) != -1);
if( index != lastindex )
{
switch(index)
{
case 0:
tty_set_bit_size(5);
break;
case 1:
tty_set_bit_size(6);
break;
case 2:
tty_set_bit_size(7);
break;
case 3:
tty_set_bit_size(;
break;
default:
g_warning("should no come here\n");
break;
}
lastindex = index;
}
}
void combo_select_check_selectchild(GtkList *list,GtkWidget *child,gpointer data)
{
static gint lastindex=-1;
gint index;
g_assert( (index = gtk_list_child_position(list,child)) != -1);
if( index != lastindex )
{
tty_set_checkmod(index);
lastindex = index;
}
}
GtkWidget * MakeAppWindow()
{
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(window),10);
gtk_window_set_title(GTK_WINDOW(window),"涓插彛璋冭瘯鍣?);
gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(CloseTheApp),NULL);
return window;
}
GtkWidget * MakeButton(GtkWidget *box,const gchar* caption,gpointer callbackfunc)
{
GtkWidget *button;
button = gtk_button_new_with_label(caption);
gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);
if(callbackfunc != NULL)
{
gtk_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(callbackfunc),NULL);
}
return button;
}
GtkWidget * MakeCheckButton(GtkWidget *box,const gchar* caption,gpointer callbackfunc)
{
GtkWidget *button;
button = gtk_check_button_new_with_label(caption);
gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);
if(callbackfunc != NULL)
{
gtk_signal_connect(GTK_OBJECT(button),"toggled",GTK_SIGNAL_FUNC(callbackfunc),NULL);
}
return button;
}
GtkWidget * MakeEntry(GtkWidget *box)
{
GtkWidget *entry;
entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(box),entry,FALSE,FALSE,0);
return entry;
}
GtkWidget * MakeCombo(GtkWidget *box,GList *items,gpointer callbackfunc)
{
GtkWidget *combo;
combo = gtk_combo_new();
gtk_combo_set_popdown_strings(GTK_COMBO (combo), items);
//gtk_combo_set_value_in_list (GTK_COMBO(combo),TRUE,FALSE);
gtk_box_pack_start(GTK_BOX(box),combo,FALSE,FALSE,0);
if(callbackfunc != NULL)
{
gtk_signal_connect(GTK_OBJECT(GTK_COMBO(combo)->list),"select-child",
GTK_SIGNAL_FUNC(callbackfunc),NULL);
}
gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(combo)->entry),FALSE);
gtk_widget_set_size_request(combo,100,20);
return combo;
}
GtkWidget * MakeComboWithLabel(GtkWidget *box,GList *items,
gpointer callbackfunc,const gchar* labeltext)
{
GtkWidget *combo;
GtkWidget *temphbox;
GtkWidget *label;
temphbox = gtk_hbox_new(FALSE,5);
gtk_box_pack_start(GTK_BOX(box),temphbox,FALSE,FALSE,0);
label = gtk_label_new(labeltext);
gtk_box_pack_start(GTK_BOX(temphbox),label,FALSE,FALSE,0);
combo = MakeCombo(temphbox,items,callbackfunc);
return combo;
}
GtkWidget * MakeTextView(GtkWidget *box,guint width,guint height)
{
GtkWidget *view;
GtkTextBuffer *buffer;
GtkWidget *view_scroll_window;
view_scroll_window = gtk_scrolled_window_new(NULL,NULL);
gtk_container_set_border_width (GTK_CONTAINER (view_scroll_window),0);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (view_scroll_window),
GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
gtk_box_pack_start(GTK_BOX(box),view_scroll_window,FALSE,FALSE,0);
view = gtk_text_view_new();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(view));
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view),GTK_WRAP_CHAR);
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view),10);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view),10);
gtk_container_add(GTK_CONTAINER(view_scroll_window),view);
gtk_widget_set_size_request(view,width,height);
return view;
}
gint MakeAppView(GtkWidget *AppWindow)
{
GtkWidget *mainbox;
GtkWidget *main_h1_vbox,*main_h2_vbox;
GtkWidget *main_h2_v1_hbox;
GtkWidget *main_h2_v2_hbox;
GtkWidget *tempbox;
GList *items_tty = NULL;
GList *items_baud = NULL;
GList *items_bits = NULL;
GList *items_check = NULL;
mainbox = gtk_hbox_new(FALSE,10);
main_h1_vbox = gtk_vbox_new(FALSE,10);
main_h2_vbox = gtk_vbox_new(FALSE,10);
// ---------- add tty select combo box ---------------
items_tty = g_list_append (items_tty, "/dev/ttyS0");
items_tty = g_list_append (items_tty, "/dev/ttyS1");
items_tty = g_list_append (items_tty, "/dev/ttyS2");
items_tty = g_list_append (items_tty, "/dev/ttyS3");
combo_select_tty = MakeComboWithLabel(main_h1_vbox,items_tty,
combo_select_tty_selectchild,"绔?彛: ");
gtk_list_select_item(GTK_LIST(GTK_COMBO(combo_select_tty)->list),0);
//------------------------------------------------------
// ---------- add baud rate select combo box ----------
items_baud = g_list_append (items_baud, "300");
items_baud = g_list_append (items_baud, "600");
items_baud = g_list_append (items_baud, "1200");
items_baud = g_list_append (items_baud, "1800");
items_baud = g_list_append (items_baud, "2400");
items_baud = g_list_append (items_baud, "4800");
items_baud = g_list_append (items_baud, "9600");
items_baud = g_list_append (items_baud, "19200");
items_baud = g_list_append (items_baud, "38400");
items_baud = g_list_append (items_baud, "57600");
items_baud = g_list_append (items_baud, "115200");
combo_select_baud = MakeComboWithLabel(main_h1_vbox,items_baud,
combo_select_baud_selectchild,"娉㈢壒鐜?");
gtk_list_select_item(GTK_LIST(GTK_COMBO(combo_select_baud)->list),10);
//------------------------------------------------------
// --------- add bits per byte select combo box ----------
items_bits = g_list_append (items_bits, "5");
items_bits = g_list_append (items_bits, "6");
items_bits = g_list_append (items_bits, "7");
items_bits = g_list_append (items_bits, "8");
combo_select_bits = MakeComboWithLabel(main_h1_vbox,items_bits,
combo_select_bits_selectchild,"姣旂壒: ");
gtk_list_select_item(GTK_LIST(GTK_COMBO(combo_select_bits)->list),3);
//---------------------------------------------------------
//---------- add odd/even check select combo box ----------
items_check = g_list_append (items_check, "None");
items_check = g_list_append (items_check, "Odd");
items_check = g_list_append (items_check, "Even");
combo_select_check = MakeComboWithLabel(main_h1_vbox,items_check,
combo_select_check_selectchild,"鏍¢獙鍜?");
gtk_list_select_item(GTK_LIST(GTK_COMBO(combo_select_check)->list),0);
//--------------------------------------------------------
//---------- add open/close serial button ---------------------------------
button_open_close_tty = MakeButton(main_h1_vbox,"鎵撳紑涓插彛",button_open_close_tty_clicked);
//---------------------------------------------------------------------------
//---------- add input count utility --------------------------------------
tempbox = gtk_hbox_new(FALSE,5);
gtk_box_pack_start(GTK_BOX(main_h1_vbox),tempbox,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(tempbox),gtk_label_new("杈撳叆璁℃暟"),FALSE,FALSE,0);
entry_read_count = MakeEntry(tempbox);
gtk_widget_set_size_request(entry_read_count,60,20);
gtk_entry_set_editable(GTK_ENTRY(entry_read_count),FALSE);
update_read_count(ReadCount);
MakeButton(tempbox,"娓呴櫎璁℃暟",button_clear_read_count_clicked);
//---------- add output count utility --------------------------------------
tempbox = gtk_hbox_new(FALSE,5);
gtk_box_pack_start(GTK_BOX(main_h1_vbox),tempbox,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(tempbox),gtk_label_new("杈撳嚭璁℃暟"),FALSE,FALSE,0);
entry_write_count = MakeEntry(tempbox);
gtk_widget_set_size_request(entry_write_count,60,20);
gtk_entry_set_editable(GTK_ENTRY(entry_write_count),FALSE);
update_write_count(WriteCount);
MakeButton(tempbox,"娓呴櫎璁℃暟",button_clear_write_count_clicked);
//---------- add input view window ---------------------------------------
//entry_input = MakeEntry(main_h2_vbox);
TV_input = MakeTextView(main_h2_vbox,400,100);
//---------- add hex input,send,clear input,read button -------------------
main_h2_v1_hbox = gtk_hbox_new(FALSE,15);
gtk_box_pack_start(GTK_BOX(main_h2_vbox),main_h2_v1_hbox,FALSE,FALSE,0);
MakeCheckButton(main_h2_v1_hbox,"16杩涘埗",button_input_hex_toggled);
button_send = MakeButton(main_h2_v1_hbox,"鍙戦 |
|