Beispiel #1
0
QSerial::QSerial(QObject * p_)
{
    m_nFdModbus = -1;
    m_pCurCmd = NULL;
    InitModbus();
    m_nTimer = startTimer(10); //开启定时器,1ms一次
}
Beispiel #2
0
uint8_t ModbusInput(uint8_t *pIn, uint32_t inSize, uint8_t * g_modbusReceiveBuf, uint8_t * g_modbusSendBuf, int16u* g_modbusReceiveBufNumber, uint16_t * g_modbusSendBufNumber, uint8_t type, uint16_t * longth, uint16_t * address)
{
	uint16_t i = 0;
	uint16_t receiveSize = 0;
	uint8_t  tempbuf[4] = {0};
	
	while (receiveSize < inSize) 
	{
		if (*g_modbusReceiveBufNumber >= MODBUS_BUFFER_SIZE)
			InitModbus(type);
		g_modbusReceiveBuf[*g_modbusReceiveBufNumber] = *((pIn) + receiveSize);
		
		(*g_modbusReceiveBufNumber)++;
			receiveSize++;
		
		if (g_modbusReceiveBuf[0] == 0x03) 
		{//read
			if (*g_modbusReceiveBufNumber > 4) 
			{
				*address	= g_modbusReceiveBuf[2] + ((uint16_t)(g_modbusReceiveBuf[1]) << 8);
				*longth		= g_modbusReceiveBuf[4] + ((uint16_t)(g_modbusReceiveBuf[3]) << 8);
				
				if (type == 1) 
				{
					if (*g_modbusReceiveBufNumber > 6) 
					{
						if (usMBCRC16(&g_modbusReceiveBuf[0] - 1, 8)) 
						{
							InitModbus(type);
							ModbusReportError(0x3, type);
							continue;
						}
					}
					else
						continue;
				}
				if ((((*address + *longth) << 1) > MODBUS_REG4_SIZE) || (*longth > 125)) 
				{
					InitModbus(type);
					ModbusReportError(0x3, type);
				}
				else 
				{
					if (type == 1) 
					{
						g_modbusSendBuf++;
					}
					g_modbusSendBuf[0] = 0x03;
					g_modbusSendBuf[1] = *longth << 1;
					memcpy(g_modbusSendBuf+2, g_modbusReg4+(*address << 1), (*longth << 1));
					
					tempbuf[0] = g_modbusSendBuf[2]; 
					tempbuf[1] = g_modbusSendBuf[3]; 
					tempbuf[2] = g_modbusSendBuf[4]; 
					tempbuf[3] = g_modbusSendBuf[5]; 					
					
					g_modbusSendBuf[2] = tempbuf[1]; 
					g_modbusSendBuf[3] = tempbuf[0]; 
					
					g_modbusSendBuf[4] = tempbuf[3];
					g_modbusSendBuf[5] = tempbuf[2]; 
					
					*g_modbusSendBufNumber = (*longth << 1) + 2;
					
//					ModbusSendData(type);
					TcpSendData(g_modbusSendBuf, *g_modbusSendBufNumber);
//					InitModbus(type);
					InitModbusTcp();
				}
			}
			continue;
		}


		// 最多接收5个
		if (g_modbusReceiveBuf[0] == 0x06) 
		{//write single
			if (*g_modbusReceiveBufNumber > 4) 
			{
				if (type == 1) 
				{
					if (*g_modbusReceiveBufNumber > 6) 
					{
						if (usMBCRC16(g_modbusReceiveBuf - 1, 8)) 
						{
							InitModbus(type);
							ModbusReportError(0x6, type);
							continue;
						}
					}
					else
						continue;
				}
				
				*address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8);
				if ((*address << 1) >= MODBUS_REG4_SIZE) 
				{
					InitModbus(type);
					ModbusReportError(0x6, type);
				}
				else 
				{
					if (type == 1) 
					{
						g_modbusSendBuf++;
					}
					// 更新寄存器数据,MODBUS 寄存器显示的是高位在后 需要逆转一下
					g_modbusReg4[((*address) << 1)] 		= g_modbusReceiveBuf[4];
					g_modbusReg4[((*address) << 1) + 1] = g_modbusReceiveBuf[3];	

					TcpSendData(g_modbusReceiveBuf, 5);
//					InitModbus(type);	
					InitModbusTcp();
				}
			}
			continue;
		}
		
		// 写多个寄存器
		if (g_modbusReceiveBuf[0] == 0x10) 
		{//write mul
			if (*g_modbusReceiveBufNumber > 4 && *longth == 0)
			{
				*address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8);
				*longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8);
				if (((*address + *longth) << 1) > MODBUS_REG4_SIZE || *longth > 0x7d) 
				{
					InitModbus(type);
					ModbusReportError(0x10, type);
				}
			}
			else 
			{
				if (*g_modbusReceiveBufNumber >= ((*longth << 1) + 6)) 
				{
					if (type == 1) {
						if (*g_modbusReceiveBufNumber >= ((*longth << 1) + 8)) 
						{
							if (usMBCRC16(g_modbusReceiveBuf - 1, *g_modbusReceiveBufNumber + 3)) 
							{
								InitModbus(type);
								ModbusReportError(0x10, type);
								continue;
							}
						}
						else
							continue;
					}
					tempbuf[0] = g_modbusReceiveBuf[6]; 
					tempbuf[1] = g_modbusReceiveBuf[7]; 
					tempbuf[2] = g_modbusReceiveBuf[8]; 
					tempbuf[3] = g_modbusReceiveBuf[9]; 

					
					g_modbusReceiveBuf[7] = tempbuf[0];
					g_modbusReceiveBuf[6] = tempbuf[1];
					
					g_modbusReceiveBuf[9] = tempbuf[2];
					g_modbusReceiveBuf[8] = tempbuf[3];					
					
					for(i = 0; i < (*longth << 1); i++) 
					{
						g_modbusReg4[(*address << 1) + i] = g_modbusReceiveBuf[6 + i];
					}
					if (type == 1) 
						g_modbusSendBuf++;
					

					TcpSendData(g_modbusReceiveBuf, 5);
					InitModbusTcp();		
//					InitModbus(type);
				}
			}
			continue;
		}
		
		// 扩展指令
		if (g_modbusReceiveBuf[0] == 0x41) 
		{
			if (*g_modbusReceiveBufNumber > 4) 
			{
				*address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8);
				*longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8);
				//sprintf(tempbuf, "%d %d %d %d\r\n", address, longth, ((int16u)(g_modbusReceiveBuf[1]) << 8), ((int16u)(g_modbusReceiveBuf[3]) << 8));
				//DebugLog(tempbuf);
				if (((*address + *longth) << 1) > MODBUS_REG5_SIZE || *longth > 0x7d) {
					InitModbus(type);
					ModbusReportError(0x41, type);
				}
				else {
					if (type == 1) {
						g_modbusSendBuf++;
					}
					g_modbusSendBuf[0] = 0x41;
					g_modbusSendBuf[1] = *longth << 1;
					for(i = 0; i < (*longth << 1); i++) {
						g_modbusSendBuf[i + 2] = g_modbusReg5[(*address << 1) + i];
					}
					*g_modbusSendBufNumber = (*longth << 1) + 2;
					ModbusSendData(type);
					InitModbus(type);
				}
			}
			continue;
		}
		
		
		if (g_modbusReceiveBuf[0] == 0x42) 
		{
			if (*g_modbusReceiveBufNumber > 4 && *longth == 0) 
			{
				*address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8);
				*longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8);
				
				if (((*address + *longth) << 1) > MODBUS_REG5_SIZE || *longth > 0x7d) {
					InitModbus(type);
					
					ModbusReportError(0x42, type);
				}
			}
			else if (*g_modbusReceiveBufNumber == ((*longth << 1) + 6)) {
				for(i = 0; i < (*longth << 1); i++) {
					g_modbusReg5[(*address << 1) + i] = g_modbusReceiveBuf[6 + i];
				}
				if (type == 1) {
					g_modbusSendBuf++;
				}
				g_modbusSendBuf[0] = 0x42;
				g_modbusSendBuf[1] = g_modbusReceiveBuf[1];
				g_modbusSendBuf[2] = g_modbusReceiveBuf[2];
				g_modbusSendBuf[3] = g_modbusReceiveBuf[3];
				g_modbusSendBuf[4] = g_modbusReceiveBuf[4];
				*g_modbusSendBufNumber = 5;
				ModbusSendData(type);
				InitModbus(type);
			}
			continue;
		}
		if (g_modbusReceiveBuf[0] == 0x43) 
		{
			if (*g_modbusReceiveBufNumber > 4) {
				*address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8);
				*longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8);
				if (((*address + *longth) << 1) > MODBUS_REG6_SIZE || *longth > 0x7d) {
					InitModbus(type);
					ModbusReportError(0x43, type);
				}
				else {
					if (type == 1) {
						g_modbusSendBuf++;
					}
					g_modbusSendBuf[0] = 0x43;
					g_modbusSendBuf[1] = *longth << 1;
					for(i = 0; i < (*longth << 1); i++) {
						g_modbusSendBuf[i + 2] = g_modbusReg6[(*address << 1) + i];
					}
					*g_modbusSendBufNumber = (*longth << 1) + 2;
					ModbusSendData(type);
					InitModbus(type);
				}
			}
			continue;
		}
		if (g_modbusReceiveBuf[0] == 0x44) 
		{
			if (*g_modbusReceiveBufNumber > 4 && *longth == 0) 
			{
				*address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8);
				*longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8);
				if (((*address + *longth) << 1) > MODBUS_REG6_SIZE || *longth > 0x7d) {
					InitModbus(type);
					ModbusReportError(0x44, type);
				}
			}
			else if (*g_modbusReceiveBufNumber == ((*longth << 1) + 6)) 
			{
				for(i = 0; i < (*longth << 1); i++) 
				{
					g_modbusReg6[(*address << 1) + i] = g_modbusReceiveBuf[6 + i];
				}
				if (type == 1) 
				{
					g_modbusSendBuf++;
				}
				g_modbusSendBuf[0] = 0x44;
				g_modbusSendBuf[1] = g_modbusReceiveBuf[1];
				g_modbusSendBuf[2] = g_modbusReceiveBuf[2];
				g_modbusSendBuf[3] = g_modbusReceiveBuf[3];
				g_modbusSendBuf[4] = g_modbusReceiveBuf[4];
				*g_modbusSendBufNumber = 5;
				ModbusSendData(type);
				InitModbus(type);
			}
			continue;
		}
		
		
		ModbusReportError(g_modbusReceiveBuf[0], type);
		InitModbus(type);
		continue;
	}
	return MODBUS_OK;
}