commrs232.c
来自「STC12C5410AD模拟串口再现多串口通讯」· C语言 代码 · 共 1,073 行 · 第 1/3 页
C
1,073 行
Rs232CommSendByteWithChk(0x01); //
Rs232CommSendWordWithChk(0x55AA);
Rs232ComCalculChkSum = (~Rs232ComCalculChkSum)+1;
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum>>8));
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum));
rs_send_byte(0x0D);
while (rs_f_TI == 0); //等待最后一个字节发送完毕
}
/********************************************************************************************************
** 函数名称: void Comm_rs232_readanalog(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Comm_rs232_readanalog(void)
{
uint8 i;
rs_send_byte(0x7E); //报文头
Rs232ComCalculChkSum = 0;
Rs232CommSendByteWithChk((uint8)(ModeSysConfig[8])); //BoardAddress地址范围0~16
Rs232CommSendByteWithChk(Rs232CommCid); //CID 02
Rs232CommSendWordWithChk(0xF05C); //LENTH
Rs232CommSendByteWithChk(0x01); //
for(i = 0;i < 15;i++) //15只池电电压
{
Rs232CommSendWordWithChk(CellVoltage[i]);
}
Rs232CommSendByteWithChk(0x02); //
for(i = 0;i < 9;i++) //15只电池温度,1路环境温度
{
CommSendByteWithChk(Temperature[i]);
}
Rs232CommSendByteWithChk(0x03);
Rs232CommSendWordWithChk(ChargeCurrent); //充电电流
Rs232CommSendWordWithChk(DisChargeCurrent); //放电电流
Rs232ComCalculChkSum = (~Rs232ComCalculChkSum)+1;
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum>>8));
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum));
rs_send_byte(0x0D);
while (rs_f_TI == 0); //等待最后一个字节发送完毕
}
/********************************************************************************************************
** 函数名称: void Comm_rs232_read_digital(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Comm_rs232_read_digital(void)
{
rs_send_byte(0x7E); //报文头
Rs232ComCalculChkSum = 0;
Rs232CommSendByteWithChk((uint8)(ModeSysConfig[8])); //BoardAddress地址范围0~16
Rs232CommSendByteWithChk(Rs232CommCid); //CID 03
Rs232CommSendWordWithChk(0x501A); //LENTH
Rs232CommSendByteWithChk(0x01); //
Rs232CommSendByteWithChk(ComAlarmET ); //综合告警
Rs232CommSendByteWithChk(ComAlarmVh0); //1~ 8只电池过压告警
Rs232CommSendByteWithChk(ComAlarmVh1); //9~15只电池过压告警
Rs232CommSendByteWithChk(ComAlarmVl0); //1~ 8只电池欠压告警
Rs232CommSendByteWithChk(ComAlarmVl1); //9~15只电池欠压告警
Rs232CommSendByteWithChk(ComAlarmScCTh); //1~15只电池充电过温
Rs232CommSendByteWithChk(ComAlarmScCTl); //1~15只电池充电欠温
Rs232CommSendByteWithChk(ComAlarmSDcTh); //1~15只电池放电过温
Rs232CommSendByteWithChk(ComAlarmSDcTl); //1~15只电池放电欠温
Rs232CommSendByteWithChk(ComTemperaSensor ); //1~ 8路温度传感器失效
Rs232CommSendByteWithChk(ComVoltageSensor0); //电压传感器失效
Rs232ComCalculChkSum = (~Rs232ComCalculChkSum)+1;
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum>>8));
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum));
rs_send_byte(0x0D);
while (rs_f_TI == 0); //等待最后一个字节发送完毕
}
/********************************************************************************************************
** 函数名称: void Comm_rs232_read_config(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Comm_rs232_read_config(void)
{
uint8 i;
rs_send_byte(0x7E); //报文头
Rs232ComCalculChkSum = 0;
Rs232CommSendByteWithChk((uint8)(ModeSysConfig[8])); //BoardAddress地址范围0~16
Rs232CommSendByteWithChk(Rs232CommCid); //CID 05
Rs232CommSendWordWithChk(0x9052); //LENTH
Rs232CommSendByteWithChk(0x01); //电压限值配置信息
for(i = 0;i < 9;i++)
{
Rs232CommSendWordWithChk(ModeSysConfig[i]);
}
Rs232CommSendByteWithChk(0x02); //温度限值配置信息
for(i = 0;i < 12;i++)
{
Rs232CommSendByteWithChk(ModeTempCofing[i]);
}
Rs232CommSendByteWithChk(0x03); //系数
Rs232CommSendWordWithChk(CUK);
Rs232CommSendWordWithChk(CTK);
Rs232CommSendWordWithChk(OTK);
Rs232ComCalculChkSum = (~Rs232ComCalculChkSum)+1;
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum>>8));
Rs232CommSendByteWithoutChk((uint8)(Rs232ComCalculChkSum));
rs_send_byte(0x0D);
while (rs_f_TI == 0); //等待最后一个字节发送完毕
}
/********************************************************************************************************
** 函数名称: void Comm_rs232_write_vconfig(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
//void Comm_rs232_write_vconfig(void) //电压限值存储在第一扇区
//{
// uint8 i,c;
// uint16 address;
// c = 0;
// address = 0x2800;
// for(i = 1;i < 17;)
// {
// MoveVXdata[c] = ((CommRxBuf[i] << 8)|CommRxBuf[i+1]);
// i++;
// i++;
// c++;
// }
// EepSectotErase(address);
// for(i = 0;i < 8;i++)
// {
// EepWriteWord(MoveVXdata[i],address);
// address++;
// address++;
// }
// for(i = 0;i < 8;i++) //将修正值赋给动作变量
// {
// ModeSysConfig[i] = EepReadWord(address);
// }
// rs232_commright();
//}
/********************************************************************************************************
** 函数名称: void Comm_rs232_write_tconfig(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
//void Comm_rs232_write_tconfig(void) //温度限值存储在第二扇区
//{
// uint8 i;
// uint16 address;
// address = 0x2A00;
// for(i = 1;i < 13;i++)
// {
// MoveTdata[i] = CommRxBuf[i];
// }
// EepSectotErase(address);
// for(i = 0;i < 12;i++)
// {
// EepWriteByte(MoveTdata[i],address);
// address++;
// }
// for(i = 0;i < 12;i++) //将修正值赋给动作变量
// {
// ModeTempCofing[i] = EepReadByte(address);
// }
// rs232_commright();
//}
/********************************************************************************************************
** 函数名称: void Comm_rs232_write_xconfig(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Comm_rs232_write_xconfig(void) //系数存储在第三扇区
{
uint8 i,c,j;
uint16 address;
c = 0;
j = 0;
address = 0x2C00;
for(i = 1;i < 7;)
{
MoveVXdata[c] = ((rs232buffer[i] << 8)|rs232buffer[i+1]);
i++;
i++;
c++;
}
for(i = 0;i < 3;i++)
{
if((MoveVXdata[i] > 6000) && (MoveVXdata[i] < 15000))j++;
}
if(j == 3)
{
j = 0;
EepSectotErase(address);
for(i = 0;i < 3;i++)
{
EepWriteWord(MoveVXdata[i],address);
address++;
address++;
}
CUK = EepReadWord(address);
CTK = EepReadWord(address+2);
OTK = EepReadWord(address+4);
rs232_commright();
}
else
{
j = 0;
rs232_commwrong();
}
}
/********************************************************************************************************
** 函数名称: void Rs232CommManage(void)
** 功能描述:
** 输 入: 无
** 输 出: 无
** 全局变量:
** 调用模块:
**
** 作 者: 李伟
** 日 期: 2008年12月20日
**-------------------------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Rs232CommManage(void)
{
switch(Rs232CommCid)
{
case RS232_HAND_SHAKE: //握手命令 1
{
Comm_rs232_handshake();
}break;
case RS232_READ_ANALOG: //读模拟量 2
{
Comm_rs232_readanalog();
}break;
case RS232_READ_DIGITAL: //读数字量 3
{
Comm_rs232_read_digital();
}break;
case RS232_READ_CONFIG: //读配置 4
{
Comm_rs232_read_config();
}break;
case RS232_WRITE_VCONFIG: //电压限值配置 5
{
// Comm_rs232_write_vconfig();
}break;
case RS232_WRITE_TCONFIG: //电压限值配置 6
{
// Comm_rs232_write_tconfig();
}break;
case RS232_WRITE_XCONFIG: //电压限值配置 7
{
Comm_rs232_write_xconfig();
}break;
default: break;
}
}
/********************************************************************************************************
End of file
********************************************************************************************************/
//void main (void)
//{
////首先发送 128 个字节 00H--7FH, 然后等待 PC 机发送的数据。当收到 128
////个字节后,立刻将收到的 128 个数据回发送给 PC 机,然后继续等待下一个
////数据块。
//
// uint8 i;
// initiate_MCU(); //系统初始化
//
// soft_send_enable (); //允许发送,禁止接收
// for (i=0; i < rs_RECEIVE_MAX; i++ )
// {
// COMEN = CommTxden;
// rs_send_byte(i);
// }
// while ( rs_f_TI == 0) ; // 等待最后一个字节发送完毕
//
// while(1)
// {
// COMEN = CommRxden;
// soft_receive_enable (); //启动并开始接收,禁止发送
// while (ReceivePoint < rs_RECEIVE_MAX); // 等待接收缓冲区满
//
// soft_send_enable (); //允许发送,禁止接收
// for (i=0; i < rs_RECEIVE_MAX; i++ )
// {
// COMEN = CommTxden;
// rs_send_byte(rs232buffer[i]);
// }
// while ( rs_f_TI == 0) ; //等待最后一个字节发送完毕
// ReceivePoint = 0;
// COMEN = CommRxden;
// }
//}
/********************************************************************************************************
End of file
********************************************************************************************************/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?