#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <winnt.h>
int main()
{
DWORD dwLength;
char recvBuf[1024];
DWORD dwactlen;
char psendbuf[32] = "test\n";
char ch;
DCB myDCB;
HANDLE m_hComm;
m_hComm = CreateFile(
"COM4:",
GENERIC_READ | GENERIC_WRITE, //允許讀和寫
0, //獨佔方式(共享模式)
NULL,
OPEN_EXISTING, //打開而不是創建(創建方式)
0,
NULL
);
if (m_hComm == (HANDLE)-1) //打開串口失敗返回
{
printf("打開串口失敗");
return 0;
}
//得到打開串口的當前屬性參數,修改後再重新設置串口。
if (!GetCommState(m_hComm, &myDCB))
{
printf("GetCommState error");
return FALSE;
}
//設置串口參數
myDCB.BaudRate = CBR_9600; // 設置波特率9600
myDCB.fBinary = TRUE; // 設置二進制模式,此處必須設置TRUE
myDCB.fParity = TRUE; // 支持奇偶校驗
myDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
myDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
myDCB.fDtrControl = DTR_CONTROL_DISABLE; // No DTR flow control
myDCB.fDsrSensitivity = FALSE; // DSR sensitivity
myDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
myDCB.fOutX = FALSE; // No XON/XOFF out flow control
myDCB.fInX = FALSE; // No XON/XOFF in flow control
myDCB.fErrorChar = FALSE; // Disable error replacement
myDCB.fNull = FALSE; // Disable null stripping
myDCB.fRtsControl = RTS_CONTROL_DISABLE; //No RTS flow control
myDCB.fAbortOnError = FALSE; // 當串口發生錯誤,並不終止串口讀寫
myDCB.ByteSize = 8; // 數據位,範圍:4-8
myDCB.Parity = NOPARITY; // 校驗模式
myDCB.StopBits = 0; // 1位停止位
//設置串口參數
if (!SetCommState(m_hComm, &myDCB))
{
printf("SetCommState error");
return FALSE;
}
SetupComm(m_hComm, 1024, 1024);//設置串口的輸入/輸出緩衝區大小
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR);
COMMTIMEOUTS TimeOuts;
//設定讀超時
TimeOuts.ReadIntervalTimeout = 1000;
TimeOuts.ReadTotalTimeoutMultiplier = 500;
TimeOuts.ReadTotalTimeoutConstant = 5000;
//設定寫超時
TimeOuts.WriteTotalTimeoutMultiplier = 500;
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(m_hComm, &TimeOuts); //設置超時 !mportant
//ClearCommError(m_hComm, &dwReadErrors, &cmState);
//printf("Input a character:");
//ch = _getch();
//printf("\nYou input a '%c'\n", ch);
//psendbuf[0] = ch;
while (1)
{
WriteFile(m_hComm, psendbuf, 32, &dwactlen, NULL);
ReadFile(m_hComm, recvBuf, 10, &dwLength, NULL);
printf("read success!\nread string: %s", recvBuf);
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR);
}
/*WriteFile(m_hComm, psendbuf,32, &dwactlen, NULL);
if (WriteFile(m_hComm, psendbuf, 32, &dwactlen, NULL))
{
printf("write success!\n");
}
else
{
printf("write failed!\n");
}
if (ReadFile(m_hComm, recvBuf, 10, &dwLength, NULL))
{
printf("read success!\nread string: %s", recvBuf);
}
else
{
printf("read failed!\n");
}*/
CloseHandle(m_hComm); //m_hComm是CreateFile函數返回的串口句柄。
return 0;
}
該程序在vs2013中能運行,能進行簡單的讀寫操作,尚待進一步完善。調試過程中可能遇到的問題及解決辦法如下:
問題一:
IntelliSense: "const char *" 類型的實參與 "LPCTSTR" 類型的形參不兼容 (vs2010)
例如:
MessageBox(szNameList,"人員");
改爲:MessageBox(szNameList,_T("人員"));
原因:
unicode字符問題。2010工程默認是寬字符版本
解決方法:
菜單 項目 -> 屬性 (打開屬性頁) -> 配置屬性 -> 常規 -> 字符集
單前選中的應該是“使用Unicode字符集”,改爲“使用多字節字符集”*/
問題二:
1>d:\code\20130925\20130925\stdafx.cpp(18): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2012\vc\include\stdio.h(218) : 參見“fopen”的聲明
1> 20130925.cpp
1> 正在生成代碼...
========== 生成: 成功 0 個,失敗 1 個,最新 0 個,跳過 0 個 ==========
解決方案,項目 =》屬性 =》c/c++ =》預處理器=》點擊預處理器定義,編輯,加入_CRT_SECURE_NO_WARNINGS,即可。*/
還遇到一個比較有意思的程序(轉載),可以進行讀取操作:
#include <stdio.h>
#include <windows.h>
int main(void)
{
FILE *fp;
char temp;
char buf[100];
if ((fp = fopen("com4", "r")) == NULL) puts("Can't open com3 /n");
while (1)
{
temp = 0;
fscanf(fp, "%c", &temp);
if (temp != 0)
putchar(temp);
else
Sleep(100);
}
fclose(fp);
return 0;
}