串口程序设计

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <pthread.h>
 
#define BAUDRATE B115200
#define COM1 "/dev/ttyS0"
#define COM2 "/dev/ttyS1"
#define ENDMINITERM 27           /* ESC to quit miniterm */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;
volatile int fd;
 
void child_handler(int s)
{
  printf("stop!!!\n");
   STOP=TRUE;
}
/*--------------------------------------------------------*/
void* keyboard(void * data)
{
    int c;
 for (;;){
   c=getchar();
        if( c== ENDMINITERM){
          STOP=TRUE;
          break ;
        }
 }
    return NULL;
}
/*--------------------------------------------------------*/
/* modem input handler */
void* receive(void * data)
{
 int c;
    printf("read modem\n");
    while (STOP==FALSE) 
    {
         read(fd,&c,1); /* com port */
     write(1,&c,1); /* stdout */
    }
    printf("exit from reading modem\n");
    return NULL; 
}
/*--------------------------------------------------------*/
void* send(void * data)
{
    int c='0';
    printf("send data\n");
     while (STOP==FALSE) /* modem input handler */
    {
     c++;
     c %= 255;
     write(fd,&c,1); /* stdout */
     usleep(100000);
    }
     return NULL; }
/*--------------------------------------------------------*/
int main(int argc,char** argv)
{
 struct termios oldtio,newtio,oldstdtio,newstdtio;
 struct sigaction sa;
 int ok;
    pthread_t th_a, th_b, th_c;
    void * retval;
 
 if( argc > 1)
         fd = open(COM2, O_RDWR );
    else  
       fd = open(COM1, O_RDWR ); //| O_NOCTTY |O_NONBLOCK);
 if (fd <0) {
     error(COM1);
     exit(-1);
    }
    tcgetattr(0,&oldstdtio);
    tcgetattr(fd,&oldtio);           /* save current modem settings */
    tcgetattr(fd,&newstdtio);        /* get working stdtio */
 newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;       /*ctrol flag*/
 newtio.c_iflag = IGNPAR;        /*input flag*/
 newtio.c_oflag = 0;            /*output flag*/
    newtio.c_lflag = 0;
   newtio.c_cc[VMIN]=1;
 newtio.c_cc[VTIME]=0;
 /* now clean the modem line and activate the settings for modem */
   tcflush(fd, TCIFLUSH);
 tcsetattr(fd,TCSANOW,&newtio);                        /*set attrib*/

    sa.sa_handler = child_handler;
    sa.sa_flags = 0; 
    sigaction(SIGCHLD,&sa,NULL);                        /* handle dying child */
     pthread_create(&th_a, NULL, keyboard, 0);
     pthread_create(&th_b, NULL, receive, 0);
     pthread_create(&th_c, NULL, send, 0);
    pthread_join(th_a, &retval);
    pthread_join(th_b, &retval);
    pthread_join(th_c, &retval);
    tcsetattr(fd,TCSANOW,&oldtio);            /* restore old modem setings */
    tcsetattr(0,TCSANOW,&oldstdtio);          /* restore old tty setings */
    close(fd);
    exit(0); 
}

C#串口介绍以及简单串口通信程序设计实现 源代码和串口程序介绍连接:https://www.cnblogs.com/JiYF/p/6618696.html 本站积分太贵,自己变得。。直接到连接地址下载代码 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍   串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。(至于再详细,自己百度) 串口应用:   工业领域使用较多,比如:数据采集,设备控制等等,好多都是用串口通信来实现!你要是细心的话,你会发现,目前家用国网智能电能表就具备RS485通信总线(串行总线的一种)与RS232可以相互转化(当然一般,非专业的谁也不会闲的蛋疼,趴电表上瞎看,最多也就看看走了多少度电) RS232 DB9介绍: 1.示意图 2.针脚介绍: 载波检测(DCD) 接受数据(RXD) 发出数据(TXD) 数据终端准备好(DTR) 信号地线(SG) 数据准备好(DSR) 请求发送(RTS) 清除发送(CTS) 振铃指示(RI) 3.实物图: 以下是我购买XX公司的一个usb转串口线:这个头就是一个公头,另一端是一个usb口 笨小孩串口工具运行图: 1.开启程序 2.发送一行字符串HelloBenXH,直接将针脚的发送和接收链接起来就可以测试了(针脚2 接受数据(RXD) 和3 发出数据(TXD))直接链接, C#代码实现:采用SerialPort 1.实例化一个SerialPort [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 private SerialPort ComDevice = new SerialPort(); 2.初始化参数绑定接收数据事件 [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 public void init() { btnSend.Enabled = false; cbbComList.Items.AddRange(SerialPort.GetPortNames()); if (cbbComList.Items.Count > 0) { cbbComList.SelectedIndex = 0; } cbbBaudRate.SelectedIndex = 5; cbbDataBits.SelectedIndex = 0; cbbParity.SelectedIndex = 0; cbbStopBits.SelectedIndex = 0; pictureBox1.BackgroundImage = Properties.Resources.red; ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值