Creating serial port application

This is a discussion on Creating serial port application within the C Programming forums, part of the General Programming Boards category; I am creating a simple application in which i will open a serial port and configure it and make two ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Noida
    Posts
    11

    Lightbulb [b]Creating serial port application[/b]

    I am creating a simple application in which i will open a serial port and configure it and make two threads one is reader thread and second is writer thread. writer thread will write on serial port and reader thread will read this data.
    I have done with opening and configuring the serial port now how a thread can write and read data from serial port; please help me out on this issue.
    Last edited by tejendra; 03-09-2011 at 03:03 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On what operating system?
    On what hardware?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Location
    Noida
    Posts
    11
    the operating sytem is linux(ubuntu). kernel version 2.6.38-rc4.i am reading and writing on tty0 port.
    i think hardware shouldn't matter.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320

  5. #5
    Registered User
    Join Date
    Mar 2011
    Location
    Noida
    Posts
    11
    Quote Originally Posted by Mr.777 View Post
    i have done it already...now all i have to do is create two threads one is reader thread and another writer thread, reader will read data on serial port written by the writer; actually i am not getting the concept how a thread can write data.please give some example.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Try using
    Code:
    Log(char* s); // prints out a log message 
    e.g.
    Log("Thread writing...");
    Output:Thread writing...

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Erm, forget all about threads for the moment, and see how you get on with reading/writing the serial port using normal sequential code.

    Threads are just a distraction until you can manage this simpler step.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    First, you need to inherit your class from QThread class.
    Then declare a pointer of type QTcpSocket...
    And then use like..
    Code:
    Suppose,
    class abc :public QThread{
    private:
    QTcpSocket * p;
    public:
    void write_Thread(QString writing_Data);
    };
    void abc::write_Thread(QString writing_Data){
    p->write(writing_Data.toAscii());
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Erm, forget all about threads for the moment, and see how you get on with reading/writing the serial port using normal sequential code.

    Threads are just a distraction until you can manage this simpler step.
    I'm waiting till he discovers the output on a serial port is not connected to the inputs... You can't write "hello" then read it back... It won't be there.

    He needs a "loopback" plug if he intends to send and recieve through the port on the same machine.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Location
    Noida
    Posts
    11
    My code look like this after opening and configuring the baud rates of the ports
    Code:
    #include<stdio.h>  // Standard Input/output definations 
    #include<unistd.h> // UNIX standard function definations 
    #include<fcntl.h>  // File control definations 
    #include<termios.h>// POSIX terminal control definations 
    #include<pthread.h>
    int open_port(void)
    {
     int fildes;//File descriptor
     fildes=open("/dev/tty0",O_RDWR | O_NOCTTY);
     if(fildes==-1)
     	printf("\nUnable to open port!\n");
     else
       {
     	fcntl(fildes,F_SETFL,0);//Manipulate file descriptor,F_SETFL will return the value of flag	
     	printf("\n**PORT IS OPEN**\n\n");
       }
    }
    int config_port(int fildes)
    {
     struct termios termios_p;
     cfsetispeed(&termios_p,B38400);//Setting output baud rate
     cfsetospeed(&termios_p,B38400);//Setting input baud rate
     tcsetattr(fildes,TCSANOW,&termios_p);
     return (fildes);
     
    }
    
    int main()
    {
    int fildes=open_port();
    config_port(fildes);
    
    }
    Actually i have to create this application with threads only.Without threads i know how to read and write directly on a serial port but after creating thread that thread should write i want the concept or code for this in which a thread read the data written by another thread.
    Last edited by Salem; 03-09-2011 at 01:23 AM. Reason: Use code tags for CODE!!

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int open_port(void)
    {
    int fildes;//File descriptor
    fildes=open("/dev/tty0",O_RDWR | O_NOCTTY);
    if(fildes==-1)
    printf("\nUnable to open port!\n");
    else
    {
    fcntl(fildes,F_SETFL,0);//Manipulate file descriptor,F_SETFL will return the value of flag 
    printf("\n**PORT IS OPEN**\n\n");
    }
    }
    Don't you really think, you should return some value that should be used by fildes in main()?

  12. #12
    Registered User
    Join Date
    Mar 2011
    Location
    Noida
    Posts
    11
    Don't you really think, you should return some value that should be used by fildes in main()?
    ya i will return it but this code is running,i will return the value once it is completed after writing code for creating threads and reading and writing data from serial port. Now please help me out i am in the middle of this application.

  13. #13
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Didn't you read my above posts in which i told you to write with threads????
    Try that and tell....

  14. #14
    Registered User
    Join Date
    Mar 2011
    Location
    Noida
    Posts
    11
    Quote Originally Posted by Mr.777 View Post
    Try using
    Code:
    Log(char* s); // prints out a log message 
    e.g.
    Log("Thread writing...");
    Output:Thread writing...
    where have you created the thread and how will it read the tty0 port??

    I think the more suitable approach will be creating a thread with posix library as pthread_create and in the start_routine we can give the read or write function which will read or write the serial port.Can we do it?

  15. #15
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1
    2
    Code:
    BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
    {
       OVERLAPPED osWrite = {0};
       DWORD dwWritten;
       DWORD dwRes;
       BOOL fRes;
    
       // Create this <strong class="highlight">write</strong> operation's OVERLAPPED structure's hEvent.
       osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
       if (osWrite.hEvent == NULL)
          // error creating overlapped event handle
          return FALSE;
    
       // Issue <strong class="highlight">write</strong>.
       if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
          if (GetLastError() != ERROR_IO_PENDING) { 
             // WriteFile failed, but isn't delayed. Report error and abort.
             fRes = FALSE;
          }
          else
             // <strong class="highlight">Write</strong> is pending.
             dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
             switch(dwRes)
             {
                // OVERLAPPED structure's event has been signaled. 
                case WAIT_OBJECT_0:
                     if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE))
                           fRes = FALSE;
                     else
                      // <strong class="highlight">Write</strong> operation completed successfully.
                      fRes = TRUE;
                     break;
                
                default:
                     // An error has occurred <strong class="highlight"><vb_highlight>in</strong></vb_highlight> WaitForSingleObject.
                     // This usually indicates a problem with the
                    // OVERLAPPED structure's event handle.
                     fRes = FALSE;
                     break;
             }
          }
       }
       else
          // WriteFile completed immediately.
          fRes = TRUE;
    
       CloseHandle(osWrite.hEvent);
       return fRes;
    }
    The code above uses the WaitForSingleObject function with a time-out value of INFINITE, this causes the WaitForSingleObject function to wait for long until the operation is completed and this can make the thread appear to be "hung" when the write operation is simply taking a long time to complete or flow control has blocked the transmission.

    Read the given links (1,2)
    Last edited by Salem; 03-09-2011 at 03:37 AM. Reason: Removed double-posted code without newlines

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplex communication thro serial port
    By Priyachu in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-30-2009, 04:24 AM
  2. Serial Port Questions
    By valaris in forum Tech Board
    Replies: 2
    Last Post: 05-22-2009, 08:26 AM
  3. sending data over UDP from serial port
    By forumguy in forum Linux Programming
    Replies: 0
    Last Post: 04-25-2009, 02:10 PM
  4. how to write multiports serial application
    By pansuriya.amit in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-25-2007, 07:29 AM
  5. serial port communication from two Application
    By lsme in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-12-2005, 10:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21