?? send.cpp
字號:
?
+
#include <windows.h>
#include <stdio.h>//文件操作,訪問I/O全靠它了
#include <conio.h>//控制臺I/O, getch()之類的
#include <mmsystem.h>
//多媒體的,我的程序中用到了多媒體定時器,一般windows定
//時器,精度差。而多媒體定時器精度高,可精確到1mS.所以跟硬件打交道的時候就很
//有用了。
//下面的程序需要在project-->setting-->link中設置一下連接的庫,否則compile能通
//過,link通不過也白搭。庫文件列表如下
//kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
//shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
//kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
//shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
//winmm.lib
//下面可以看我的程序了,這個程序是通過多媒體定時器每1mS,從串行口以115.2kbps的
//波特率向外播放一個wave文件,弄這個干嗎?我的板子可以放歌了。呵呵。
#define TIME_INTERVAL 1//定時間隔,mS單位
static UINT MyTimerID;//訪問定時器的時候所用的定時器id
static HANDLE hComm,hWaveFile;//串口和wave文件句柄
static DCB Current_Comm_dcb = {0};//串口參數(shù)結構體,波特率,流量控制,校驗
//什么的
static DCB Previous_Comm_dcb = {0};//同上
static DWORD FileSize,rFileSize,PlayPosition;//一些變量,文件大小,播放指針
static BYTE *FileBuffer;//文件緩沖區(qū)的指針,用malloc申請來的內存給他
static OVERLAPPED osWrite = {0};//對問建交疊操作所必需的一個結構體。
//交疊操作是指,用一個函數(shù)觸發(fā)一下操作,然后函數(shù)立刻返回了,這時候你可以干別的
//操作完成后會給信號的。98不支持的,2000和nt才支持。
BOOL WriteABuffer(BYTE * lpBuf, DWORD dwToWrite)//將lpBuf所只想的緩沖區(qū)中
//dwToWrite個字節(jié)通過串口發(fā)出。
{
static DWORD dwWritten;
static BOOL fRes;
// Issue write.
if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
if (GetLastError() != ERROR_IO_PENDING) {
// WriteFile failed, but it isn't delayed. Report error and abort.
fRes = FALSE;
}
else {
// Write is pending.
if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
fRes = FALSE;
else
// Write operation completed successfully.
fRes = TRUE;
}
}
else
// WriteFile completed immediately.
fRes = TRUE;
// CloseHandle(osWrite.hEvent);
return fRes;
}
void WINAPI TimerEventProc(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
//這個函數(shù)是定時器的回掉函數(shù),定時事件發(fā)生后,它自動被調用。
//我在這個函數(shù)里發(fā)送8個字節(jié)。
{
static long delay=2000,j;
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
WriteABuffer(&((BYTE)FileBuffer[PlayPosition]),1);
PlayPosition++;
// for(j=0;j<delay;j++);
}
BOOL InitializeTimer(UINT *TimerID,UINT TimeInterval)
//初始化定時器
{
TIMECAPS tc;
if(timeGetDevCaps(&tc, sizeof(TIMECAPS))==TIMERR_NOERROR)
{
/*if(TIME_INTERVAL>=minforeach (foreach (function ()
{
}$array as $key => $value) {
}$array as $value) {
}(max(tc.wPeriodMin,1),tc.wPeriodMax))*/
if(TimeInterval>=min(max(tc.wPeriodMin,1),tc.wPeriodMax))
{
timeBeginPeriod(TIME_INTERVAL);
printf("1\n");
getch();
}
else
{
return(FALSE);
}
}
else
{
return(FALSE);
}
if(!((*TimerID)=timeSetEvent(TIME_INTERVAL,TIME_INTERVAL,(LPTIMECALLBACK)
TimerEventProc,(DWORD)1,TIME_PERIODIC)))
{
printf("2\n");
getch();
return(FALSE);
}
else
{
printf("3\n");
return(TRUE);
}
}
void FreeTimer(UINT *TimerID, UINT TimeInterval)
//釋放定時器,如果忘了釋放,嘿嘿。。
{
timeKillEvent((*TimerID));
timeEndPeriod(TimeInterval);
}
void fbc(void)
//普通二進制轉折疊二進制編碼,這是pcm編碼標準必須的
//對大家沒什么用
{
DWORD j;
int i;
BYTE rom[256];
for(i=0;i<128;i++)
rom[i]=127-i;
for(;i<256;i++)
rom[i]=rom[255-i]+128;
for(j=0;j<FileSize;j++)
{
//FileBuffer[j]=FileBuffer[j]/2;
FileBuffer[j] = rom[FileBuffer[j]];
}
}
int main(int argc,char *argv[])
//主函數(shù)開始了,睜大眼睛。
{
printf("Writen by jxj.2001.5. All rights reserved\n\n");
//版權。
if(argc<2)
{
printf("Require wave_File name!\n");
getch();
exit(1);
}
//wave文件名從命令行給
PlayPosition=0;//播放指針回零
printf("Open serial communication port ... ");
//下面是怎么打開串口
hComm = CreateFile( "com1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,//這個參數(shù)表明是交疊方式打開
0);
if (hComm == INVALID_HANDLE_VALUE)
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
FillMemory(&Previous_Comm_dcb, sizeof(Previous_Comm_dcb), 0);
FillMemory(&Current_Comm_dcb, sizeof(Current_Comm_dcb), 0);
Previous_Comm_dcb.DCBlength = sizeof(Previous_Comm_dcb);
Current_Comm_dcb.DCBlength = sizeof(Current_Comm_dcb);
printf("Get previous CommState ... ");
//得到當前串口設置
if (!GetCommState(hComm, &Previous_Comm_dcb))
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
printf("Set current CommState ... ");
//寫一個你需要的串口設置的結構
Current_Comm_dcb = Previous_Comm_dcb;
Current_Comm_dcb.BaudRate = 115200;
Current_Comm_dcb.fBinary = TRUE;
Current_Comm_dcb.fParity = TRUE;
Current_Comm_dcb.StopBits = ONESTOPBIT;
Current_Comm_dcb.fDtrControl = DTR_CONTROL_DISABLE;
Current_Comm_dcb.fRtsControl = RTS_CONTROL_DISABLE;
Current_Comm_dcb.ByteSize = 8;
Current_Comm_dcb.Parity = ODDPARITY;
Current_Comm_dcb.fOutX = FALSE;
Current_Comm_dcb.fInX = FALSE;
//設置進入串口
if (!SetCommState(hComm, &Current_Comm_dcb))
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
printf("Open WaveFile ... ");
//打開普通的wave文件
hWaveFile = CreateFile( argv[1],
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hWaveFile == INVALID_HANDLE_VALUE)
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
printf("Get file size ...");
//獲得文件大小,以便確定申請多大內存
if (!(FileSize=GetFileSize(hWaveFile,NULL)))
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("%ld successful!\n",FileSize);
}
printf("Apply memory ... ");
//申請內存
if (!(FileBuffer = (BYTE *)malloc(FileSize)))
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
printf("Fill the FileBuffer with WaveFile ... ");
//文件放入內存
if(!ReadFile(hWaveFile,FileBuffer,FileSize,&rFileSize,NULL))
{
printf("error!\n");
getch();
return(1);
}
else
{
if(FileSize!=rFileSize)
{
printf("error!\n");
getch();
return(1);
}
printf("successful!\n");
}
fbc();
printf("Initialize write ... ");
//準備以交疊的方式寫串口
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
//結構初始化一下
if (osWrite.hEvent == NULL)
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
printf("Initialize Timer ... ");
//定時器初始化
if (!InitializeTimer(&MyTimerID,TIME_INTERVAL))
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
while(PlayPosition<FileSize);
//等待播放完畢
FreeTimer(&MyTimerID,TIME_INTERVAL);
printf("Recover the previous CommState ... ");
if (!SetCommState(hComm, &Previous_Comm_dcb))
{
printf("error!\n");
getch();
return(1);
}
else
{
printf("successful!\n");
}
CloseHandle(osWrite.hEvent);
CloseHandle(hComm);
CloseHandle(hWaveFile);
return(0);
}
//我的板子上用的FLEX10K10LC84-4 FPGA芯片來通過MAX232電平轉換接受,F(xiàn)PGA當中通信
//用的UART是從QUICKLOGIC公司下載的免費IP核。
//當然還有一個碼速調整問題,因為115.2kbps最高實現(xiàn)8kHz,8bit的音頻,定時精度達不到
//8k,只能到1k,所以1mS發(fā)送8個字節(jié),這8個是連在一起的.在fpga內部將它們在1mS之內
//散開。實現(xiàn)均勻的8k.
//8k 8bit自然是很不爽,后來用并口實現(xiàn)了48k 8bit,可以忍受了。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -