Esempio n. 1
0
/**
* @brief 读取FLAT文件数据
* @param sector 文件扇区号
* @param buf 缓存区指针
* @param len 缓存区长度
* @return 成功返回实际读取长度, 失败返回-1
*/
int ReadFlatFile(unsigned int sector, unsigned char *buf, int len)
{
	int rtn = 0;
	int rdlen = 0;
	unsigned char memcache[BUFFER_SIZE];	

	AssertLogReturn(FlatFid<0, -1, "invalid flat id\n");
	AssertLogReturn(sector >= MAX_SECTOR, -1, "invalid sector(%d)\n", sector);
	AssertLog(len<=0, "invalid len(%d:%d)\n", sector, len);
	AssertLogReturn(len > REC_DATASIZE(sector), -1, "invalid record size(%d:%d)\n", sector, REC_DATASIZE(sector));

	memset(memcache, 0x0, sizeof(memcache));
	rdlen = len + sizeof(rec_head_t);

	FLAT_LOCK;
	ioctl(FlatFid, 0, &sector); //选择写入的扇区
	rtn = read(FlatFid, (char *)memcache, rdlen); 
	FLAT_UNLOCK;

	if(1 != ValidRecord(memcache, rdlen) || rtn<4)
	{
		DebugPrint(0, "validRecord\n");
		return -1;
	}
	else
	{
		memcpy(buf, memcache+4, len);
		return rtn-4;
	}
}
Esempio n. 2
0
/**
* @brief 读ESAM
* @param    etRead  读到数据接口指针
* @return 0成功, 否则失败
*/
int ReadESAM(esam_t *etRead )
{
	int rtn;

	AssertLog(-1==FidEsam, "invalid fid(%d)\n", FidEsam);

	rtn = EsamUartRecv(etRead->buf, 100);

	etRead->len = (unsigned short)rtn;

	return 0;
}
Esempio n. 3
0
/**
* @brief ��UART�˿ڷ�������
* @param port �˿ں�, 0~4
* @param buf ���ͻ�����
* @param len ����������
* @param 0�ɹ�, ����ʧ��
*/
int UartSend(unsigned int port, const unsigned char *buf, int len)
{
	int fd;

	AssertLogReturn(port>=UART_PORTNUM, 1, "invalid port(%d)\n", port);
	fd = FidUart[port];
	//printf("UartSend port = %d fd = %d",port,fd);
	AssertLogReturn(-1 == fd, 1, "invalid fid(%d)\n", fd);
	AssertLog(len<=0, "invalid len(%d)\n", len);

	write(fd, (char *)buf, len);

	return 0;
}
Esempio n. 4
0
/**
* @brief 从文件中载入终端参数
* @return 0成功, 否则失败
*/
int LoadParaTerm(void)
{
	int rt=-1;

	DebugPrint(0, "  load param term...\n");

	rt = para_readflash_bin(PARA_FILEINDEX_COMB,(unsigned char *)&para_comb,sizeof(para_comb));
	AssertLog(rt==-1,"readflash para_term failed!\n");
	if(rt==-1)
	{
		LoadDefParaTerm();
		return 1;
	}
	
	return 0;
}
Esempio n. 5
0
int Rs485BusRead(const plc_dest_t *dest, unsigned short itemid, unsigned char *buf, int len)
{
	unsigned char *cache = GetPlCommBuffer();
	const plcmet_prot_t *pfunc;
	int applen, timeout, i;
	unsigned char port, uc;
	
	pfunc = GetPlcMetProto(1);
	if(NULL == pfunc) 
	     return -1;
	
	AssertLog(dest->metid > MAX_METER, "invalid metid(%d)\n", dest->metid);

	applen = (*pfunc->makeread)(dest->dest, itemid, cache, RS485BUS_BUF_LEN);
	if(applen <= 0) 
	     return -1;
	port = 2;
	Rs485Lock(port);
	Rs485Set(port, 1200, 8, 1, 'E');
	while(Rs485Recv(port, &uc, 1) > 0);

	PrintLog(LOGTYPE_DOWNLINK, "PLC Send(%d):\n", applen);
	Rs485Send(port, cache, applen);

	timeout = 50;
	applen = Rs485BusRecvPkt(dest, cache, RS485BUS_BUF_LEN, timeout);
	Rs485Unlock(port);
	if(applen > 0) {
		applen = (*pfunc->checkread)(dest->dest, itemid, cache, applen);
		if(applen > 0) {
			for(i=0; i<len; i++) {
				if(i < applen) *buf++ = *cache++;
				else *buf++ = 0;
			}
		}
	}

		if(!dest->metid) {
		if(applen <= 0) {
			PrintLog(LOGTYPE_DOWNLINK, "proto check error(%d)\n", applen);
			return -1;
		}
		else return applen;
	}

	return applen;
}
Esempio n. 6
0
/**
* @brief 写ESAM
* @param    etRead  写入数据接口指针
* @return 0成功, 否则失败
*/
int WriteESAM(esam_t *cmd){

	esam_t et,etRev ;
	int rtn;
	AssertLog(-1==FidEsam, "invalid fid(%d)\n", FidEsam);

	et.len = cmd->len;
	memcpy(et.buf,cmd->buf,cmd->len);

	if(et.len>5) {
		et.len = 5;

		///rtn = EsamSend(et.buf, et.len);
		if(rtn == -1) {
			ErrorLog("set esam data fail\n");
			return rtn;
		}
		
		rtn = EsamUartRecv(etRev.buf, 30);
		if(0 == rtn){
			ErrorLog("read esam data fail\n");
			return rtn;
		}

		memcpy(et.buf,cmd->buf+5,cmd->len-5);
		et.len =cmd->len - 5;

		///rtn = EsamSend(et.buf, et.len);
		if(-1 == rtn){
			ErrorLog("set esam data fail\n");
			return rtn;
		}
	}
	else{
		///rtn = EsamSend(et.buf, et.len);
		if(-1 == rtn){
			ErrorLog("set esam data fail\n");
			return rtn;
		}
	}

	return 0;
}
Esempio n. 7
0
/**
* @brief ��UART�˿ڽ�������
* @param port �˿ں�, 0~4
* @param buf ���ջ�����
* @param len ����������
* @return ʧ�ܷ���-1, �ɹ����ؽ��յ����ֽ���, ����0��ʾδ���յ�����
*/
int UartRecv(unsigned int port, unsigned char *buf, int len)
{
	int fd, rtn, i;
	unsigned char *precv;

	AssertLogReturn(port>=UART_PORTNUM, -1, "invalid port(%d)\n", port);
	fd = FidUart[port];

	if(-1 == fd)
	{
		printf("UartRecv............................................");
		printf("port = %d fd = %d",port,fd);
	}
	AssertLogReturn(-1 == fd, -1, "invalid fid(%d)\n", fd);
	AssertLog(len<=0, "invalid len(%d)\n", len);

	if(UartRecvBuffer[port].len <= 0) 
	{
		UartRecvBuffer[port].len = 0;
		UartRecvBuffer[port].head = 0;
		rtn = read(fd, UartRecvBuffer[port].buf, UART_RCVBUF_SIZE);
		if((rtn <=0) || (rtn > UART_RCVBUF_SIZE)) return 0;
		UartRecvBuffer[port].len = rtn;
	}

	if(UartRecvBuffer[port].len > len) rtn = len;
	else rtn = UartRecvBuffer[port].len;

	precv =  &UartRecvBuffer[port].buf[UartRecvBuffer[port].head];
	for(i=0; i<rtn; i++) *buf++ = *precv++;
	//memcpy(buf, &UartRecvBuffer[port].buf[UartRecvBuffer[port].head], rtn);
	UartRecvBuffer[port].len -= rtn;
	UartRecvBuffer[port].head += rtn;

	return rtn;
}
Esempio n. 8
0
/**
* @brief 接收数据帧
* @param dest 目的地址
* @param buf 接收缓存区指针
* @param len 接收缓存区长度
* @param timeout 超时时间(100ms)
* @param 成功返回接收到的数据长度, 失败返回负数(参见错误码PLCERR_XXX)
*/
int Rs485BusRecvPkt(const plc_dest_t *dest, unsigned char *buf, int len, int timeout)
{
	unsigned char state, recvlen, maxlen, cnt;
	unsigned char *pbuf = Rs485BusBuffer;
	unsigned char port;
	int times;
	AssertLog(len <= 0, "invalid len(%d)\n", len);

	state = 0;
	recvlen = 0;
	maxlen = 0;
	port = 1;
	cnt = 0;
	for(times=0; times<timeout; times++) {
		while(Rs485Recv(port, pbuf, 1) > 0) {
			PrintLog(LOGTYPE_DOWNLINK, "recv: %02X, %d\n", *pbuf, state);
			switch(state) {
			case 0:
				if(0x68 == *pbuf) {
					pbuf++;
					recvlen = 1;
					maxlen = 6;
					cnt = 0;
					state = 1;	
				}
				break;
			case 1:
				pbuf++;	
				recvlen++;
				cnt ++;
				if(cnt >= maxlen){
					state = 2;
				}	
				break;
			case 2:
				if(0x68 != *pbuf){
					pbuf = Rs485BusBuffer;
					state = 0;
					break;
				}
				pbuf++;
				recvlen++;
				state  = 3;
				break;
			case 3:
				pbuf++;
				recvlen++;
				state = 4;
				break;
			case 4:
				recvlen++;
				cnt = 0;
				maxlen = *pbuf;
				if(maxlen>128){
					pbuf = Rs485BusBuffer;
					state = 0;
					break;
				}
				pbuf++;	
				maxlen += 2;
				state  = 5;
				break;
			case 5:
				recvlen++;
				cnt++;
				if(cnt >= maxlen) {
					if(0x16 == *pbuf){
					       goto mark_rcvend;
					}
					else{
						pbuf = Rs485BusBuffer;
						state = 0;
						break;
					}
				}
				pbuf++;
			}
		}

		Sleep(10);
	}

	PrintLog(LOGTYPE_DOWNLINK, "Rs485 recv timeout(%d), time=%d00ms:\n", recvlen, times);
	if(recvlen) PrintHexLog(LOGTYPE_DOWNLINK, Rs485BusBuffer, recvlen);

	return PLCERR_TIMEOUT;

mark_rcvend:

	PrintLog(LOGTYPE_DOWNLINK, "Rs485 recv(%d), time=%d00ms:\n", recvlen, times);
	PrintHexLog(LOGTYPE_DOWNLINK, Rs485BusBuffer, recvlen);
	
	smallcpy(buf, Rs485BusBuffer, recvlen);
	
	return recvlen;
}