void CScanDlg::OpenDefaultCom()
{	
	CString strSql;
	strSql.Format(_T("select * from Building order by Main_BuildingName"));
	//_RecordsetPtr pRs;
	//_ConnectionPtr pCon;
	
	//pCon.CreateInstance("ADODB.Connection");
	//pCon->Open(g_strDatabasefilepath.GetString(),"","",adModeUnknown);
	//pRs.CreateInstance("ADODB.Recordset");

	m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);			
	_variant_t temp_variant;
	CString strDefaultCom = 0;		
	while(VARIANT_FALSE==m_pRs->EndOfFile)
	{	
		int bSel = 0;
		bSel=m_pRs->GetCollect(_T("Default_SubBuilding"));
		if(bSel==-1)//def building;
		{
			strDefaultCom=m_pRs->GetCollect(_T("Com_Port"));
		}			
	}

	if (strDefaultCom.GetLength() == 0)
	{
		return;
	}
	int nCom = _wtoi(strDefaultCom.Right(1));
	open_com(nCom);
	m_pCon->Close();
	
}
BOOL CAirQuality::prodtopc()
{
	KillTimer(AirQualityTimer);
#if 1
	//读取串口号,从数据库

	open_com(comnum);

	int sumret = 0;
	BOOL flag = FALSE;

	//读取AirQuality数据
	BOOL ret_bool = FALSE;
	long start = clock(),end(0); 		


	memset(&shmPRegister,0,sizeof(shmPRegister)); 
	for(int i=0;i<4;i++)
	{
		int ret = Read_Multi(g_tstat_id,&shmPRegister[i*64],i*64,64,2);
		sumret += ret;

		if (sumret<0)
		{
			TRACE(_T("write_multi ret sum = %d\n"),sumret);
			SetTimer(AirQualityTimer,1000,NULL);
			return FALSE;
		}

	}



	for (int i = 0;i<400;i++)
	{
		//	vecminipanel.at(i).CStvalue = shmPRegister[i];		// error C2593: 'operator =' is ambiguous
		vecairquality.at(i).CStvalue.Format(_T("%d"),shmPRegister[i]);	


	}

	if (sumret>200)
	{
		flag = TRUE;
	}
	TRACE(_T("write_multi ret sum = %d\n"),sumret);

	end = clock();
	long resultvalue = (end - start)/1000;
	TRACE(_T("GetData() usetime %d second\n"),resultvalue);


	SetTimer(AirQualityTimer,1000,NULL);
#endif
   return flag;
}
Beispiel #3
0
int
bc_open(char *path, int flags, int mode)
{
	CHKNULL(path);
	if (flags & FNDELAY) {
		flags &= ~FNDELAY;
		flags |= O_NONBLOCK;
	}
	return (open_com(path, flags, mode));
}
Beispiel #4
0
Calibrator::Calibrator()
{
	m_data = new(data_t);
	open_com(CALIBRATOR, &_(HPORT));
	write_com_cstr(_(HPORT), "C" EOS);
	LSleep(3500);
	write_com_cstr(_(HPORT), "MI" EOS);
	write_com_cstr(_(HPORT), "O0" EOS);
	m_state = false;
	m_F = 0;
	m_U = 0.L;
	printf("Creating COM1 calibrator\n");
}
bool CReg_ControlDlg::OnConnect(int comport)
{

	open_com(comport);//open*************************************
	CString strInfo;
	if(!is_connect())
	{	
		strInfo.Format(_T("COM %d : Not available "), comport);
		MessageBox(strInfo);
		return false;
	}
	else
	{
		strInfo.Format(_T("COM %d Connected: Yes"), comport);	
		MessageBox(strInfo);
		return true;
	}	
}
Beispiel #6
0
int open_com(int dev)
{
  if (dev<0)
    {
      // find serial devices and print them
      
      // get user input
      open_com(dev);
      while(1)
	{
	  if (scanf("%d", &dev)==1)
	    break;
	}
    }
  else
    {
      // open serial device
    }
  return 0;
}
BOOL CDialogT3::MiniUpdateData()
{
    KillTimer(T3_MODEL);
#if 1
    //读取串口号,从数据库
    open_com(comnum);

    int sumret = 0;
    BOOL flag = FALSE;

    //读取miniPanel数据
    BOOL ret_bool = FALSE;
    long start = clock(),end(0);


    memset(&shmPRegister,0,sizeof(shmPRegister));
    for(int i=0; i<4; i++)
    {
        int ret = Read_Multi(g_tstat_id,&shmPRegister[i*64],i*64,64,2);
        sumret += ret;

        if (sumret<0)
        {
            TRACE(_T("write_multi ret sum = %d\n"),sumret);
            SetTimer(MiniPanelTimer,1000,NULL);
            return FALSE;
        }

    }



    for (int i = 0; i<230; i++)
    {
        //	vecminipanel.at(i).CStvalue = shmPRegister[i];		// error C2593: 'operator =' is ambiguous
        vect3model.at(i).CStvalue.Format(_T("%d"),shmPRegister[i]);
        if (i>=124&&i<=147)
        {
            switch(shmPRegister[i])
            {
            case 0:
                vect3model.at(i).CStautomannel = _T("OFF");
                break;
            case 1:
                vect3model.at(i).CStautomannel = _T("Manual");
                break;
            case 2:
                vect3model.at(i).CStautomannel = _T("Auto");
                break;
            default:
                break;
            }

        }

        if (i>=100&&i<=123)
        {

            if (shmPRegister[i]>512)
                vect3model.at(i).CStvalue = _T("ON");
            else
                vect3model.at(i).CStvalue = _T("OFF");

        }
    }

    if (sumret>200)
    {
        flag = TRUE;
    }
    TRACE(_T("write_multi ret sum = %d\n"),sumret);

    end = clock();
    long resultvalue = (end - start)/1000;
    TRACE(_T("GetData() usetime %d second\n"),resultvalue);


    SetTimer(T3_MODEL,1000,NULL);
#endif
    return flag;

}
void CFlashSN::FlashTstatSN()
{
	m_nSerialNumber=GetNewSerialNumber();// get last serialnumber of the file
	if(m_nSerialNumber==-1)//can't fine the serialnumber file on z drive
	{
		AfxMessageBox(_T("WARNING : Can't find serial number file on Z driver."));
		return;
	}

	unsigned int loword,hiword;
	loword=m_nSerialNumber & 0xffff;
	hiword=(m_nSerialNumber >> 16) & 0xffff;
	if (open_com(m_nComPort))
	{if(Write_One(m_nMBID,0,loword)>0 && Write_One(m_nMBID,2,hiword)>0)
	{		
		int nSWVersion=Read_One(m_nMBID, 4);//tstat version			
		if(nSWVersion >= 240 && nSWVersion < 250)
			nSWVersion /=10;
		else 
		{
			nSWVersion = Read_One(255,5)*256+Read_One(255,4);	
				nSWVersion /=10;
		}//tstat_version		
		
		if(nSWVersion >= 25)
		{
				Write_One(255,7,m_nProductModel);//write product_model register
				Write_One(255,8,m_nHardWareVersion);//write hardware_rev register
		}
			int year,month,day;
			char software_version[8]={'\0'};
			time_t curtime;
			struct tm* today = NULL;
			curtime = time(NULL);
			//today = localtime(&curtime);
			localtime_s(today, &curtime);
			year = today->tm_year + 1900;
			month = today->tm_mon + 1;
			day = today->tm_mday;
			//		getTime(&year,&month,&day);//get the month,day,year of now							
			float fl_temp=(float)nSWVersion;
			CString f_l_temp;
			f_l_temp.Format(_T("%.1f"),fl_temp);
			for(int j=0;j<f_l_temp.GetLength();j++)
				software_version[j]=(char)f_l_temp.GetAt(j);
		
			//fstream tstat;

			////tstat.open(_T("z:\\Serial_Records\\serial_records.txt"),ios_base::out | ios_base::app);
			//tstat.open(_T("e:\\serial_records.txt"),ios_base::out | ios_base::app);
			//if(tstat.is_open())
			//{
			//	tstat	<<endl<<m_nSerialNumber                    //serialnumber
			//			<<_T(", ")<<m_strProductModel                //product_model
			//			<<_T(", ")<<m_nHardWareVersion           //hardware version
			//			<<_T(", ")<<nSWVersion				        //software version
			//			<<_T(", ")<<month<<_T("-")<<day<<_T("-")<<year;        //the time									
			//}
			//tstat.close();
			CString stringlog;
			stringlog.Format(_T("%d,%s,%d,%s,%d-%d-%d"),
				m_nSerialNumber,
				m_strProductModel,
				m_nHardWareVersion,
				f_l_temp,
				month,
				day,
				year);
			CStdioFile seriallogfile;
			if(seriallogfile.Open(c_strSNRecordFileName.GetString(),CFile::modeReadWrite | CFile::shareDenyNone))
			{

				seriallogfile.SeekToEnd();

				seriallogfile.WriteString(_T("\n")+stringlog);



				seriallogfile.Close();
			}
			else
			{
				ASSERT(0);
				seriallogfile.Close();

			}
			CString index;
			index.Format(_T("SN=%d have been written,sucessfully."),m_nSerialNumber);
			AfxMessageBox(index);
	}
	else
	{
			m_nSerialNumber = 
				Read_One(255,0 ) +
				Read_One(255,1) * 256 +
				Read_One(255,2) * 65536 +           // 256 * 256 
				Read_One(255 , 3) * 16777216 ;// 256 *256 *256
			CString index;
			index.Format(_T("SN:%d have not been overwritten."),m_nSerialNumber);
			AfxMessageBox(index);
	}
	}
	else
	{
      AfxMessageBox(_T("COM Can't Open"));
	}
	

}
Beispiel #9
0
int main(int argc, char *argv[]) {
	/* select部份 */
	int opt = TRUE;
	int master_socket, addrlen, new_socket, client_socket[MAXCLIENTS],
			max_clients = MAXCLIENTS, activity, i, valread, sd;
	user_content_t *my_contents[MAXCLIENTS + 1];
	int max_sd;
	struct timeval tv; /* select超时 */
	//set of socket descriptors
	fd_set readfds;

	/* IP部份 */
	struct addrinfo hints, *res; /* 连接到target的用到的 */
	struct sockaddr_in address;
	/* 每个客户端的缓冲区和索引 */
	char buffer[MAXCLIENTS][MAXLEN];
	char *buffer_p[MAXCLIENTS];  //data buffer of 1K
	int buffer_data_size[MAXCLIENTS];
	char itoa_buffer[8]; /* ip地址从网络顺序转成char数组 */
	char *header = NULL; /* 将转换好的ip地址:封包成header */

	/* 串口部份 固定变量 */
	static char com_devicename[] = "/dev/ttyUSB0"; /* 固定的linux串口设备文件 */
	user_content_t *my_com_conf = my_malloc(sizeof(user_content_t));/* 串口配置 */

	/* 串口部份 动态变量 */
#ifdef MODULE_SERIAL
	char buffer_com[MAXLEN]; /* 串口缓冲区 */
	char *buffer_com_p=buffer_com;
	int buffer_com_data_size=0;
#endif

	/* 蓝牙部份 */
#ifdef MODULE_BLUETOOTH
	struct sockaddr_rc blue_rem_addr = {0};
	char blue_buffer[MAXLEN],blue_sender_MAC[18];
	int blue_fd,blue_fd_client,blue_bytes_read;
	socklen_t blue_opt=sizeof(blue_rem_addr);
	user_content_t *blue_user_content;
#endif

	/* args参数 */
	char *PORT;

	if (argc != 2) {
		fprintf(stderr, "usage: %s listen-port\n", argv[0]);
		return 1;
	}

	PORT = argv[1];

#ifdef MODULE_SERIAL
	/* 打开串口,须root权限 */
	if(NULL==(my_com_conf=open_com(com_devicename))) {
		printf("error open com!\n");
		return 1;
	}
#endif

#ifdef MODULE_BLUETOOTH
	/* 打开蓝牙 */
	blue_fd=create_bluetooth_socket();
	if(blue_fd<0) {
		printf("error bluetooth fd is -1\n");
		return -1;
	}
	listen(blue_fd, 1);
#endif

	//initialise all client_socket[] to 0 so not checked
	for (i = 0; i < max_clients; i++) {
		client_socket[i] = 0;
	}

	if ((master_socket = create_server_socket("0.0.0.0", PORT)) < 0) {
		printf("error create socket fd\n");
		return 1;
	}

	//set master socket to allow multiple connections , this is just a good habit, it will work without this
	if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &opt,
			sizeof(opt)) < 0) {
		perror("setsockopt");
		exit(EXIT_FAILURE);
	}

	//try to specify maximum of 3 pending connections for the master socket
	if (listen(master_socket, 3) < 0) {
		perror("listen");
		exit(EXIT_FAILURE);
	}
	printf("Listening on port %s \n", PORT);

	//accept the incoming connection
	addrlen = sizeof(address);
	puts("Waiting for connections ...");

	while (TRUE) {
		//clear the socket set
		FD_ZERO(&readfds);
		//add master socket to set
		FD_SET(master_socket, &readfds);
		max_sd = master_socket;

#ifdef MODULE_SERIAL
		FD_SET(my_com_conf->fd,&readfds);
		max_sd = max_sd>my_com_conf->fd?max_sd:my_com_conf->fd;
#endif

#ifdef MODULE_BLUETOOTH
		FD_SET(blue_fd,&readfds);
		max_sd = max_sd>blue_fd?max_sd:blue_fd;
#endif

		//add child sockets to set
		for (i = 0; i < max_clients; i++) {
			//socket descriptor
			sd = client_socket[i];

			//if valid socket descriptor then add to read list
			if (sd > 0)
				FD_SET(sd, &readfds);

			//highest file descriptor number, need it for the select function
			if (sd > max_sd)
				max_sd = sd;
		}

		//wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
		activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);

		if ((activity < 0) && (errno != EINTR)) {
			printf("select error");
		}

		//If something happened on the master socket , then its an incoming connection
		if (FD_ISSET(master_socket, &readfds)) {
			if ((new_socket = accept(master_socket,
					(struct sockaddr *) &address, (socklen_t*) &addrlen)) < 0) {
				perror("accept");
				exit(EXIT_FAILURE);
			}

			//add new socket to array of sockets
			for (i = 0; i < max_clients; i++) {
				//if position is empty, create new one
				if (client_socket[i] == 0) {
					client_socket[i] = new_socket;
					// 初始化buffer
					buffer_p[i] = buffer[i];
					memset(buffer_p[i], 0, MAXLEN);
					buffer_data_size[i] = 0;

					printf("accepted #%d client\n", i);
					break;
				}
			}
		}

