C++ RS232串口通信

头文件

#ifndef SERIALCOM_H
#define SERIALCOM_H
#include <sys/select.h>

#include <stdio.h>
class SerialCom
{
public:
    SerialCom();
    ~SerialCom();
    int openCom(char *pcom,int baud);
    int sent_to(char *pstr,int len);
    int recv_from(char *precvBuf);
    void closeCom();
    int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
private:
    int comHandle;
    fd_set m_stRfds;
};

#endif // SERIALCOM_H

源文件

#include "serialcom.h"

#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>


SerialCom::SerialCom()
{
    comHandle = 0;
}
SerialCom::~SerialCom()
{
    if(comHandle)
        closeCom();
    comHandle = 0;
}

int SerialCom::set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		g_MainCoreLog->LogError("[%s][%s]()-[%d]SetupSerial  "
		, __FILE__, __FUNCTION__, __LINE__);

		return -1;
	}
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;
	newtio.c_cflag &= ~CSIZE;
 
	switch( nBits )
	{
		case 7:
			newtio.c_cflag |= CS7;
			break;
		case 8:
			newtio.c_cflag |= CS8;
			break;
	}
 
	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK );
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}
 
	switch( nSpeed )
	{
		case 2400:
			cfsetispeed(&newtio, B2400);
			cfsetospeed(&newtio, B2400);
			break;
		case 4800:
			cfsetispeed(&newtio, B4800);
			cfsetospeed(&newtio, B4800);
			break;
		case 9600:
			cfsetispeed(&newtio, B9600);
			cfsetospeed(&newtio, B9600);
			break;
		case 115200:
			cfsetispeed(&newtio, B115200);
			cfsetospeed(&newtio, B115200);
			break;
		case 460800:
			cfsetispeed(&newtio, B460800);
			cfsetospeed(&newtio, B460800);
			break;
		default:
			cfsetispeed(&newtio, B9600);
			cfsetospeed(&newtio, B9600);
			break;
	}
	if( nStop == 1 )
        {
		newtio.c_cflag &=  ~CSTOPB;
        }
	else if ( nStop == 2 )
	{
		newtio.c_cflag |=  CSTOPB;
	}	

	newtio.c_cc[VTIME]  = 1;///* 设置超时10 seconds*/
	newtio.c_cc[VMIN] = 8;
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		g_MainCoreLog->LogError("[%s][%s]()-[%d]com set error  "
		, __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}
	
	return 0;
}
int SerialCom::openCom(char *pcom,int baud)
{
    struct termios termios;
    g_MainCoreLog->LogInfo("[%s][%s]()-[%d]openCom  "
		, __FILE__, __FUNCTION__, __LINE__);
    bzero(&termios,sizeof(termios));

    comHandle = open(pcom,O_RDWR|O_NOCTTY); //|O_NOCTTY|O_NONBLOCK
    if(comHandle < 0)
    {
       g_MainCoreLog->LogError("[%s][%s]()-[%d]com open failed  "
		, __FILE__, __FUNCTION__, __LINE__);
       return -1;
    }
       g_MainCoreLog->LogInfo("[%s][%s]()-[%d]opecom open successed,COM %d Baud:%d "
		, __FILE__, __FUNCTION__, __LINE__,comHandle,baud);
    set_opt(comHandle, baud, 8, 'N', 1);

    return 0;
}
int SerialCom::sent_to(char *pstr,int len)
{
    if(comHandle <= 0)
        return -1;

    if (write(comHandle, pstr, len) != len)
    {
	g_MainCoreLog->LogError("[%s][%s]()-[%d]send cmd failed "
		, __FILE__, __FUNCTION__, __LINE__);
        return -1;
    }
    
    return 0;

}
int SerialCom::recv_from(char *precvBuf)
{
    if(comHandle <= 0)
    {
        printf("failed comHandle <= 0 \r\n");
	g_MainCoreLog->LogError("[%s][%s]()-[%d]failed comHandle <= 0 "
		, __FILE__, __FUNCTION__, __LINE__);
        return -1;
    }

    int rlen = 0;
    //for(int i=0;i<8;i++)
    {
        // printf("start recv 1 byte \r\n");
        tcflush(comHandle,TCIFLUSH);
        int iNum = read(comHandle,(void*)precvBuf,8);
	   // int iNum = read(comHandle,(void*)precvBuf,8);
        //printf("iNum:%d \r\n",iNum);
        rlen+=iNum;
    }
    return rlen;
}
void SerialCom::closeCom()
{
    if(comHandle <= 0)
        return;
    close(comHandle);
}

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

设备系统软件集成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值