LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-12-2005, 07:28 AM   #1
tklima
LQ Newbie
 
Registered: Jan 2005
Posts: 15

Rep: Reputation: 0
Serial port transfer


Hi all,
i'm writting a programm to transfer data via serial port. I'd like to test it on my computer first, so i connected serial ports and wrote a simple program just to check the connection. I write one character into ttyS0 and want to read it from ttyS1. Writing works OK (numb_send = 1) but there is some problem with reading (numb_rec = -1). I think, port settings are OK.

ttyS0: Line 0, UART: 16550A, Port: 0x03f8, IRQ: 4
Baud_base: 115200, close_delay: 50, divisor: 0, closing_wait: 3000

ttyS1: Line 1, UART: 16550A, Port: 0x02f8, IRQ: 3
Baud_base: 115200, close_delay: 50, divisor: 0, closing_wait: 3000

Shall I set anything else? Thank you for your advise.


#include <stdio.h>
#include <sys/signal.h>
#include <sys/types.h>


int main(void) {

int fd1, fd2;
int numb_send,numb_rec;
char c;
char buf[5];

fd1 = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd1 < 0) {
//perror(devicename);
exit(-1);
}

fd2 = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd2 < 0) {
//perror(devicename);
exit(-1);
}



scanf("%c",&c);
numb_send = write(fd1,&c,1);
numb_rec = read(fd2,buf,sizeof(buf));
printf("sent: %d\n received: %d\n",numb_send,numb_rec);
printf("%s\n",buf);
close(fd1);
close(fd2);
}
 
Old 01-12-2005, 02:13 PM   #2
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
does that require using a null-modem cable?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 09:32 PM
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM
Using an USB port as a standard DB9 Serial Port Lsteele Linux - Newbie 1 10-22-2005 09:48 AM
using serial port pjbii Slackware 3 09-17-2005 02:05 PM
serial port vasanthraghavan Linux - Hardware 2 02-05-2005 11:55 AM


All times are GMT -5. The time now is 12:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration