Results 1 to 5 of 5

Thread: serial communication in linux,using c

  1. #1
    Join Date
    Aug 2009
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post

    Unhappy serial communication in linux,using c

    hi guys:
    my serial code works well,AND NOTwant to use QextSerialPort:
    Qt Code:
    1. int main(void)
    2. {
    3. int fd,nread;
    4. struct termios Opt;
    5. char buff[512];
    6. /*以读写方式打开串口*/
    7. fd = open( "/dev/ttySAC2", O_RDWR|O_NONBLOCK);
    8. if(-1 == fd)
    9. {
    10. perror("提示错误!");/* 不能打开串口一*/
    11. return 1;
    12. }
    13. //获取属性
    14. tcgetattr(fd, &Opt);
    15. tcflush(fd, TCIOFLUSH);
    16. //设置为19200Bps
    17. cfsetispeed(&Opt,B115200);
    18. cfsetospeed(&Opt,B115200);
    19. //8位数据位
    20. Opt.c_cflag &= ~CSIZE;
    21. Opt.c_cflag |= CS8;
    22. //无校验
    23. Opt.c_cflag &= ~PARENB; /* Clear parity enable */
    24. Opt.c_iflag &= ~INPCK;
    25. //1停止位
    26. Opt.c_cflag &= ~CSTOPB;
    27. //
    28. Opt.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
    29. Opt.c_cc[VMIN] = 0; /* Update the options and do it NOW */
    30.  
    31. //设置属性
    32. tcsetattr(fd,TCSANOW,&Opt);
    33. tcflush(fd, TCIOFLUSH);
    34.  
    35. //
    36. while(!quit)
    37. {
    38. while((nread = read(fd, buff, 512))>0)
    39. {
    40. buff[nread+1] = '\0';
    41. write(fd,buff,nread);
    42. }
    43. }
    44. printf("quit\n");
    45. close(fd);
    46. exit (0);
    47. }
    To copy to clipboard, switch view to plain text mode 
    now i want to place it into a class ,inherited from QThread.
    however,when i invoke close(fd), it report error.because close() is QT's default function to close the dialog.
    how can i solve this problem?

  2. #2
    Join Date
    Aug 2009
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: serial communication in linux,using c

    i have fixed the error:
    ::close(fd);

  3. #3
    Join Date
    Aug 2009
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question Re: serial communication in linux,using c

    another question:
    this code seems not to run as expected.
    i want read() a char and write() it back.
    but now when i read() a char,the write() method has no effect.
    that's to say:
    it never go to:
    Qt Code:
    1. buff[nread+1] = '\0';
    2. write(fd,buff,nread);
    To copy to clipboard, switch view to plain text mode 
    HOWEVER,my serial terminal can receive the char i press!!!
    what's wrong with this code?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: serial communication in linux,using c

    How is this question related to Qt exactly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2009
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: serial communication in linux,using c

    well , later i will put the read() in a QThread, and use postEvent() to communicate with main dialog. hope you could help
    Last edited by lanmanck; 29th September 2009 at 13:19. Reason: reason

Similar Threads

  1. Replies: 1
    Last Post: 1st July 2009, 00:37
  2. Replies: 1
    Last Post: 1st July 2009, 00:36
  3. serial communication
    By semden in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2009, 09:57
  4. serial communication problem
    By Tavit in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2008, 12:56
  5. C++ Library for serial communication
    By dec0ding in forum General Programming
    Replies: 7
    Last Post: 8th July 2007, 18:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.