November 23, 2024, 07:09:58 PM

News : LinuxSolved.com Linux Help Community Forum..


Author Topic: Serial port transfer  (Read 4654 times)

Offline tklima

  • New Member
  • Posts: 1
Serial port transfer
« on: January 12, 2005, 02:10:29 PM »
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);
}