亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 7361 | 回復: 6
打印 上一主題 下一主題

弱問一個:串口波特率咋設置 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-09-22 13:50 |只看該作者 |倒序瀏覽
ioctl ?
details please.

論壇徽章:
0
2 [報告]
發(fā)表于 2008-09-22 21:07 |只看該作者

論壇徽章:
0
3 [報告]
發(fā)表于 2008-09-22 21:18 |只看該作者
其實串口編程的實質(zhì)就是多串口屬性的設置。
而屬性也就下面這么幾個:
c_cflag Control options
c_lflag Line options
c_iflag Input options
c_oflag Output options
c_cc Control characters
c_ispeed Input baud (new interface)
c_ospeed Output baud (new interface)
關鍵是理解有那些屬性參數(shù)可以設置以及是什么意思。

繼續(xù)找資料。發(fā)現(xiàn)下面的經(jīng)典文章,可以說基本上所有的串口編程的文章都或多或少的參考了這篇文章,《Serial Programming Guide for POSIX Operating Systems》是一定要看的,我讀的是5th Edition, 3rd Revision - Updated March 11, 2003,下載地址:http://www.easysw.com/~mike/serial/index.html

當把這篇文章看完之后,基本可以解決串口的設置問題了。不過這是一個英文的版本,本人打算在暑假里把他翻譯為中文版本。

關于具體的例子:
http://www.comptechdoc.org/os/li ... inux_pgcserial.html不錯,很詳細,不過比較復雜。
中文的《Linux Serial HOWTO 中譯版》上面就有不少,也很值得參考。

下面的是我的程序,一個串口讀取,往mysql數(shù)據(jù)庫寫數(shù)據(jù)的程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "mysql.h"



#define BAUDRATE B9600
#define DEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1

