線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1225
推到 Plurk!
推到 Facebook!

test

尚未結案
hsun_780116
一般會員


發表:3
回覆:0
積分:0
註冊:2009-09-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-09-11 18:52:32 IP:219.85.xxx.xxx 訂閱

[code delphi]
請在此區域輸入
/* tab:4
*
*
* "Copyright (c) 2000-2002 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/* tab:4
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
* downloading, copying, installing or using the software you agree to
* this license. If you do not agree to this license, do not download,
* install, copy or use the software.
*
* Intel Open Source License
*
* Copyright (c) 2002 Intel Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
/*
*
* Authors: Jason Hill, David Gay, Philip Levis
* Date last modified: 6/25/02
*
*/

// Message format

enum {
TOS_BCAST_ADDR = 0xffff,
TOS_UART_ADDR = 0x007e,
};

#ifndef DEF_TOS_AM_GROUP
#define DEF_TOS_AM_GROUP 0x7d
#endif

enum {
TOS_DEFAULT_AM_GROUP = DEF_TOS_AM_GROUP
};

unsigned __int8 TOS_AM_GROUP = TOS_DEFAULT_AM_GROUP;

#ifndef TOSH_DATA_LENGTH
#define TOSH_DATA_LENGTH 29
#endif

typedef struct TOS_Msg
{
/* The following fields are transmitted/received on the radio. */
unsigned __int16 addr;
unsigned __int8 type;
unsigned __int8 group;
unsigned __int8 length;
__int8 data[TOSH_DATA_LENGTH];
unsigned __int16 crc;

/* The following fields are not actually transmitted or received
* on the radio! They are used for internal accounting only.
* The reason they are in this structure is that the AM interface
* requires them to be part of the TOS_Msg that is passed to
* send/receive operations.
*/
unsigned __int16 strength;
unsigned __int8 ack;
unsigned __int16 time;
unsigned __int16 toneTime;
} TOS_Msg;

enum {
MSG_DATA_SIZE = offsetof(struct TOS_Msg, crc) sizeof(unsigned __int16), // 36 by default
DATA_LENGTH = TOSH_DATA_LENGTH,
LENGTH_BYTE_NUMBER = offsetof(struct TOS_Msg, length) 1
};

typedef TOS_Msg *TOS_MsgPtr;

unsigned __int8 TOS_MsgLength(unsigned __int8 type)
{
#if 0
uint8_t i;

for (i = 0; i < MSGLEN_TABLE_SIZE; i )
if (msgTable[i].handler == type)
return msgTable[i].length;
#endif

return offsetof(TOS_Msg, strength);
}

[/code]
test
test
test

[code delphi]
請在此區域輸入程式碼
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"
#include "AM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;

HANDLE hComm;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
Output_ListBox->Items->Clear();
Output_ListBox->Font->Color = clBlack;
////////////////////////////////////////////////////// open

DCB dcb;


////設定 buffer 大小/////////
SetupComm(
hComm, // handle of communications device
1024, // size of input buffer
1024 // size of output buffer
);
//////////////////////////////////////
////設定time out//////////////
COMMTIMEOUTS pCOMMTIMEOUTS;
GetCommTimeouts( hComm,&pCOMMTIMEOUTS); //抓取預設值
pCOMMTIMEOUTS.ReadIntervalTimeout=200;
//GetOverlappedResult 沒抓到要求數目資料的等待時間
pCOMMTIMEOUTS.ReadTotalTimeoutMultiplier=500;
pCOMMTIMEOUTS.ReadTotalTimeoutConstant = 1000;
//GetOverlappedResult 抓不到資料會等待的時間
pCOMMTIMEOUTS.WriteTotalTimeoutConstant =1000;
pCOMMTIMEOUTS.WriteTotalTimeoutMultiplier =500;
SetCommTimeouts( hComm, &pCOMMTIMEOUTS); //把值設定進去
//////////////////////////////
////清空一些com 的東西//////
PurgeComm(hComm,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
////////////////////////////
//////////////////////////////////////
hComm = CreateFile(
ComboBox1->Text.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

if (hComm == INVALID_HANDLE_VALUE) //如果COM 未開啟
{
MessageBox(NULL, "開啟通訊埠錯誤!!","ERROR",MB_OK);
CloseHandle(hComm);
return;
}

//將dcb位址傳入,以取得通訊參數
GetCommState(hComm,&dcb); //得知目前COM 的狀態

dcb.BaudRate = CBR_38400; //設定鮑率為19200

dcb.DCBlength=sizeof(dcb);

dcb.Parity = NOPARITY; //Parity 為 None

dcb.ByteSize = 8; //位元組為 8 bit

dcb.StopBits = ONESTOPBIT; //1 個Stop bit

//通訊埠設定
if (!SetCommState(hComm, &dcb)) //設定COM 的狀態
{
MessageBox (NULL, "通訊埠設定錯誤!!","ERROR",MB_OK);
CloseHandle(hComm);
return;
}
/////////////////////////////////////////////
Output_ListBox->Items->Strings[0]= "感 測 中.......";
Value_Timer->Enabled=TRUE;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Value_Timer->Enabled=FALSE;
////////////////////////////////// close
if (hComm!=INVALID_HANDLE_VALUE)
CloseHandle(hComm);
//////////////////////////////////
Output_ListBox->Items->Clear();
Output_ListBox->Items->Strings[0]= "停 止 動 作";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Value_TimerTimer(TObject *Sender)
{
//////////////////////////////////////////////////////////////////////////////////////////

AnsiString buf;
buf=IntToStr(wValueNum);

//Output_ListBox->Items->Strings[wValueNum]= "第" IntToStr(wValueNum 1) "個sensor溫度=" x;



///////////////////////////////////////////////////////////////////////////////////////////
TOS_Msg *inbuff;

String temp;
DWORD nBytesRead=10, dwEvent, dwError;
DWORD read_bytes;
read_bytes=100;
COMSTAT cs;
COMMTIMEOUTS time_out ;
time_out.ReadIntervalTimeout = MAXDWORD ;
SetCommTimeouts( hComm , &time_out ) ;

ClearCommError(hComm,&dwError,&cs);
// 資料是否大於我們所準備的Buffer

if (read_bytes > sizeof(inbuff->data))
{
PurgeComm(hComm, PURGE_RXCLEAR); // 清除COM 資料
return;
}
ReadFile(hComm, inbuff->data,read_bytes,&nBytesRead,NULL);
// 接收COM 的資料
//inbuff[wValueNum]= '\0';

//Memo1->Text =Memo1->Text "第" IntToStr(wValueNum 1) "次感測溫度:" IntToStr(inbuff->data) "\n" ;
Memo1->Lines->Add(" " IntToStr(inbuff->data) );
wValueNum=wValueNum 1;



}
//---------------------------------------------------------------------------



[/code]

------
123
系統時間:2024-03-29 22:48:50
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!