GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

serial port communication(rs232) in gtk

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
jincy.thomas
Familiar Face


Joined: 03 Mar 2010
Posts: 7

PostPosted: Wed Mar 03, 2010 1:13 pm    Post subject: serial port communication(rs232) in gtk Reply with quote

I have a GUI application in gtk.

I need to do the data transfer using rs232. I have the terminal gtkterm.

Am defining sCmd in serial port program. Which should print ABC.
char sCmd[254];
sCmd[0] = 0x41;
sCmd[1] = 0x42;
sCmd[2] = 0x43;
sCmd[3] = 0x00;

In the GUI application i want to receive and send the data.

How i need to define the code in GUI.
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Wed Mar 03, 2010 7:01 pm    Post subject: Reply with quote

Hello and welcome to the GTK+ forums.

Code you shown has nothing to do with GUI whatsoever. Low level data transfer is done exactly the same whether you're creating command line or GUI application.

Tadej
Back to top
jincy.thomas
Familiar Face


Joined: 03 Mar 2010
Posts: 7

PostPosted: Fri Mar 05, 2010 4:32 am    Post subject: Reply with quote

Thanks for the reply.

The exact problem is:

I have a gui. when i click on the button it should transfer data.

For that i have button.
getbutton= gtk_button_new_with_label(" get value");
gtk_widget_set_size_request(getbutton, 80,25);
gtk_fixed_put(GTK_FIXED(frame),getbutton,100, 300);

g_signal_connect(G_OBJECT(getbutton), "clicked",
G_CALLBACK(writeport), NULL);


And in the main:

int main(int argc, char *argv[])
{

gtk_init(&argc, &argv);
displayUI(); //GUI functions creating windows etc.

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
return 1;
}
else
{
fcntl(fd, F_SETFL, 0);
}

printf("baud=%d\n", getbaud(fd));
initport(fd);
printf("baud=%d\n", getbaud(fd));

char sCmd[254];
sCmd[0] = 0x41;
sCmd[1] = 0x42;
sCmd[2] = 0x43;
sCmd[3] = 0x00;
writeport(fd, sCmd) ;
printf("written:\n", sCmd);
usleep(500000);
char sResult[254];
fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
close(fd);
gtk_main();

return EXIT_SUCCESS;
}


And the writeport function is:

int writeport(int fd, char *chars) {
int len = strlen(chars);
chars[len] = 0x0d;
chars[len+1] = 0x00;
int n = write(fd, chars, strlen(chars));
if (n < 0) {
fputs("write failed!\n", stderr);
return 0;
}
return 1;
}


Now the problem is when i run program, the gui gets displayed. When i click on the getbutton it displays the value "ABC" (this is what the write function sends) and comes out of the window with segmentation fault.

What is wrong exactly? (the serial port communication program works fine without gui)

Thanks in advance..
Back to top
errol
GTK+ Geek


Joined: 28 Apr 2008
Posts: 78
Location: UK

PostPosted: Sun Mar 07, 2010 10:17 am    Post subject: Reply with quote

The signal handler for the button click should be in the form of :-

Code: (C)
1
 void user_function(GtkButton *button, gpointer user_data)


Your writeport() function is totally different. What should be done is have a signal handler in the above form which then calls your function writeport().
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP