c語言程式設計實現電腦com口發資料
#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;
}