Beispiel #1
0
void  TestDeviceCode()
{

    char     filename[200] = {0};
    sprintf(filename,"/bin/udevcode");
    unsigned char devicecode[16];
    unsigned char number[4];
    if(ReadBinFile(filename, UUID_MAGIC, devicecode, 16) > 0)
    {
        DebugPrint(0, "Read UUID ok, \n uuid    :");
        PrintHexLog(0, devicecode, 16);
    }
    else
    {
        DebugPrint(0, "Read UUID  fail, ");
    }



}
Beispiel #2
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;
}