int insertdb(int d1, int d2, int d3, int d4) {
MYSQL connect;
int res, no1, no2, sd1, sd2;
char *query = "INSERT INTO mydata  ( stime, sno1, sno2, sdata1, sdata2 ) VALUES ( '%s', %d, %d, %x, %x)";
char *sql, *st1;
struct tm *ptr;
time_t lt;

no1 = d1;
no2 = d2;
sd1 = d3;
sd2 = d4;
lt = time(NULL);
ptr = localtime(&lt);
st1 = (char *)asctime(ptr);
st1[strlen(st1) -1 ] = '\0';
sql = (char *)malloc(255*sizeof(char));
sprintf(sql, query, st1, no1, no2, sd1, sd2);

/* debug here
printf("%c : ", st1[strlen(st1)]);
printf("%s : %d :",sql, strlen(st1));
return EXIT_SUCCESS;
*/

mysql_init(&connect);

if(mysql_real_connect(&connect, "localhost", "root", "root", "mytest", 0, NULL, 0)) {
  printf("connect success!\n");

  res = mysql_query(&connect, sql);

  if(!res) {
   printf("insert success!\n");
  } else {
   fprintf(stderr, "insert error %d: %s\n", mysql_errno(&connect), mysql_error(&connect));
   return EXIT_FAILURE;
  }

  mysql_close(&connect);
} else {
  fprintf(stderr, "connect fail!\n");
  return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

int main(void) {
int fd, res_w, res_r, i, j, k;
struct termios oldtio,newtio;
char inbuf[255];
char cbuf[4];
int buf[4];

res_w = 0;
res_r = 0;

fd = open(DEVICE, O_RDWR | O_NOCTTY ); // | O_NDELAY);
if(fd < 0) {
  perror(DEVICE);
  exit(-1);
}

tcgetattr(fd, &oldtio);

bzero(&newtio,sizeof(struct termios));

newtio.c_cflag|= (CLOCAL | CREAD);
newtio.c_cflag|=BAUDRATE;
newtio.c_cflag&=~CSTOPB;
newtio.c_cflag&=~PARENB;
newtio.c_cflag&=~CSIZE;
newtio.c_cflag|=CS8;
newtio.c_cflag&=~CRTSCTS;

newtio.c_lflag=0;

newtio.c_oflag=0;

newtio.c_cc[VMIN]=4;
newtio.c_cc[VTIME]=0;

newtio.c_iflag&=~(IXON|IXOFF|IXANY);

cfsetispeed(&newtio, BAUDRATE);
cfsetospeed(&newtio, BAUDRATE);

tcsetattr(fd, TCSANOW, &newtio);

tcflush(fd, TCIFLUSH);

         cbuf[0] = 0x00;
//  cbuf[1] = 0x00;

  j = 0;

for(k = 0; k < 4; k++) {
switch (j) {
  case 0:
  default:
   cbuf[1] = 0x00;
   j = 2;
   break;
  case 2:
   cbuf[1] = 0x02;
   j = 0;
   break;
}
   
res_w = write(fd, cbuf, 2);

/* debug here
printf("cbuf : %x %x \n", cbuf[0], cbuf[1]);
printf("buf : %x : %x : %x : %x \n", inbuf[0], inbuf[1], inbuf[2], inbuf[3]);
*/
res_r = read(fd, &inbuf, 255);

if(res_r != -1) {
  for(i = 0; i < res_r; i++) {
   buf = (int)inbuf;
   buf = buf & 0xff;
  // printf(" %x ", buf);   
  }
  printf("\n");
  if(insertdb(buf[0], buf[1], buf[2], buf[3]))
   printf("insert into db success!");
}
else {
  perror("read fail");
  exit(-1);
}// if end here
}// for end here

tcsetattr(fd, TCSANOW, &oldtio);

close(fd);
exit(0);
} // main end here

評分

參與人數(shù) 1可用積分 +15 收起 理由
bitmilong + 15 鼓勵回複

查看全部評分

論壇徽章:
0
4 [報告]
發(fā)表于 2008-09-24 08:47 |只看該作者
我就是參考http://www.comptechdoc.org/os/li ... inux_pgcserial.html寫了一個自己的串口終端程序。這個程序不復雜。

論壇徽章:
0
5 [報告]
發(fā)表于 2008-09-24 11:02 |只看該作者
引用于“隨點BBS”

Linux 串口編程(附測試程序)

概述:

串口,UART(通用異步收發(fā)器),廣泛應用于各種場合,本文就Linux下的串口編程,做簡單的闡述!

準備知識:

Linux的設備管理比較有意思(vxworks下也是這樣),對設備的操作就仿佛是對文件的操作一樣,所以

無外乎“打開”,“控制”,“讀”,“寫”,“關閉”等操作,對應的系統(tǒng)調(diào)用就是open,ioctl,read,

write,close。

數(shù)據(jù)結(jié)構分析:

在之前做過的測試和應用的基礎上,感覺串口的“終端控制結(jié)構”的選項設置比較重要,在系統(tǒng)的

頭文件<bits/termios.h>中定義,struct termios,調(diào)用tcgetattr和tcsetattr來獲取和設置串口的終端屬性。


  1. struct termios
  2. 31   {
  3. 32     tcflag_t c_iflag;    /* input mode flags */
  4. 33     tcflag_t c_oflag;    /* output mode flags */
  5. 34     tcflag_t c_cflag;    /* control mode flags */
  6. 35     tcflag_t c_lflag;    /* local mode flags */
  7. 36     cc_t c_line;         /* line discipline */
  8. 37     cc_t c_cc[NCCS];     /* control characters */
  9. 38     speed_t c_ispeed;    /* input speed */
  10. 39     speed_t c_ospeed;    /* output speed */
  11. 40 #define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
  12. 41 #define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
  13. 42   };
復制代碼



  1. c_cflag:
  2.         CLOCAL  本地模式,不改變端口的所有者
  3.         CREAD  表示使能數(shù)據(jù)接收器
  4.         PARENB  表示偶校驗
  5.         PARODD  表示奇校驗
  6.         CSTOPB  使用兩個停止位
  7.         CSIZE  對數(shù)據(jù)的bit使用掩碼
  8.         CS8  數(shù)據(jù)寬度是8bit
  9. c_lflag:
  10.         ICANON  使能規(guī)范輸入,否則使用原始數(shù)據(jù)(本文使用)
  11.         ECHO  回送(echo)輸入數(shù)據(jù)
  12.         ECHOE  回送擦除字符
  13.         ISIG  使能SIGINTR,SIGSUSP, SIGDSUSP和 SIGQUIT 信號
  14. c_iflag:
  15.         IXON  使能輸出軟件控制
  16.         IXOFF  使能輸入軟件控制
  17.         IXANY  允許任何字符再次開啟數(shù)據(jù)流
  18.         INLCR  把字符NL(0A)映射到CR(0D)
  19.         IGNCR  忽略字符CR(0D)
  20.         ICRNL  把CR(0D)映射成字符NR(0A)
  21. c_oflag:
  22.         OPOST  輸出后處理,如果不設置表示原始數(shù)據(jù)(本文使用原始數(shù)據(jù))
  23.         c_cc[VMIN]  最少可讀數(shù)據(jù)
  24.         c_cc[VTIME]  等待數(shù)據(jù)時間(10秒的倍數(shù))

復制代碼


設置例子:


  1.              tcgetattr(fd, &opt);
  2. 60         tcgetattr(fd, &newOpt);
  3. 61
  4. 62         newOpt.c_cflag |= (CLOCAL | CREAD);
  5. 63         newOpt.c_cflag &= ~CBAUD;
  6. 64         newOpt.c_cflag |= (B115200 | CS8);
  7. 65
  8. 66         newOpt.c_lflag &= ~(ICANON | ISIG | NOFLSH);
  9. 67
  10. 68         newOpt.c_lflag &= ~(ECHO);
  11. 69
  12. 70         newOpt.c_lflag &= ~(ECHOE);
  13. 71
  14. 72
  15. 73         newOpt.c_oflag &= ~(OPOST | ONLCR | OCRNL);
  16. 74
  17. 75         newOpt.c_cc[VMIN]=100;
  18. 76         newOpt.c_cc[VTIME]=2;
  19. 77
  20. 78         tcflush(fd,TCIFLUSH);
  21. 79
  22. 80         ret = tcsetattr(fd, TCSANOW, &newOpt);
復制代碼


comTest.rar (1.06 KB, 下載次數(shù): 103)

[ 本帖最后由 myforever 于 2008-9-24 11:03 編輯 ]

評分

參與人數(shù) 1可用積分 +15 收起 理由
bitmilong + 15 鼓勵

查看全部評分

論壇徽章:
0
6 [報告]
發(fā)表于 2008-10-06 11:55 |只看該作者
好感動哦

太詳細了 親親

論壇徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵節(jié)徽章
日期:2015-03-06 15:50:392015亞冠之大阪鋼巴
日期:2015-06-12 16:01:352015年中國系統(tǒng)架構師大會
日期:2015-06-29 16:11:2815-16賽季CBA聯(lián)賽之四川
日期:2018-12-17 14:10:21
7 [報告]
發(fā)表于 2008-10-06 12:02 |只看該作者
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP