/*
* Program: serial.c
* Author: Paul Dean
* Version: 0.0.3
* Date: 2002-02-19
* Description: To provide underlying serial port function,
* for high level applications.
*
*/
#include <termios.h> /* tcgetattr, tcsetattr */
#include <stdio.h> /* perror, printf, puts, fprintf, fputs */
#include <unistd.h> /* read, write, close */
#include <fcntl.h> /* open */
#include <sys/signal.h>
#include <sys/types.h>
#include <string.h> /* bzero, memcpy */
#include <limits.h> /* CHAR_MAX */
#include "serial.h"
/*
* Decription for TIMEOUT_SEC(buflen,baud);
* baud bits per second, buflen bytes to send.
* buflen*20 (20 means sending an octect-bit data by use of the maxim bits 20)
* eg. 9600bps baudrate, buflen=1024B, then TIMEOUT_SEC = 1024*20/9600+1 = 3
* don't change the two lines below unless you do know what you are doing.
*/
#define TIMEOUT_SEC(buflen,baud) (buflen*20/baud+2)
#define TIMEOUT_USEC 0
#define CH_TO_WAIT 5
#define CH_BITS 11
#define BUFFER_LEN 1024 /* sendfile() */
static INT32 fd; //File descriptor for the port
static struct termios termios_old, termios_new;
static fd_set fs_read, fs_write;
static struct timeval tv_timeout;
static void set_baudrate (INT32);
static INT32 get_baudrate ();
static void set_data_bit (INT32 databit);
static INT32 baudrate2Bxx (INT32 baudrate);
static INT32 Bxx2baudrate (INT32 _baudrate);
static INT32 set_port_attr (
INT32 baudrate, //
INT32 databit,
const char *stopbit,
char parity);
static void set_stopbit (const char *stopbit);
static void set_parity (char parity);
/* Open serial port ComPort at baudrate baud rate. */
INT32 OpenComPort (INT32 ComPort, INT32 baudrate, INT32 databit,
const char *stopbit, char parity)
{
char *pComPort;
INT32 retval;
switch (ComPort) {
case 0:
pComPort = "/dev/ttyO0";
break;
case 1:
pComPort = "/dev/ttyO1";
break;
case 2:
pComPort = "/dev/ttyO2";
break;
case 3:
pComPort = "/dev/ttyO3";
break;
case 4:
pComPort = "/dev/ttyO4";
break;
case 5:
pComPort = "/dev/ttyO5";
break;
default:
pComPort = "/dev/ttyO0";
break;
}
fd = open (pComPort, O_RDWR | O_NOCTTY);
if (-1 == fd) {
fprintf (stderr, "cannot open port %s\n", pComPort);
return (-1);
}
printf("comport fd = %d\n", fd);
tcgetattr (fd, &termios_old); /* save old termios value */
/* 0 on success, -1 on failure */
retval = set_port_attr (baudrate, databit, stopbit, parity);
if (-1 == retval) {
fprintf (stderr, "\nport %s cannot set baudrate at %d\n", pComPort,
baudrate);
}
return (retval);
}
/* close serial port by use of file descriptor fd */
void CloseComPort ()
{
/* flush output data before close and restore old attribute */
tcsetattr (fd, TCSADRAIN, &termios_old);
close (fd);
}
int getPortFd(){
return fd;
}
INT32 ReadComPort (void *data, INT32 datalength)
{
INT32 retval = 0;
FD_ZERO (&fs_read);
FD_SET (fd, &fs_read);
tv_timeout.tv_sec = TIMEOUT_SEC (datalength, get_baudrate ());
tv_timeout.tv_usec = TIMEOUT_USEC;
retval = select (fd + 1, &fs_read, NULL, NULL, &tv_timeout);
if (retval > 0) {
retval = read (fd, data, datalength);
return (retval);
}
else {
if (0 == retval ) {
return (0);
}else{
return (-1);
}
}
}
INT32 ReadComPortA (void *data, INT32 datalength)
{
INT32 retval = 0;
int bytes_read;
int readlen;
/**
* caculate the time of 5 characters and get the maxim
* with 3ms and 5 ch's time
*/
tv_timeout.tv_sec = 0;
tv_timeout.tv_usec = ( (CH_TO_WAIT * CH_BITS) * (1000000/get_baudrate()));
//printf("port read timeout:%dus\n",tv_timeout.tv_usec);
bytes_read = 0;
while(bytes_read<datalength){
tv_timeout.tv_sec = 0;
tv_timeout.tv_usec = ( (CH_TO_WAIT * CH_BITS) * (1000000/get_baudrate()));
FD_ZERO (&fs_read);
FD_SET (fd, &fs_read);
retval = select (fd + 1, &fs_read, NULL, NULL, &tv_timeout);
if ( retval >0 ) {
readlen = read (fd, (data+bytes_read), datalength);
bytes_read += readlen;
} else
return (bytes_read==0?-1:bytes_read);
}
return -1;
}
/*
* Write datalength bytes in buffer given by UINT8 *data,
* return value: bytes written
* Nonblock mode
*/
INT32 WriteComPort (UINT8 * data, INT32 datalength)
{
INT32 retval, len = 0, total_len = 0;
FD_ZERO (&fs_write);
FD_SET (fd, &fs_write);
tv_timeout.tv_sec = TIMEOUT_SEC (datalength, get_baudrate ());
tv_timeout.tv_usec = TIMEOUT_USEC;
for (total_len = 0, len = 0; total_len < datalength;) {
retval = select (fd + 1, NULL, &fs_write, NULL, &tv_timeout);
if (retval) {
len = write (fd, &data[total_len], datalength - total_len);
if (len > 0) {
total_len += len;
}
}
else {
tcflush (fd, TCOFLUSH); /* flush all output data */
break;
}
}
return (total_len);
}
/* get serial port baudrate */
static INT32 get_baudrate ()
{
return (Bxx2baudrate (cfgetospeed (&termios_new)));
}
/* set serial port baudrate by use of file descriptor fd */
static void set_baudrate (INT32 baudrate)
{
termios_new.c_cflag = baudrate2Bxx (baudrate); /* set baudrate */
}
static void set_data_bit (INT32 databit)
{
termios_new.c_cflag &= ~CSIZE;
switch (databit) {
case 8:
termios_new.c_cflag |= CS8;
break;
case 7:
termios_new.c_cflag |= CS7;
break;
case 6:
termios_new.c_cflag |= CS6;
break;
case 5:
termios_new.c_cflag |= CS5;
break;
default:
termios_new.c_cflag |= CS8;
break;
}
}
static void set_stopbit (const char *stopbit)
{
if (0 == strcmp (stopbit, "1")) {
termios_new.c_cflag &= ~CSTOPB; /* 1 stop bit */
}
else if (0 == strcmp (stopbit, "1.5")) {
termios_new.c_cflag &= ~CSTOPB; /* 1.5 stop bits */
}
else if (0 == strcmp (stopbit, "2")) {
termios_new.c_cflag |= CSTOPB; /* 2 stop bits */
}
else {
termios_new.c_cflag &= ~CSTOPB; /* 1 stop bit */
}
}
static void set_parity (char parity)
{
switch (parity) {
case 'N': /* no parity check */
termios_new.c_cflag &= ~PARENB;
break;
case 'E': /* even */
termios_new.c_cflag |= PARENB;
termios_new.c_cflag &= ~PARODD;
break;
case 'O': /* odd */
termios_new.c_cflag |= PARENB;
termios_new.c_cflag |= ~PARODD;
break;
default: /* no parity check */
termios_new.c_cflag &= ~PARENB;
break;
}
}
static INT32 set_port_attr (
INT32 baudrate, // 1200 2400 4800 9600 .. 115200
INT32 databit, // 5, 6, 7, 8
const char *stopbit, // "1", "1.5", "2"
char parity) // N(o), O(dd), E(ven)
{
bzero(&termios_new, sizeof (termios_new));
cfmakeraw (&termios_new);
set_baudrate (baudrate);
termios_new.c_cflag |= CLOCAL | CREAD; /* | CRTSCTS */
set_data_bit (databit);
set_parity (parity);
set_stopbit (stopbit);
termios_new.c_oflag = 0;
termios_new.c_lflag |= 0;
termios_new.c_oflag &= ~OPOST;
termios_new.c_cc[VTIME] = 1; /* unit: 1/10 seco
没有合适的资源?快使用搜索试试~ 我知道了~
M6708U-512LIV1_00光盘例程.rar_5.1 hdmi_M6708_M6708U_USB 1000m_周立功工控板
共26个文件
c:11个
makefile:10个
sh:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 4 浏览量
2022-07-13
21:47:16
上传
评论
收藏 19KB RAR 举报
温馨提示
周立功M6708U-512LIV1_00工控板源程序,全套光盘例程,掏银子才可以得到的。M6708 运行 Linux-3.0.15,包含如下源程序: 1.显示驱动程序(支持 TFT 液晶屏,1 路 HDMI,2 路 LVDS); 2. 触摸屏驱动程序; 3.SD/MMC 卡驱动程序; 4.USB Host 驱动程序,支持 USB键盘、USB鼠标和 U盘; 5.CAN-Bus 驱动程序(Socket CAN 接口); 6.10/100/1000M 自适应以太网驱动程序; 7.RS-232C 接口驱动程序; 8.RTC 驱动程序; 9.GPIO 驱动程序; 10. 看门狗驱动程序; 11.CAMERA驱动;
资源详情
资源评论
资源推荐
收起资源包目录
M6708U-512LIV1_00光盘例程.rar (26个子文件)
示例
NetWork
udp
listener
listener.c 3KB
Makefile 341B
utalker
Makefile 256B
talker.c 3KB
tcp
server
server.c 4KB
Makefile 337B
uclient
client.c 3KB
Makefile 241B
wdt
watchdog.c 712B
Makefile 261B
hello
hello.c 152B
Makefile 241B
uart_raw
Makefile 250B
uart_raw.c 1KB
cantest
config.sh 110B
cantest.c 3KB
Makefile 161B
gpio
gpio_system.c 508B
gpio_buzzer.sh 259B
Makefile 74B
gpio_system 6KB
serial
test_uart.c 1KB
Makefile 236B
serial.h 627B
serial.c 10KB
Ver_1_00 0B
共 26 条
- 1
APei
- 粉丝: 83
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- NE555+74LS192+74LS48电子秒表课程设计报告(纯数电实现)
- 基于深度学习的视频描述综述:视觉与语言的桥梁
- 2024年全球干式变压器行业规模及市场占有率分析报告
- 小红书2024新年市集合作方案解析与品牌营销策略
- 基于javaweb的沙发销售管理系统论文.doc
- 毕业设计Jupyter Notebook基于深度网络的垃圾识别与分类算法研究项目源代码,用PyTorch框架中的transforms方法对数据进行预处理操作,后经过多次调参实验,对比不同模型分类效果
- 基于java的扫雷游戏的设计与实现论文.doc
- 基于java的企业员工信息管理系统论文.doc
- 深度视频压缩框架:从预测编码到条件编码的技术革新
- 1221额的2的2的2额
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0