#ifdef MODULE_SERIAL
		// 串口读
		if (FD_ISSET(my_com_conf->fd, &readfds)) {
			/* 非阻塞读取 */
			valread=sp_nonblocking_read(my_com_conf->com_port,buffer_com_p+buffer_com_data_size,MAXLEN);
			if(valread<0) {
				printf("read data from com error: %d\n",valread);
				return 1;
				buffer_com_data_size=0;
				buffer_com_p=buffer_com;
			} else {
				buffer_com_data_size+=valread;
				/* 读完所有数据,串口数据包必须以\r\n结尾 */
				if(buffer_com[buffer_com_data_size-2]==13 && buffer_com[buffer_com_data_size-1]==10) {
					printf("- - - - - - - - - -\nread from COM ok\n");
					buffer_com_p[buffer_com_data_size]=0;

					my_contents[MAXCLIENTS]=new_user_content_from_str(buffer_com,com_devicename,get_direction(buffer_com));
					if(!my_contents[MAXCLIENTS]) {
						printf("invalid packet!\n");
					} else {
						printf("  %s",com_devicename);
						redirect_from_user_content(my_contents[MAXCLIENTS]);
					}
					my_free(my_contents[MAXCLIENTS]);
					/* reset buffer offset */
					buffer_com_data_size=0;
					buffer_com_p=buffer_com;
				}
			}
		}
