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

2020-02-22 12:42

}

}

}

} // 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_ov.Offset = 0; m_ov.OffsetHigh = 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)

}

}

} }

ProcessErrorMessage(\);

} // end if(bWrite) if (!bWrite) {

bWrite = TRUE;

bResult = GetOverlappedResult(hComm, // Handle to COMM port

&m_ov, // Overlapped structure

&BytesSent, // Stores number of bytes sent TRUE); // Wait flag

// deal with the error code if (!bResult) { }

ProcessErrorMessage(\);

} // end if (!bWrite)

// Verify that the data size send equals what we tried to send if (BytesSent != m_nToSend) { }

return true;

printf(\,

BytesSent, strlen((char*)m_szWriteBuffer));

//读线程

DWORD WINAPI MyThread1(LPVOID pParam) { } //写线程

DWORD WINAPI MyThread2(LPVOID pParam) {

while (hComm != INVALID_HANDLE_VALUE) // 串口已被成功打开 { } return 0;

Sleep(100);//等待100毫秒 if (sendflag){ }

WriteChar(&sendchar, 1); sendflag = false;//发送

ReceiveChar(); return 0;

}

/************** 函数名Entry 参数

comname:串口名 BaudRate:波特率 ***************/

void Entry(char *comname, int BaudRate, HWND rhbox) { }

/*****************设置显示******************/ void DisplayHex(int flag){ }

/*****************设置超时*************/

void SetTimeOut(int a, int b, int c, int d, int e){ }

/************发送数据*************/ void SendChar(unsigned char ch){ }

/*********关闭************/ void Close(){

sendchar = ch;

sendflag = true;//通知发送线程发送数据 if (setuptimeout(a, b, c, d, e))

printf(\); if (flag == 0){ } else{ }

DisplayHEX = true; DisplayHEX = false; hRbox = rhbox;

if (openport(comname))

printf(\); printf(\); printf(\); if (setupdcb(BaudRate))

if (setuptimeout(0, 0, 0, 0, 0)) //清空缓冲区

PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); hThread1 = CreateThread(NULL, 0, MyThread1, 0, 0, NULL); //读线程 hThread2 = CreateThread(NULL, 0, MyThread2, 0, 0, NULL); //写线程

} CloseHandle(hThread1);//关闭都线程 CloseHandle(hThread2);//关闭写线程 CloseHandle(hComm);//关闭串口 二、验证是否成功 把DLL放在EXE一起

三、验证串口库代码:

#pragma comment(lib,\) extern \_declspec(dllimport)void Entry(char *comname, int BaudRate, NULL); extern \_declspec(dllimport) void SendChar(unsigned char ch); extern \_declspec(dllimport) void Close(); extern \_declspec(dllimport) void DisplayHex(int flag); #include #include int main() { char ch; Entry(\, 9600, NULL);//根据需要设置串口号 DisplayHex(0); Sleep(2000); SendChar('A');//测试字符 Sleep(2000); SendChar('B'); Sleep(2000); DisplayHex(1); SendChar(0x00); }Sleep(2000); SendChar(0xf3);//测试16进制数 Sleep(2000); SendChar(0xff); Sleep(2000); Close();//关闭串口 return 0; 四、(可选)编写窗口程序

可以看出不用MFC编写的串口程序体积非常小

五、单片机上的程序

#include #define jingzhen 11059200UL #define botelv 9600UL//定义波特率为9600 volatile unsigned char sending, receive = 0; unsigned char buf; sbit button = P1 ^ 7;//最后一个按钮


自己动手编写串口动态库(超详细)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:钜丰金业-新手如何炒黄金?

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: