SerialPort 1.0beta
|
串口测试示例 更多...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include "debug-msg.h"
#include "serialport.h"
函数 | |
void * | write_port_thread (void *argc) |
void * | read_port_thread (void *argc) |
void | sig_handle (int sig_num) |
int | main (void) |
变量 | |
pthread_t | write_tid |
pthread_t | read_tid |
char * | buf = "Are you going to die? " |
char | tmp [512] |
详细描述
串口测试示例
Copyleft (C) 2010 Late Lee This program is tested on LINUX PLATFORM, WITH GCC 4.x. The program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Please feel free to use the program, and I feel free to ignore the related issues. Any questions or suggestions, or bugs, please contact me at $ echo -n "aHR0cDovL3d3dy5sYXRlbGVlLm9yZwo=" | base64 -d or e-mail to $ echo -n "bGF0ZWxlZUAxNjMuY29tCg==" | base64 -d if you want to do this.
- 日期:
- 2011年1月10日
- 注解:
- 注意,本程序文件使用UTF8编码,需要使用gcc4.4.0以上编译器方能支持。 (本程序在gcc 4.4.5版本测试通过)
- 测试:
- 输入
gcc main.c serialport.c -lthread
来编译程序,默认生 成的可执行文件为a.out
(当然,可以使用gcc的-o选项改变生成文件名称). 输入./a.out
来运行该程序.注意,须使用root权限来运行,因为 打开串口需要这个权限,如果出错,程序会有相应提示。
本人测试如下:在tty1终端上运行该程序,在tty2终端上运行minicom,
短接串口线的2、3脚,即可在minicom中看到程序中发送的数据。 注意,minicom的设置需与程序中设置一致(如果波特率、数据位,等)。
疑问:似乎在程序中已经设置串口为阻塞方式,但依然可以使用minicom打开 相同的串口,目前未找到原因。
另:本人虽在虚拟机中使用物理串口测试,但这不应该影响测试的真实性。
又:本想在同一程序中设置两个线程,分别写、读串口,但测试不成功。
下面是测试图示:

程序正在写数据

minicom中显示读到的数据
下面是串口公头与母头

A 9-pin male D-Sub connector(*)

A 9-pin female D-Sub connector
(*)测试使用公头。
函数文档
void* read_port_thread | ( | void * | argc | ) |
read_port_thread - 从串口读数据的线程
- 参数:
-
argc : 此处为传递进来的串口文件描述符
- 注解:
- 该函数测试不成功
void sig_handle | ( | int | sig_num | ) |
sig_handle - 信号处理函数
- 参数:
-
sig_num : signal函数第二个参数要求带一参数
- 注解:
- 该函数未使用
void* write_port_thread | ( | void * | argc | ) |
write_port_thread - 写数据到串口的线程
- 参数:
-
argc : 此处为传递进来的串口文件描述符
- 注解:
- 该函数仅为测试目的,并不是真正使用中的代码
变量文档
char* buf = "Are you going to die? " |
我们发送的数据
pthread_t read_tid |
“读数据”线程ID
char tmp[512] |
接收数据缓冲区
pthread_t write_tid |
“写数据”线程ID
Generated at 2011年一月17日 星期一 10:44:34 for SerialPort by