#endif

#ifdef MODULE_BLUETOOTH
		// 蓝牙读
		if(FD_ISSET(blue_fd,&readfds)) {
			// accept one connection
			blue_fd_client = accept(blue_fd, (struct sockaddr *)&blue_rem_addr, &blue_opt);

			ba2str( &blue_rem_addr.rc_bdaddr, blue_sender_MAC );

			// read data from the client
			blue_bytes_read = read(blue_fd_client, blue_buffer, sizeof(blue_buffer));
			if( blue_bytes_read > 0 ) {
				printf("- - - - - - - - - -\nread from bluetooth ok\n");
				blue_buffer[blue_bytes_read]=0;

				blue_user_content=new_user_content_from_str(blue_buffer,blue_sender_MAC,get_direction(blue_buffer));
				if(!blue_user_content) {
					printf("invalid packet!\n");
				} else {
					printf("  %s",blue_sender_MAC);
					redirect_from_user_content(blue_user_content);
				}
				my_free(blue_user_content);
			} else {
				printf("bluetooth recv data error!\n");
			}

			// close connection
			close(blue_fd_client);
		}
#endif		

		// 局域网ip读
		for (i = 0; i < max_clients; i++) {
			sd = client_socket[i];

			if (FD_ISSET(sd, &readfds)) {
				//Check if it was for closing , and also read the incoming message
				if ((valread = read(sd, buffer_p[i] + buffer_data_size[i],
						MAXLEN)) == 0) {
					//Somebody disconnected , get his details and print
					buffer[i][buffer_data_size[i]] = 0;
					getpeername(sd, (struct sockaddr*) &address,
							(socklen_t*) &addrlen);
					printf(
							"- - - - - - - - - -\nread %d bytes from LAN client\n",
							buffer_data_size[i]);

					//Close the socket and mark as 0 in list for reuse
					close(sd);
					client_socket[i] = 0;

					/* convert port(interger) to char* */
					sprintf(itoa_buffer, "%d", ntohs(address.sin_port));
					header = get_header_ipv4(inet_ntoa(address.sin_addr),
							itoa_buffer);
					/* create relay struct: from LAN ip, to serial */
					my_contents[i] = new_user_content_from_str(buffer[i],
							header, get_direction(buffer[i]));

					if (!my_contents[i]) {
						printf("invalid packet!\n");
					} else {
						printf("  %s", header);
						redirect_from_user_content(my_contents[i]);
					}
					my_free(header);
					my_free(my_contents[i]);

				} else {
					/* 累加数据 */
					buffer_data_size[i] += valread;
				}
			}
		}
	}

#ifdef MODULE_SERIAL
	sp_close(my_com_conf->com_port);
	sp_free_port(my_com_conf->com_port);
	sp_free_config(my_com_conf->com_conf);
	my_free(my_com_conf);
#endif

#ifdef MODULE_BLUETOOTH
	close(blue_fd);
#endif

	close(master_socket);
	printf("exit..\n");
	return 0;
}