|
I tried two serial communication at the same time.
It's work fine.
- //
- // Serial Receive Program
- //
- // cc -o serialR serialR.c -lwiringPi
- //
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <wiringPi.h>
- #include <wiringSerial.h>
- #define DEBUG 0
- int readSerial(int fd, char * buff, int blen, int timeout) {
- unsigned long endTime;
- int ch;
- int pos = 0;
- if(DEBUG)printf("millis=%d\n",millis());
- endTime = millis () + timeout;
- if(DEBUG)printf("endTime=%d\n",endTime);
- buff[pos] = 0;
- while (1) {
- if (millis () > endTime) return -1;
- if (serialDataAvail (fd)) {
- ch = serialGetchar (fd);
- if(DEBUG)printf (" -> %02x\n", ch);
- if (ch == 0x0d) {
- } else if (ch == 0x0a) {
- return pos;
- } else {
- if (pos < blen) buff[pos++] = ch;
- if (pos < blen) buff[pos] = 0;
- }
- } // end if
- } // end while
- }
- int main (int argc, char **argv) {
- int fd1;
- int fd2;
- int len;
- char buff[64];
- char device1[32];
- char device2[32];
- memset(device1,0,sizeof(device1));
- memset(device2,0,sizeof(device2));
- strcpy(device1,"/dev/ttyAMA0");
- if (argc >= 2) strcpy(device1,argv[1]);
- if (argc >= 3) strcpy(device2,argv[2]);
- printf("device1=[%s] device2=[%s]\n",device1,device2);
- if ((fd1 = serialOpen (device1, 115200)) < 0) {
- printf ("Unable to open serial device1: %s\n", strerror (errno)) ;
- return 1 ;
- }
- if (strlen(device2) > 0) {
- if ((fd2 = serialOpen (device2, 115200)) < 0) {
- printf ("Unable to open serial device2: %s\n", strerror (errno)) ;
- return 1 ;
- }
- }
- if (wiringPiSetup () == -1) {
- printf ("Unable to start wiringPi: %s\n", strerror (errno)) ;
- return 1 ;
- }
- printf("wiringPiSetup\n");
- while(1) {
- // Receive data
- len=readSerial(fd1, buff, sizeof(buff),100);
- if (len != -1) {
- printf("readSerial(device1) data=[%s]\n",buff);
- // Send data
- serialPrintf (fd1,"You are Welcome!! This is %s\n",device1);
- }
- len=readSerial(fd2, buff, sizeof(buff),100);
- if (len != -1) {
- printf("readSerial(device2) data=[%s]\n",buff);
- // Send data
- serialPrintf (fd2,"You are Welcome!! This is %s\n",device2);
- }
- }
- }
Copy code
|
This thread contains more resources
You need to Log in to download or view,No account?
Register
x
|