关键词不能为空

当前您在: 主页 > 英语 >

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

作者:高考题库网
来源: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

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

  • 余华爱情经典语录,余华爱情句子

    余华的经典语录——余华《第七天》40、我不怕死,一点都不怕,只怕再也不能看见你——余华《第七天》4可是我再也没遇到一个像福贵这样令我难忘的人了,对自己的经历如此清楚,

    语文
  • 心情低落的图片压抑,心情低落的图片发朋友圈

    心情压抑的图片(心太累没人理解的说说带图片)1、有时候很想找个人倾诉一下,却又不知从何说起,最终是什么也不说,只想快点睡过去,告诉自己,明天就好了。有时候,突然会觉得

    语文
  • 经典古训100句图片大全,古训名言警句

    古代经典励志名言100句译:好的药物味苦但对治病有利;忠言劝诫的话听起来不顺耳却对人的行为有利。3良言一句三冬暖,恶语伤人六月寒。喷泉的高度不会超过它的源头;一个人的事

    语文
  • 关于青春奋斗的名人名言鲁迅,关于青年奋斗的名言鲁迅

    鲁迅名言名句大全励志1、世上本没有路,走的人多了自然便成了路。下面是我整理的鲁迅先生的名言名句大全,希望对你有所帮助!当生存时,还是将遭践踏,将遭删刈,直至于死亡而

    语文
  • 三国群英单机版手游礼包码,三国群英手机单机版攻略

    三国群英传7五神兽洞有什么用那是多一个武将技能。青龙飞升召唤出东方的守护兽,神兽之一的青龙。玄武怒流召唤出北方的守护兽,神兽之一的玄武。白虎傲啸召唤出西方的守护兽,

    语文
  • 不收费的情感挽回专家电话,情感挽回免费咨询

    免费的情感挽回机构(揭秘情感挽回机构骗局)1、牛牛(化名)向上海市公安局金山分局报案,称自己为了挽回与女友的感情,被一家名为“实花教育咨询”的情感咨询机构诈骗4万余元。

    语文