HITEA 版 (精华区)

发信人: hfl (凤凰·风中轻舞), 信区: HITEA
标  题: 一个USB的程序源码
发信站: 哈工大紫丁香 (2002年04月07日16:36:42 星期天), 站内信件

这个是HW9911 USB接口模块的程序代码。
具体功能是:重复产生0x61到0x80的十六进制数,通过USB接口
写入板上的RAM,在将RAM中的数据通过USB接口读回PC。
最后将几个字符串通过USB传到板上并转换成为RS232信号
发送给PC接收,串口波特率为9600,用超级终端可以监测到
字符串内容。

HW9911.h:

define USC unsigned char
#define FALSE 0
#define TRUE 1
#define hReadPipe1 0  //endpoint 1, IN
#define hReadPipe2 2  //endpoint 3, IN
#define hReadPipe3 4  //endpoint 5, IN
#define hWritePipe1 1  //endpoint 2, OUT
#define hWritePipe2 3  //endpoint 4, OUT
#define hWritePipe3 5  //endpoint 6, OUT

test9911.cpp
///////////////////////////////////////////////////////////////
// HW9911 evaluation board software Rev. 1.0
// program by Liu Ding, Bei Jing HEAD Elec.
// this is the main program for the HW9911 USB device evaluation board
// it illustrate the basical read/write process of HW9911
// by three examples:
// 1. write datas to ram on the evaluation board
// 2. read the datas from the ram on the board
// 3. send datas through RS-232 serial prot on the board
//
// the whole project are build with MSVC 6.0
// and tested on windows 98 platform.
//
// notes: you can use hyper terminal to reaceive datas form RS232 port
//
// this software are designed by Liu Ding
// if you meet any problems, pls contact by email:
// hugehard@263.net
// or contact HEAD Co. by Tel 86-10-87312497
// or by fax: 86-10-87312495
///////////////////////////////////////////////////////////////
#include "hwdll.h"
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <memory.h>
#include <DEVIOCTL.H>
#include <stdlib.h>
#include <conio.h>
#include "hw9911.h"
#include "test9911.h"
int main()
{
 int hOpen=FALSE;
 int hCommandLength=32;
 int hDataLength=64;
 int i,VertAddr=0,HorAddr=0;
 int addr,TotalLen;
 USC StAddrH=0,StAddrL=0;
 USC DataLenH,DataLenL;
 USC *hDataBuffer,*hCommandBuffer;
 USC ch1=0x61;
 HANDLE hDeviceHandle;

 printf("************************************************\n");
 printf("*******HW9911 USB DEVELOPMENT BOARD*************\n");
 printf("************************************************\n\n");
 printf("now press any key to open board\n");
 getch();
/********* call hDeviceOpen in hwdll.dll to open the board***/
//hOpenDevice 说明:
// BOOL hOpenDevice(HANDLE *DeviceHandle)
// 作用:打开设备
// 参数说明:
// DeviceHandle:设备句柄
// 返回值:
// 设备打开成功返回 TRUE,失败则返回FALSE
 if( (hOpen = hOpenDevice( &hDeviceHandle ))==FALSE)
 {
  printf("can't open device\n");
  printf("press any key to exit\n");
  getch();
  return 0;
 }
 else
  printf("\nCongratulations! device opened!\n\n");
/******  build and send command **********/
 printf("how many bytes do you want to access?");
 scanf("%x",&TotalLen);
 while(TotalLen > 0x7fff)
 {
  printf("pls input a hex data less than 08000\n");
  scanf("%x",&TotalLen);
 }
 DataLenH=(USC)(TotalLen/0x100);  //data length high bytes
 DataLenL=(USC)(TotalLen%0x100);  //data length low bytes
 printf("************************************************\n");
 printf("now program will write datas to ram on board\n");
 printf("total bytes of datas is %x\n",TotalLen);
 printf("press any key to continue\n\n");
 getch();
/*****************************************************************
      now will build a write command packet
        with the first byte is 'w'.
  and the following bytes are:
  start ram address byte high, start ram address byte low,
  datalength byte high, data length byte low.
  this packet will send to device through WritePipe1,
  which is endpoint 2 of HW9911.
  when the device receive this command packet,
  it will write TotalLen bytes data to Ram on the board
******************************************************************/
 hCommandBuffer=(USC *)malloc(hCommandLength);
 hDataBuffer=(USC *)malloc(hDataLength);
 hCommandBuffer[0] = 'w'; //read command
 hCommandBuffer[1] = StAddrH;
 hCommandBuffer[2] = StAddrL;
 hCommandBuffer[3] = DataLenH;
 hCommandBuffer[4] = DataLenL;
/* call hUSBIO() in hwdll.dll to send command packet to device*/
// hUSBIO 说明:
// BOOL hOpenDevice(HANDLE *DeviceHandle,
//     unsigned char *IOBuffer,
//     int BufferLength,
//     int PipeNumber,
//     int Action)
// 作用:设备读写
// 参数说明:
// DeviceHandle:设备句柄
// IOBuffer: 指向要传送的数据指针
// BufferLength: 数据包长度
// 对于发送缓存1、2和接收缓存1、2,BufferLength必须小于或等于32
// 对于发送缓存3和接收缓存3,BufferLength必须小于或等于64
// PipeNumber: 通道号
//  发送缓存1:PipeNumber=0;
//  接收缓存1:PipeNumber=1;
//  发送缓存3:PipeNumber=2;
//  接收缓存3:PipeNumber=3;
// Action: 读写标志。TRUE代表从设备读数据到主机
//      FALSE代表从主机发送数据到设备
//  操作接收缓存,Action必须为FALSE
//  操作发送缓存,Action必须为TRUE
// 返回值:
// 操作成功返回TRUE,失败返回FALSE
 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   hCommandLength,
   hWritePipe1,
   FALSE) == TRUE)
 {
  printf("command 'w' writed to board\n");
  printf("now press any key to write datas to ram\n");
  getch();
 }
 else
  printf("data can't write to device\n");
/******** write ram data to board **********/
 printf("      ");
 for(VertAddr=0;VertAddr<=0xf;VertAddr++)
  printf("%02x ",VertAddr);
  printf("\n");
 addr=0;
 while (addr<TotalLen)
 {
  if (addr+hDataLength > TotalLen)
   hDataLength = TotalLen-addr;
  for(i=0;i<hDataLength;i++)
  {
   hDataBuffer[i]=ch1++;
   if (ch1 > 0x80)
    ch1 = 0x61;
  }
/* call hUSBIO to write datas to write pipe3, which
   is endoint 6 of HW9911*/
  if ( hUSBIO( &hDeviceHandle,
    hDataBuffer,
    hDataLength,
    hWritePipe3,
    FALSE) == TRUE)  //FALSE means Write operate
  {
   for(i=0;i<hDataLength;i++)
   {
/* print format control   */
    if(i % 16 == 0)
    {
     printf("\n%04x ",HorAddr);
     HorAddr=HorAddr+16;
    }
    printf("%02x ",(USC)hDataBuffer[i]);
   }
   addr=addr+hDataLength;
  }
  else
  {
   printf("can't write to board\n");
   exit(0);
  }
 }
 printf("\n\nTotal%x bytes write to ram OK\n\n",TotalLen);
/*****************************************************************
      now will build a read command packet
        with the first byte is 'R'.
  and the following bytes are:
  start ram address byte high, start ram address byte low,
  datalength byte high, data length byte low.
  this packet will send to device through WritePipe1,
  which is endpoint 2 of HW9911.
  when the device receive this command packet,
  it will read TotalLen bytes data from Ram on the board
******************************************************************/
 printf("************************************************\n");
 printf("now program will read datas from ram on board\n");
 printf("total bytes of datas is %x\n",TotalLen);
 printf("press any key to continue\n\n");
 getch();
 hDataLength=64;
 hCommandBuffer[0] = 'r'; //read command
 hCommandBuffer[1] = StAddrH;
 hCommandBuffer[2] = StAddrL;
 hCommandBuffer[3] = DataLenH;
 hCommandBuffer[4] = DataLenL;
/*  call hUSBIO to send packet   */
 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   hCommandLength,
   hWritePipe1,
   FALSE) == TRUE)
 {
  printf("command 'r' writed to board\n");
  printf("now press any key to read datas from ram\n\n");
  getch();
 }
 else
  printf("data can't write to device\n");
/******** read ram data form board **********/
 printf("      ");
 for(VertAddr=0;VertAddr<=0xf;VertAddr++)
  printf("%02x ",VertAddr);
  printf("\n");
 addr=0;
 HorAddr=0;
 while (addr<TotalLen)
 {
  if (addr+hDataLength > TotalLen)
   hDataLength = TotalLen-addr;
/* call hUSBIO to read datas from board  */
  if ( hUSBIO( &hDeviceHandle,
    hDataBuffer,
    hDataLength,
    hReadPipe3,
    TRUE) == TRUE)   //TRUE means Read Operate
  {
   for(i=0;i<hDataLength;i++)
   {
/*   print format control   */
    if(i % 16 == 0)
    {
     printf("\n%04x ",HorAddr);
     HorAddr=HorAddr+16;
    }
    printf("%02x ",(USC)hDataBuffer[i]);
   }
   addr=addr+hDataLength;
  }
  else
  {
   printf("can't read from board\n");
   exit(0);
  }
 }
 printf("\n\n");
 printf("%x bytes read from ram OK\n\n",TotalLen);
/*****************************************************/
/*      process rs232 test                           */
/*     the first byte in this packet is command 's'  */
/*     and the following 2 bytes are used to set     */
/*     the baud rate of 8051       */
/*     they will be write to TH1 and TL1 respectively*/
/*     the following bytes are datas will be         */
/*     send through RS232 serial poart     */
/*****************************************************/
 printf("\npress any key to process RS232 Test\n");
 getch();

 USC TH1,TL1;
 char hStr[30];
 TH1=(USC)(BD9600/0x100);
 TL1=(USC)(BD9600%0x100);
 hCommandBuffer[0] = 's'; //read command
 hCommandBuffer[1] = TH1;
 hCommandBuffer[2] = TL1; //start address is 0x0000
 strcpy(hStr, "Hi, dear HW9911 consumers! ");
 printf("Hi, dear HW9911 consumers! ");
 memcpy(&(hCommandBuffer[3]),hStr,27);
 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   30,
   hWritePipe1,
   FALSE) == FALSE)
 {
   printf("can't write datas to RS232\n");
   exit(0);
 }
 strcpy(hStr, "I'm HW9911 Evaluation Board, ");
 printf("I'm HW9911 Evaluation Board, ");
 memcpy(&(hCommandBuffer[3]),hStr,29);

 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   32,
   hWritePipe1,
   FALSE) == FALSE)
 {
   printf("can't write datas to RS232\n");
   exit(0);
 }
 strcpy(hStr, "thanks for purchasing me, ");
 printf("thanks for purchasing me, ");
 memcpy(&(hCommandBuffer[3]),hStr,26);

 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   29,
   hWritePipe1,
   FALSE) == FALSE)
 {
   printf("can't write datas to RS232\n");
   exit(0);
 }

 strcpy(hStr, "If you meet some problems, ");
 printf(hStr, "If you meet some problems, ");
 memcpy(&(hCommandBuffer[3]),hStr,27);

 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   30,
   hWritePipe1,
   FALSE) == FALSE)
 {
   printf("can't write datas to RS232\n");
   exit(0);
 }
 strcpy(hStr, "pls contact my designer ");
 printf("pls contact my designer ");
 memcpy(&(hCommandBuffer[3]),hStr,24);

 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   27,
   hWritePipe1,
   FALSE) == FALSE)
 {
   printf("can't write datas to RS232\n");
   exit(0);
 }
 strcpy(hStr, "by email: hugehard@263.net.");
 printf("by email: hugehard@263.net.\n");
 memcpy(&(hCommandBuffer[3]),hStr,27);

 if ( hUSBIO( &hDeviceHandle,
   hCommandBuffer,
   30,
   hWritePipe1,
   FALSE) == FALSE)
 {
   printf("can't write datas to RS232\n");
   exit(0);
 }
/********* call hDeviceOpen in hwdll.dll to open the board***/
// hCloseDevice 说明:
// BOOL hCloseDevice(HANDLE *DeviceHandle)
// 作用:关闭设备
// 参数说明:
// DeviceHandle:设备句柄
// 返回值:
// 设备关闭成功返回 TRUE,失败则返回FALSE
 if( (hOpen = hCloseDevice( &hDeviceHandle ))==FALSE)
 {
  printf("can't open device\n");
  printf("press any key to exit\n");
  getch();
  return 0;
 }
 else
  printf("device closed\n");
 printf("press any key to exit the program\n");
 free(hDataBuffer);
 free(hCommandBuffer);
 getch();
 return 0;
}





--

      >(')____,  >(')____,  >(')____,  >(')____,        这是凤凰嬉水
       (` =~~/    (` =~~/    (` =~~/    (` =~~/        哈哈哈
    ~^~^`---'~^~^~^`---'~^~^~^`---'~^~^~^`---'~^~^~               

※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.229.253]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:212.805毫秒