1——东西方向红灯;
2——东西方向黄灯;
3——东西方向绿灯;
4—— 避开发送 0x0A的底层却发送0x0D 0x0A问题
5——南北方向红灯;
6——南北方向黄灯;
7——南北方向绿灯。
https://blog.csdn.net/qiaoliang328/article/details/6153998 0x0A的底层却发送0x0D 0x0A问题
按上面博客链接修改了,但是并没有起作用。所以我修改了接线,避开了 0x0A
1 接口头文件
/**
* @file socket_uart.hpp
* @brief uart socket class
* @author
* @version 1.0.0
* @date 2019-03-27
* @par history:
* | version | date | author | description |
* | ------- | ---- | ------ | ----------- |
* | 1.0.0 | 2019-03-27 | | create |
*/
#ifndef __SOCKET_UART_HPP_INCLUDED__
#define __SOCKET_UART_HPP_INCLUDED__
#include "socket_base.hpp"
namespace com {
class socket_uart_t : public socket_base_t {
public:
socket_uart_t();
~socket_uart_t();
void* get_sock() { return NULL; }
int get_fd();
int setsockopt(sock_option_t opt, const void* val, int len) { return E_NOTIMPL; }
int getsockopt(sock_option_t opt, void *val, int* len) const { return E_NOTIMPL; }
int bind(const std::string& addr);
int unbind(const char* addr);
int connect(const std::string& addr) { return E_NOTIMPL; }
int disconnect(const char* addr) { return E_NOTIMPL; }
void close();
/**
* @brief receive data from socket
* @param[out] buf buffer of received data
* @param[in] len max length of buffer
* @param[out] addr receive address
* @return length of received data
* @retval <0 receive fail
* @retval other length of received data
*/
int recv(void* buf, int len, std::string* addr);
/**
* @brief send data on the socket
* @param[in] buf buffer of send data
* @param[in] len length of buffer
* @param[in] addr send address,if it is empty, the data will be send to the connected address.
* @return length of send data
* @retval <0 send fail
* @retval other length of send data
*/
int send(const void* buf, int len, const std::string* addr);
/**
* @brief receive message from socket
* @param[out] msg received message
* @param[in] addr send address,if it is empty, the data will be send to the connected address.
* @return length of received data
* @retval <0 receive fail
* @retval other length of received data
*/
int recv(socket_msg_t& msg, std::string* addr);
/**
* @brief send msg on the socket
* @param[in] msg send message
* @param[in] addr send address,if it is empty, the data will be send to the connected address.
* @return length of send data
* @retval <0 send fail
* @retval other length of send data
*/
int send(const socket_msg_t& msg, const std::string* addr);
private:
int fd_;
};
}
#endif // __SOCKET_UART_HPP_INCLUDED__
2接口实现文件
/**
* @file socket_uart.cpp
* @brief uart socket class
* @author xxxxxxxxxxx
* @version 1.0.0
* @date 2019-03-27
* @par history:
* | version | date | author | description |
* | ------- | ---- | ------ | ----------- |
* | 1.0.0 | 2019-03-27 | xxxxxx | create |
*/
#include "socket_uart.hpp"
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace com {
socket_uart_t::socket_uart_t()
{
fd_ = 0;
}
socket_uart_t::~socket_uart_t()
{
close();
}
int socket_uart_t::get_fd()
{
return fd_;
}
int socket_uart_t::bind(const std::string& addr)
{
std::cout<<"socket_uart_t::bind:"< 0)
{
ret = E_ALREADY;
break;
}
// parse address
prot_type = parse_addr(addr, name, &baud);
prot_type = SOCK_PROTOCOL_UART;
name="/dev/ttymxc2";
if (SOCK_PROTOCOL_UART != prot_type)
{
ret = E_NOTSUPT;
break;
}
// open uart
ret = open(name.c_str(), O_RDWR|O_NOCTTY|O_NDELAY);
if(-1 == ret)
{
ret = E_OPENFAIL;
break;
}
std::cout<<"open uart fd_ =ret= "< 0)
{
std::cout<<"socket uart write begin..."< 0)
{
msg.build(buf, ret);
}
} while(0);
return ret;
}
};
1绑定串口
int appi_spat_t::bind_uart(std::string& addr)
{
int ret=E_INVAL;
std::cout<<"bind uart start"<info("bind_uart start");
ret=socket_uart_.bind(addr);
log_->info("bind_uart ret:%d ",ret);
std::cout<<"bind uart ret:"<error("bind_uart failed!");
}
return ret;
}
2、根据通信协议 组数据包
int appi_spat_t::get_uart_light(int phase1_light,int phase2_light)
{
//根据红绿灯串口协议组一个串口帧
int ret = E_OK;
uart_rx_data_[0]=0x33;
uart_rx_data_[1]=0x01;
uart_rx_data_[2]=0x16;
uart_rx_data_[3]=0x00;
uart_rx_data_[4]=0x00;
uart_rx_data_[6]=0x08;
if(( TRA_LIGHT_STATE_RED == phase1_light) && //3
(TRA_LIGHT_STATE_GREEN == phase2_light)) //5
{
uart_rx_data_[5]=0x41;//100 0001
uart_rx_data_[7]=0x93;
}
if(( TRA_LIGHT_STATE_RED == phase1_light) && //3
(TRA_LIGHT_STATE_YELLOW == phase2_light)) //7
{
uart_rx_data_[5]=0x21;//010 0001
uart_rx_data_[7]=0x73;
}
if(( TRA_LIGHT_STATE_RED == phase2_light) && //3
(TRA_LIGHT_STATE_GREEN == phase1_light)) //5
{
uart_rx_data_[5]=0x14;//001 0100
uart_rx_data_[7]=0x66;
}
if(( TRA_LIGHT_STATE_RED == phase2_light) && //3
(TRA_LIGHT_STATE_YELLOW == phase1_light)) //7
{
uart_rx_data_[5]=0x12;//001 0010
uart_rx_data_[7]=0x64;
}
if(uart_rx_data_[5] <= 0)
{
log_->error("get_uart_light fault!");
ret = E_FALT;
}
log_->info("get uart light data_: %X",uart_rx_data_[5]);
return ret;
}
组报文的方法二
void appi_spat_t::get_uart_light(int phase1_light,int phase2_light)
{ //根据红绿灯串口协议组一个串口帧
uart_rx_data_[0]=0x33;
uart_rx_data_[1]=0x01;
uart_rx_data_[2]=0x16;
uart_rx_data_[3]=0x00;
uart_rx_data_[4]=0x00;
uart_rx_data_[6]=0x08;
switch(phase1_light)
{
case TRA_LIGHT_STATE_RED: //3
{
switch(phase2_light)
{
case TRA_LIGHT_STATE_RED:
{
log_->error("get_uart_light fault!");
ret = E_FALT;
break;
}
case TRA_LIGHT_STATE_YELLOW: //7
{
//char buff[]="0X33 0X01 0X16 0X00 0X00 0X11 0X08 0X63";//010 001
uart_rx_data_[5]=0x11;//010 001
uart_rx_data_[7]=0x63;
break;
}
case TRA_LIGHT_STATE_GREEN: //5
{
//char buff[]="0X33 0X01 0X16 0X00 0X00 0X21 0X08 0X73";//100 001
uart_rx_data_[5]=0x21;//100 001
uart_rx_data_[7]=0x73;
break;
}
}
break;
}
case TRA_LIGHT_STATE_YELLOW: //7
{
switch(phase2_light)
{
case TRA_LIGHT_STATE_RED: //3
{
//char buff[]="0X33 0X01 0X16 0X00 0X00 0X0A 0X08 0X5C";//001 010
uart_rx_data_[5]=0x0A;//001 010
uart_rx_data_[7]=0x5C;
break;
}
case TRA_LIGHT_STATE_YELLOW:
{
log_->error("get_uart_light fault!");
ret = E_FALT;
break;
}
case TRA_LIGHT_STATE_GREEN: //5
{
//char buff[]="0X33 0X01 0X16 0X00 0X00 0X22 0X08 0X74";//100 010
uart_rx_data_[5]=0x22;//100 010
uart_rx_data_[7]=0x74;
break;
}
}
break;
}
case TRA_LIGHT_STATE_GREEN: //5
{
switch(phase2_light)
{
case TRA_LIGHT_STATE_RED: //3
{
//char buff[]="0X33 0X01 0X16 0X00 0X00 0X0C 0X08 0X5E";//001 100
uart_rx_data_[5]=0x0C;//001 100
uart_rx_data_[7]=0x5E;
break;
}
case TRA_LIGHT_STATE_YELLOW: //7
{
//char buff[]="0X33 0X01 0X16 0X00 0X00 0X14 0X08 0X66";//010 100
uart_rx_data_[5]=0x14;//010 100
uart_rx_data_[7]=0x66;
break;
}
case TRA_LIGHT_STATE_GREEN:
{
log_->error("get_uart_light fault!");
break;
}
}
break;
}
}
log_->info("get uart light data_: %X",uart_rx_data_[5]);
return ;
}
3、向串口发送
int appi_spat_t::send_uart(const void* buf, int len, const std::string* addr)
{
int ret=E_INVAL;
std::cout<<"socket_uart send"<error("send_uart failed!");
}
return ret;
}
我并没有写发送返回检查程序,串口发送报文,接收模块会根据协议返回对应的返回码,
根据返回码,可以知道发送的报文是否正确