12
Return to list Post new posts
Author: bigmac

Multiple UART

[Copy link]

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2016-7-13 06:03:19 | Show all floors

cfsetispeed() sets the input baud rate stored in the termios structure to spe...

Edited by nopnop2002 at 2016-7-13 06:15
Ashtonites replied at 2016-7-7 02:17
I have a related question, so I'm not opening a new thread:
The hardware to which my OPi UART is con ...

Here is Man Page of cfsetospeed function.
http://linux.die.net/man/3/cfsetispeed

cfsetospeed() sets the output baud rate stored in the termios structure pointed to by termios_p to speed, which must be one of these constants:

B0
B50
B75
B110
B134
B150
B200
B300
B600
B1200
B1800
B2400
B4800
B9600
B19200
B38400
B57600
B115200
B230400

The zero baud rate, B0, is used to terminate the connection. If B0 is specified, the modem control lines shall no longer be asserted. Normally, this will disconnect the line. CBAUDEX is a mask for the speeds beyond those defined in POSIX.1 (57600 and above). Thus, B57600 & CBAUDEX is nonzero.
cfgetispeed() returns the input baud rate stored in the termios structure.

cfsetispeed() sets the input baud rate stored in the termios structure to speed, which must be specified as one of the Bnnn constants listed above for cfsetospeed(). If the input baud rate is set to zero, the input baud rate will be equal to the output baud rate.

cfsetspeed() is a 4.4BSD extension. It takes the same arguments as cfsetispeed(), and sets both input and output speed.


I think I can't set it as 250000 baud rate.

3

threads

10

posts

111

credits

Registered member

Rank: 2

credits
111
 Author| Published in 2016-7-13 20:53:55 | Show all floors
By the way you need to ask your hardware the supported baudrate in order to see if above can be set. ( normally 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200)

some USB 2 RS232 may do the trick
http://www.prolific.com.tw/US/ShowProduct.aspx?pcid=41

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2016-7-31 12:30:54 | Show all floors
I tried two serial communication at the same time.
It's work fine.

  1. //
  2. // Serial Receive Program
  3. //
  4. // cc -o serialR serialR.c -lwiringPi
  5. //
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <wiringPi.h>
  10. #include <wiringSerial.h>

  11. #define    DEBUG    0

  12. int readSerial(int fd, char * buff, int blen, int timeout) {
  13.   unsigned long endTime;
  14.   int ch;
  15.   int pos = 0;

  16. if(DEBUG)printf("millis=%d\n",millis());
  17.   endTime = millis () + timeout;
  18. if(DEBUG)printf("endTime=%d\n",endTime);
  19.   buff[pos] = 0;

  20.   while (1) {
  21.     if (millis () > endTime) return -1;
  22.     if (serialDataAvail (fd)) {
  23.       ch = serialGetchar (fd);
  24. if(DEBUG)printf (" -> %02x\n", ch);
  25.       if (ch == 0x0d) {

  26.       } else if (ch == 0x0a) {
  27.         return pos;
  28.       } else {
  29.         if (pos < blen) buff[pos++] = ch;
  30.         if (pos < blen) buff[pos] = 0;
  31.       }
  32.     } // end if
  33.   } // end while
  34. }


  35. int main (int argc, char **argv) {
  36.   int fd1;
  37.   int fd2;
  38.   int len;
  39.   char buff[64];
  40.   char device1[32];
  41.   char device2[32];

  42.   memset(device1,0,sizeof(device1));
  43.   memset(device2,0,sizeof(device2));

  44.   strcpy(device1,"/dev/ttyAMA0");
  45.   if (argc >= 2) strcpy(device1,argv[1]);
  46.   if (argc >= 3) strcpy(device2,argv[2]);
  47.   printf("device1=[%s] device2=[%s]\n",device1,device2);

  48.   if ((fd1 = serialOpen (device1, 115200)) < 0) {
  49.     printf ("Unable to open serial device1: %s\n", strerror (errno)) ;
  50.     return 1 ;
  51.   }

  52.   if (strlen(device2) > 0) {
  53.     if ((fd2 = serialOpen (device2, 115200)) < 0) {
  54.       printf ("Unable to open serial device2: %s\n", strerror (errno)) ;
  55.       return 1 ;
  56.     }

  57.   }

  58.   if (wiringPiSetup () == -1) {
  59.     printf ("Unable to start wiringPi: %s\n", strerror (errno)) ;
  60.     return 1 ;
  61.   }
  62.   printf("wiringPiSetup\n");

  63.   while(1) {
  64. //  Receive data
  65.     len=readSerial(fd1, buff, sizeof(buff),100);
  66.     if (len != -1) {
  67.       printf("readSerial(device1) data=[%s]\n",buff);
  68. //    Send data
  69.       serialPrintf (fd1,"You are Welcome!! This is %s\n",device1);
  70.     }

  71.     len=readSerial(fd2, buff, sizeof(buff),100);
  72.     if (len != -1) {
  73.       printf("readSerial(device2) data=[%s]\n",buff);
  74. //    Send data
  75.       serialPrintf (fd2,"You are Welcome!! This is %s\n",device2);
  76.     }
  77.   }
  78. }
Copy code


This thread contains more resources

You need to Log in to download or view,No account?    Register

x
12
Return to list Post new posts
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list