a C Program to monitor serial port data
来源:互联网 发布:java里poi是什么 编辑:程序博客网 时间:2024/05/22 08:20
第一步 打开串口 ttyUSB0, 这部分程序放在main的前面
char *port_name = "/dev/ttyUSB0";
int fd;
int j;
int nbr_writes;
printf("\n part test of RS485: \n\n");
fd = open(port_name, O_RDWR|O_NOCTTY|O_NDELAY);
if (fd < 0)
{
printf ("error %d opening %s: %s", errno, port_name, strerror (errno));
return 1;
}
printf("open %s returns %d \n", port_name, fd);
第二步 初始化串口的函数 setup_com(fd);
int setup_com(int fd){
struct termios options;
tcgetattr(fd, &options);
/* Set the baud rates to 9600...*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
/* Enable the receiver and set local mode...*/
options.c_cflag |= (CLOCAL | CREAD);
/* Set c_cflag options.*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* Set c_iflag input options */
options.c_iflag &=~(IXON | IXOFF | IXANY);
options.c_iflag &=~(INLCR | IGNCR | ICRNL);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* Set c_oflag output options */
options.c_oflag &= ~OPOST;
/* Set the timeout options */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
tcsetattr(fd, TCSANOW, &options);
return 1;
}
第3步 尝试写入一些数据,看是否成功
nbr_writes = write (fd, "hello!\n", 7); // send 7 character greeting
printf(" \n %d bytes written to ttyUSB0",nbr_writes);
第4步 读取串口数据的函数func_485_transfer(fd)
int func_485_transfer(int fd)
{
byte rcv_buf[1024];
int j;
bzero(rcv_buf,sizeof(rcv_buf));
while(1) {
read_datas_ttyS(fd,rcv_buf,5);
printf("this time we have read: \n\n");
for(j=0;j<rcv_amounts;j++)
printf("0x%x ",rcv_buf[j]);
printf("\n\n");
}
return 0;
第5步 实现具体的read函数
int read_datas_ttyS(int fd, byte *rcv_buf,int rcv_wait)
{
int retval;
fd_set rfds;
struct timeval tv;
int ret,pos;
int j;
tv.tv_sec = rcv_wait;
tv.tv_usec = 0;
pos = 0; // point to rceeive buf
while (1)
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds); //关联fd与rfds
retval = select(fd+1 , &rfds, NULL, NULL, &tv);
switch (retval) {
case -1:
perror("select()");
return 0;
break;
case 0:
printf("timed out from select()\n");
return 0; //这里必须要return,否则屏幕会不断输出 timed out from select()
break;
default:
if (FD_ISSET(fd, &rfds)) {
ret = read(fd, rcv_buf+pos, 1024);
pos += ret;
rcv_amounts=pos;
}
break;
}
}
return 1;
} // end read_datas_ttyS
最后 程序用到的库 和 一些全局变量
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h> // open() close()
#include <unistd.h> // read() write()
#include <termios.h> // set baud rate
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
typedef unsigned int byte;
int rcv_amounts;
//-----------------------
- a C Program to monitor serial port data
- A Linux serial port test program
- A Linux serial port test program
- A Simple RS-232 Serial Port Communication Program
- Configuring a Serial Port
- C#serial Port串口处理~
- cell phone data cable usb serial port
- CommMonitor8.0串口过滤工具(serial port monitor)
- How To Set Up A Serial Port Between Two Virtual Machines In VirtualBox
- How to use Serial Port in Windows
- Unable to open serial port /dev/ttyUSB0
- Unable to open serial port /dev/ttyUSB0
- A communication class for serial port
- A communication class for serial port
- communicating-with-serial-port-in-C-Sharp
- Simple linux serial port programming in C.
- Windows Serial Port Programming in C.
- Serial Port
- list,set,map的区别
- mysql中replace函数的用法
- 公司技术管理角度看游戏程序员发展
- 接口和抽象类的区别 --相信你看完不会再混淆了
- 共用email将增进情侣感情
- a C Program to monitor serial port data
- JavaScript基础回顾
- php array_keys() 函数
- ttt
- single-transaction 和master-data的用法
- UVa 10790 多少个交点?
- ios7中UIViewControllerBasedStatusBarAppearance作用
- UVALive - 3516 Exploring Pyramids
- 上传文件