?? modbus.c
字號:
/*************************************************************************************/
#ifdef MODBUS_COMM
//MODBUS 命令定義
#define REGISTERREAD 0x03 //讀出存儲寄存器的內容
#define REGISTERWRITEONBYTE 0x06 //改寫一個特定的寄存器的數據
#define REGISTERWRITE 0x10 //連續寫入多個存儲寄存器
#define COMMTEST 0x08 //通訊測試
//MODBUS 寄存器地址定義
//1.系統參數及通訊參數
#define Device_Code 0x00 //設備識別號
#define Device_Comm_Sel 0x01 //設備通訊模式寄存器
#define Device_Comm_Pr 0x02 //設備通訊參數(通訊地址,通訊速率)
#define Device_Tcp_Iph 0x03 //TCP 通訊IP地址高字節寄存器
#define Device_Tcp_Ipl 0X04 //TCP 通訊IP地址低字節寄存器
#define Device_Tcp_Maskh 0X05 //TCP 通訊子網掩碼地址高字節寄存器
#define Device_Tcp_Maskl 0X06 //TCP 通訊子網掩碼地址低字節寄存器
#define Device_Tcp_Gateh 0X07 //TCP 通訊網關地址高字節寄存器
#define Device_Tcp_Gatel 0X08 //TCP 通訊網關地址低字節寄存器
//2.系統時間
#define Minute_Second 0x09 //分,秒
#define Day_Hour 0x0a //日,小時
#define Year_Month 0x0b //年,月
#define Bak 0X0c //預留備用寄存器 oc-oh
//其他寄存器
#define Anomalous_Staus 0x10 //異常狀態寄存器
//3.機器實時運行參數
#define Ux_Volt 0x20 //系統電壓
#define Uc_Volt 0x21 //機端電壓
#define Ut_Volt 0x22 //調差電壓
#define Ilc_Value 0x23 //勵磁電流
#define Cos_Value 0x24 //功率因數
#define Q_Value 0x25 //無功功率
#define Fre_Jd 0x26 //機端頻率
#define Alf_Value 0x27 //觸發相位角
#define Ug_Volt 0x28 //給定電壓
#define Ilg_Value 0x29 //給定電流
#define Cosg_Value 0x2a //給定功率因數
#define Qg_Value 0x2b //給定無功
#define Alfg_Value 0x2c //給定ALF角度
#define Bak 0x2d //預留備用寄存器 2d-2f
//4.機器開入開出狀態寄存器
#define Switch_In 0x30 //開入輸入變量
#define Switch_Out 0x31 //開入輸入變量
//5.機器實時運行狀態寄存器
#define Run_Staus 0x32 //機器實時運行狀態寄存器
#define Run_Bak 0x33 //狀態備用寄存器 33-3f
//6.勵磁設置參數寄存器
#define P_Set 0x40 //PID參數P寄存器
#define I_Set 0x41 //PID參數I寄存器
#define D_Set 0x42 //PID參數D寄存器
#define Kq_Set 0x43 //無功功率比例參數寄存器
#define Ile_Set 0x44 //勵磁電流比例參數寄存器
#define Ct_Set 0x45 //CT 變比參數寄存器
#define Pt_Set 0x46 //PT 變比參數寄存器
#define Tc_Set 0x47 //調差系數寄存器
#define LcIncDec_Set 0x48 //增減勵寄存器
#define AutoStartStop_Set 0x49 //自動開機停機寄存器
#define RunMode_Set 0x4a //運行模式寄存器
#define UxTron_Set 0x4b //網跟控制寄存器
#define Excitation_Set 0x4c //高低起勵寄存器
#define MkActive_Set 0x4d //滅磁開關寄存器
#define Excitation_Suppression_Set 0x4e //起勵滅磁寄存器
#define Bak_Set 0x4f //預留備用寄存器
// #define
//全局變量定義:
//UART2 緩沖單元
unsigned char xdata UART2_ReceiveBuffer[UART_DATAPACK_SIZE]; //接收緩沖單元
unsigned char UART2_ReceiveCount = 0; //接收的字節數
unsigned char UART_SendNum = 0; //發送的字節數
unsigned char xdata UART_SendOk = 0; //已經發送的字節數
unsigned char xdata UART2_ReceiveBuffer[UART_SENDPACK_SIZE]; //發送緩沖單元
//擴展緩沖單元
unsigned char xdata SPI_ReceiveBuffer[SPI_DATAPACK_SIZE]; //接收緩沖單元
unsigned char SPI_ReceiveCount = 0; //接收的字節數
unsigned char SPI_SendNum = 0; //發送的字節數
unsigned char xdata SPI_SendOk = 0; //已經發送的字節數
unsigned char xdata SPI_SendBuffer[SPI_SENDPACK_SIZE]; //發送緩沖單元
//本身地址
unsigned int My_Addr
//液晶緩沖單元
unsigned char xdata Yj_ReceiveBuffer[]; //接收緩沖單元
unsigned char xdata Yj_SendBuffer[]; //發送緩沖單元
//液晶接收處理標志位
unsigned int Yj_ReceiveFlag; //液晶接收緩沖單元中是否有數據要求液晶進行處理標志位
unsigned int Yj_SendFlag; //液晶發送緩沖單元中是否有數據要求液晶進行處理標志位
//----------------------------SPI發送程序--------------------------------//
unsigned char SPISendComm(unsigned char *buf,unsigned char len)
{
unsigned int timeout=0;
if (len > SPI_SENDPACK_SIZE) //長度大于UART長度,出錯
return -1;
if (SPI_SendOk != 0) //已經發送的字節數不為0
{
while (SPI_SendOk != 0)
{
timeout++;
if (timeout >= 3000)
{
SPI_SendOk = 0; //已經發送的字節數清0
SPI_SendNum = 0; //發送的字節數清0
return 1;
}
}
return 1;
}
memcpy(SPI_SendBuffer,buf,len);
SPI_SendNum = len;
void WriteData(SPI_SendBuffer,channelA, buf,len);
ES = 1;
SPI_SendOk=1;
return 0; //modify
}
//-----------------------------串口發送程序-------------------------------//
unsigned char UARTSendComm(unsigned char *buf,unsigned char len)
{
unsigned int timeout=0;
if (len > UART_SENDPACK_SIZE)
return -1;
if (UART_SendOk != 0)
{
while (UART_SendOk != 0)
{
timeout++;
if (timeout >= 3000)
{
UART_SendOk = 0;
UART_SendNum = 0;
return 1;
}
}
return 1;
}
memcpy(UART_SendBuffer,buf,len);
UART_SendNum = len;
S2BUF=UART_SendBuffer[0];
ES = 1;
UART_SendOk=1;
return 0; //modify
}
/**************************************************************************************
* 名稱:GetCommData
* 說明:判斷液晶接收數據的正確性
* 功能:7eH 外部與MCU通訊;feH液晶與MCU通訊;My_AddrH外部通訊(0X20之前,外部與液晶通訊;之后,外部與MCU通訊)
* 調用:
* 輸入: UART2_ReceiveBuffer,SPI_ReceiveBuffer
* 返回值:7eH返回值為2;feH返回值1;My_Addr返回值0
* ************************************************************************************/
unsigned char GetCommData(unsigned char *len)
{
unsigned char comm;
unsigned char length[2];
unsigned char tempaddr;
unsigned char ret;
unsigned char receivecount; //接收的字節數
unsigned char receivebuffer[]; //接收到的數據
ret = -1;
*len = 0;
if (receivecount >= 8) //接收的字節數
{
tempaddr = receivebuffer[0];
if ( (tempaddr != My_Addr) && (tempaddr != 0x7e) && (tempaddr != 0xfe) )
return ret;
comm = receivebuffer[1];
switch (comm)
{
case REGISTERWRITE:
*(unsigned int*)(length+0) = 9 + (*(unsigned int*)( receivebuffer+4) *2);//接收到數據字節數
if (*(unsigned int*)(length+0) < MAXREADLENGTH )//接收到的字節數小于最大字節長度
if (CrcCmp16( receivebuffer,length[1]) ==0)//CRC校驗
{
if (tempaddr == 0x7e)
ret = 2;
if (tempaddr == 0xfe)
ret = 1;
if (tempaddr == My_Addr)
ret = 0;
*len = length[1];
return ret;
}
break;
case REGISTERREAD:
case COMMTEST:
case REGISTERWRITEONBYTE:
if (CrcCmp16( receivebuffer,8) ==0) //接受到字節數是8個,CRC校驗
{
if (tempaddr == 0x7e)
ret = 2;
if (tempaddr == 0xfe)
ret = 1;
if (tempaddr == My_Addr)
ret = 0;
*len = 0x08;
return ret;
}
break;
}
}
return ret;
}
/*************************************************************************************
*名稱:SendDataToMCU
*說明:液晶把接收到的串口2數據發給MCU;
*功能:把調節器地址替換成7e,重新進行CRC校驗,發送給MCU
*調用:
*輸入:UART2_ReceiveBuffer
*輸出
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -