关键词不能为空

当前您在: 主页 > 英语 >

自己动手编写串口动态库(超详细)

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-08 13:36
tags:

-

2021年2月8日发(作者:海滨)


自己动手编写


串口动态库



手把手教你编写串口调试助手,单片机串口程序





一、串口库源码


:


自行在

< p>
vc6


中建立动态库工程




#include





#include





#include





#include





#include





#include



< br>/*


导出函数


*/


< p>
//


函数参数:串口名


:


波特率:


9600


用于显示接收数据的文本框的句柄



extern





_declspec


(


dllexport


)


void


Entry(


char


*comname,


int


BaudRate, HWND rhbox);


extern





_declspec


(


dllexport


)


bool


openport(


char


*portname);


extern





_declspec


(


dllexport


)


bool


setupdcb(


int


rate_arg);


extern





_declspec


(


dllexport


)


void


SetTimeOut(


int


a,


int


b,


int


c,


int


d,


int


e);


//


发送字符或


16


进制数



extern





_declspec


(


dllexport


)


void


SendChar(


unsigned



char


ch);


//

< p>
显示方式:


1



hex 0



char



extern





_declspec


(


dllexport


)


void


DisplayHex(


int


flag);


extern





_declspec


(


dllexport


)


void


Close();


//


只使用上面有注释的三个函数即可




HANDLE hComm;


//


串口的句柄



OVERLAPPED m_ov;


//


是一个包含了用于异步输入输出的信息的结构体



COMSTAT comstat;


//


包含串口结构信息



HANDLE hThread1;


//


读线程句柄



HANDLE hThread2;


//


写线程句柄



HWND hRbox;


bool


sendflag =


false


;


unsigned



char


sendchar, receivechar;


bool


DisplayHEX =


false


;


ProcessErr orMessage(


char


* ErrorText)


//


打印进程错误信息



{


















}


/************* ***


打开串口


********************* /



bool


openport(


char


*portname)


{


char


*Temp =


new



char


[200];


LPVOID lpMsgBuf;


FormatMessage(










FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,


NULL,


GetLastError(),


MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),


// Default language


(LPTSTR)&lpMsgBuf,


0,


NULL


);




sprintf(Temp,



,


MessageBox(NULL, Temp,



, MB_ICONSTOP);


LocalFree(lpMsgBuf);


delete


[] Temp;


return



true


;


(


char


*)ErrorText, lpMsgBuf,



);













}


hComm = CreateFile(portname,










GENERIC_READ | GENERIC_WRITE,


0,


0,


OPEN_EXISTING,


FILE_FLAG_OVERLAPPED,


0);


return


FALSE;


return



true


;


if


(hComm == INVALID_HANDLE_VALUE)


else


< /p>


/****************


设备控制块的设置


*********************/



//


参数



波 特率


rate_arg



9600



bool


setupdcb(


int


rate_arg)


{






























DCB dcb;


int


rate = rate_arg;


memset(&dcb, 0,


sizeof


(dcb));


//


申请一个


DCB


结构体空 间



if


(!GetCommState(hComm, &dcb))


//

获取当前


DCB


配置



{




}


/* ------------------------------------ -------------------------------- */



//


配置当前串口



gth =


sizeof


(dcb);


/* ----------


串口的配置


------- */



te = rate;


= NOPARITY;


y = 0;


ts = ONESTOPBIT;


ze = 8;


tsFlow = 0;


srFlow = 0;


ntrol = DTR_CONTROL_DISABLE;


nsitivity = 0;


ntrol = RTS_CONTROL_DISABLE;


= 0;


= 0;


/* ------


其他参数的设置


----- */



Char = 0;


y = 1;


= 0;


ProcessErrorMessage (



);


//


打 印错误信息



return


FALSE;


















}



OnError = 0;


ved = 0;


= 2;


m = 4;


r = 0x13;


ar = 0x19;


r = 0;


/* ------------------------------------ -------------------------------- */



//


用上面的设备控制块来设置当前串口



if


(!SetCommState(hComm, &dcb))


{




}


else




return


true


;


//


设置成功



ProcessErrorMessage(



);


return



false


;


/********* **********


超时的设置


************* **************/



bool



setuptimeout(


int



ReadInterval,


int



ReadTotalMultiplier,


int



ReadTotalconstant,


int



WriteTotalMultiplier,


int


WriteTotalconstant)


{















}


void


ReceiveChar()


{








BOOL bRead = TRUE;


BOOL bResult = TRUE;


DWORD dwError = 0;


DWORD BytesRead = 0;


char


RXBuff;


//


接收数据缓冲器



char


str[10];


COMMTIMEOUTS timeouts;


tervalTimeout = ReadInterval;






//


读间隔超时



talTimeoutConstant = ReadTotalconstant;


//


读时间常量



talTimeoutMultiplier = ReadTotalMultiplier;


//


读时间系数



otalTimeoutConstant = WriteTotalconstant;


//


写时间常量



otalTimeoutMultiplier = WriteTotalMultiplier;


//


写时间系数



if


(!SetCommTimeouts(hComm, &timeouts))


{




}


else




return



true


;


ProcessErro rMessage(



);


return



false


;


//


设置当前串口的超时设置














































for


(;;)


//


死循环等待



{























Sleep(100);


bResult = ClearCommError(hComm, &dwError, &comstat);


//


清除硬件的通 讯错误以及


if


(e == 0)



{

















bResult = ReadFile(hComm,


// Handle to COMM port










}


else


{





}


//


printf(


打印接收到的数据



sprintf(str,



, RXBuff);


SetWindowText(hRbox, str);


&RXBuff,


1,









// RX Buffer Pointer







// Read one byte



continue


;


if


(bRead)


获取通讯设备的当前状态



&BytesRead,


// Stores number of bytes read



&m_ov);




// pointer to the m_ov structure



if


(DisplayHEX){


//


printf(


打印接收到的数据



sprintf(str,



, RXBuff);


SetWindowText(hRbox, str);






































if


(!bResult)


//


读不成功



{














}


else



{



bRead = TRUE;


switch


(dwError = GetLastError())


{


case


ERROR_IO_PENDING:


{




}


default


:


{



}


}




break


;












bRead = FALSE;



break


;













}












}



}


}


// close if (bRead)



if


(!bRead)


{







}


bRead = TRUE;


bResult = GetOverlappedResult(hComm,


// Handle to COMM port






&m_ov,


// Overlapped structure



&BytesRead,


// Stores number of bytes read



TRUE);


// Wait flag



WriteChar(BYTE* m_szWriteBuffer, DWORD m_nToSend)


{
































BOOL bWrite = TRUE;


BOOL bResult = TRUE;


DWORD BytesSent = 0;


HANDLE m_hWriteEvent;


ResetEvent(m_hWriteEvent);


if


(bWrite)


{

























m_ = 0;


m_High = 0;


// Clear buffer



bResult = WriteFile(hComm,


// Handle to COMM Port







{















DWORD dwError = GetLastError();


switch


(dwError)


{


case


ERROR_IO_PENDING:


{






}


default


:


{





// all other error codes
























// continue to GetOverlappedResults()



BytesSent = 0;


bWrite = FALSE;



break


;


m_szWriteBuffer,


// Pointer to message buffer in calling finction



m_nToSend,


// Length of message to send



&BytesSent,


// Where to store the number of bytes sent



&m_ov);


// Overlapped structure



if


(!bResult)

-


-


-


-


-


-


-


-



本文更新与2021-02-08 13:36,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/614861.html

自己动手编写串口动态库(超详细)的相关文章