请问:
怎样在WINDOWS下用C语言编写串口接收数据程序?
又怎样去测试程序是否正确?
12 个解决方案
#1
调用WindowsAPI的一些函数ReadFile、WriteFile等等。
验证:1、将pc串口的2-3脚短接,实现PC的自发自收,验证pc机程序的正确性!
2、将单片机的串口经电平转换(adm202)接PC的串口。试验通讯。
验证:1、将pc串口的2-3脚短接,实现PC的自发自收,验证pc机程序的正确性!
2、将单片机的串口经电平转换(adm202)接PC的串口。试验通讯。
#2
可行
#3
dos下的tc 中有个函数bioscom(),可以用c做;
我想windows中可试一下。
我想windows中可试一下。
#4
可以用函数ReadFile、WriteFile
还可以用inputb和outputb
方法是很多的
你可以将串口的2-3脚短接,实现PC的自发自收,然后判断正确性!
还可以用inputb和outputb
方法是很多的
你可以将串口的2-3脚短接,实现PC的自发自收,然后判断正确性!
#5
我也想到用BIOSCOM,你们能提供实例吗?
#6
用DELPHI最简单,有例程,用 的是最专业的通讯控件tubropower,需要发消息。
#7
用com1 c的话 inport() 函数
#8
portr.c 读串口
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Reads characters from an IO port.
*
* Usage
* -----
* ./portr.exe <IO port> <count>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1024
int main(int argc, char** argv) {
char *me = "portr"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char msg[MAX+1]; /* Message to write to port. */
int n_msg; /* Counts bytes to read. */
int n_in; /* Counts bytes read. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
n_msg = atoi(*++argv);
}
else {
printf ("%s: Usage: %s <IO port> <count>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Read message from port. ---*/
n_msg = (n_msg <= MAX ? n_msg : MAX);
ok = ReadFile(hPort, msg, n_msg, &n_in, NULL);
if (!ok) {
printf("%s: Cannot read message (error %d)\n", me, GetLastError());
return 1;
}
msg[n_msg] = '\0';
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_in);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
portw.c 写串口
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Writes message to an IO port.
*
* Usage
* -----
* ./portw.exe <IO port> <message>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char** argv) {
char *me = "portw"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char *msg; /* Message to write to port. */
int n_msg; /* Counts bytes in message. */
int n_out; /* Counts bytes written. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
msg = *++argv;
}
else {
printf ("%s: Usage: %s <IO port> <message>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Write message to port. ---*/
n_msg = strlen(msg)+1;
ok = WriteFile(hPort, msg, n_msg, &n_out, NULL);
if (!ok) {
printf("%s: Cannot write message (error %d)\n", me, GetLastError());
return 1;
}
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_out);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Reads characters from an IO port.
*
* Usage
* -----
* ./portr.exe <IO port> <count>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1024
int main(int argc, char** argv) {
char *me = "portr"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char msg[MAX+1]; /* Message to write to port. */
int n_msg; /* Counts bytes to read. */
int n_in; /* Counts bytes read. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
n_msg = atoi(*++argv);
}
else {
printf ("%s: Usage: %s <IO port> <count>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Read message from port. ---*/
n_msg = (n_msg <= MAX ? n_msg : MAX);
ok = ReadFile(hPort, msg, n_msg, &n_in, NULL);
if (!ok) {
printf("%s: Cannot read message (error %d)\n", me, GetLastError());
return 1;
}
msg[n_msg] = '\0';
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_in);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
portw.c 写串口
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Writes message to an IO port.
*
* Usage
* -----
* ./portw.exe <IO port> <message>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char** argv) {
char *me = "portw"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char *msg; /* Message to write to port. */
int n_msg; /* Counts bytes in message. */
int n_out; /* Counts bytes written. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
msg = *++argv;
}
else {
printf ("%s: Usage: %s <IO port> <message>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Write message to port. ---*/
n_msg = strlen(msg)+1;
ok = WriteFile(hPort, msg, n_msg, &n_out, NULL);
if (!ok) {
printf("%s: Cannot write message (error %d)\n", me, GetLastError());
return 1;
}
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_out);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
#9
真不知你该怎么谢我
#10
outputb/inputb在9x下还行,NT内核系统行不通。
#11
1、windows api:
createfile()
readfile()
writefile()
getcommstatus()
setcommstatus()
2、microsoft提供了控件MSCOMM,可很方便地在VC,VB 中使用
createfile()
readfile()
writefile()
getcommstatus()
setcommstatus()
2、microsoft提供了控件MSCOMM,可很方便地在VC,VB 中使用
#12
"windows.h"在哪?
#1
调用WindowsAPI的一些函数ReadFile、WriteFile等等。
验证:1、将pc串口的2-3脚短接,实现PC的自发自收,验证pc机程序的正确性!
2、将单片机的串口经电平转换(adm202)接PC的串口。试验通讯。
验证:1、将pc串口的2-3脚短接,实现PC的自发自收,验证pc机程序的正确性!
2、将单片机的串口经电平转换(adm202)接PC的串口。试验通讯。
#2
可行
#3
dos下的tc 中有个函数bioscom(),可以用c做;
我想windows中可试一下。
我想windows中可试一下。
#4
可以用函数ReadFile、WriteFile
还可以用inputb和outputb
方法是很多的
你可以将串口的2-3脚短接,实现PC的自发自收,然后判断正确性!
还可以用inputb和outputb
方法是很多的
你可以将串口的2-3脚短接,实现PC的自发自收,然后判断正确性!
#5
我也想到用BIOSCOM,你们能提供实例吗?
#6
用DELPHI最简单,有例程,用 的是最专业的通讯控件tubropower,需要发消息。
#7
用com1 c的话 inport() 函数
#8
portr.c 读串口
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Reads characters from an IO port.
*
* Usage
* -----
* ./portr.exe <IO port> <count>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1024
int main(int argc, char** argv) {
char *me = "portr"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char msg[MAX+1]; /* Message to write to port. */
int n_msg; /* Counts bytes to read. */
int n_in; /* Counts bytes read. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
n_msg = atoi(*++argv);
}
else {
printf ("%s: Usage: %s <IO port> <count>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Read message from port. ---*/
n_msg = (n_msg <= MAX ? n_msg : MAX);
ok = ReadFile(hPort, msg, n_msg, &n_in, NULL);
if (!ok) {
printf("%s: Cannot read message (error %d)\n", me, GetLastError());
return 1;
}
msg[n_msg] = '\0';
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_in);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
portw.c 写串口
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Writes message to an IO port.
*
* Usage
* -----
* ./portw.exe <IO port> <message>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char** argv) {
char *me = "portw"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char *msg; /* Message to write to port. */
int n_msg; /* Counts bytes in message. */
int n_out; /* Counts bytes written. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
msg = *++argv;
}
else {
printf ("%s: Usage: %s <IO port> <message>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Write message to port. ---*/
n_msg = strlen(msg)+1;
ok = WriteFile(hPort, msg, n_msg, &n_out, NULL);
if (!ok) {
printf("%s: Cannot write message (error %d)\n", me, GetLastError());
return 1;
}
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_out);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Reads characters from an IO port.
*
* Usage
* -----
* ./portr.exe <IO port> <count>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1024
int main(int argc, char** argv) {
char *me = "portr"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char msg[MAX+1]; /* Message to write to port. */
int n_msg; /* Counts bytes to read. */
int n_in; /* Counts bytes read. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
n_msg = atoi(*++argv);
}
else {
printf ("%s: Usage: %s <IO port> <count>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Read message from port. ---*/
n_msg = (n_msg <= MAX ? n_msg : MAX);
ok = ReadFile(hPort, msg, n_msg, &n_in, NULL);
if (!ok) {
printf("%s: Cannot read message (error %d)\n", me, GetLastError());
return 1;
}
msg[n_msg] = '\0';
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_in);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
portw.c 写串口
/*---------------------------------------------------------------------------
*
* Synopsis
* --------
* Writes message to an IO port.
*
* Usage
* -----
* ./portw.exe <IO port> <message>
*
* Author/Copyright
* ----------------
* Raymond McKendall
* 2001
*
*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char** argv) {
char *me = "portw"; /* Who I am. */
char *port; /* IO port to use: COM1, LPT1, etc. */
char *msg; /* Message to write to port. */
int n_msg; /* Counts bytes in message. */
int n_out; /* Counts bytes written. */
int ok; /* Return code from function calls. */
HANDLE hPort; /* Handle to IO port object. */
/*--- Get port ---*/
argc --;
if (argc == 2) {
port = *++argv;
msg = *++argv;
}
else {
printf ("%s: Usage: %s <IO port> <message>\n", me, me);
return 1;
}
/*--- Open IO port. ---*/
hPort = CreateFile(port, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort == INVALID_HANDLE_VALUE) {
printf("%s: Cannot open IO port %s (error %d)\n", me, port, GetLastError());
return 1;
}
/*--- Write message to port. ---*/
n_msg = strlen(msg)+1;
ok = WriteFile(hPort, msg, n_msg, &n_out, NULL);
if (!ok) {
printf("%s: Cannot write message (error %d)\n", me, GetLastError());
return 1;
}
printf ("%s: %s (%d of %d bytes)\n", me, msg, n_msg, n_out);
/*--- Cleanup. ---*/
ok = CloseHandle(hPort);
if (!ok) {
printf("%s: Cannot close port handle (error %d)\n", me, GetLastError());
}
return 0;
}
#9
真不知你该怎么谢我
#10
outputb/inputb在9x下还行,NT内核系统行不通。
#11
1、windows api:
createfile()
readfile()
writefile()
getcommstatus()
setcommstatus()
2、microsoft提供了控件MSCOMM,可很方便地在VC,VB 中使用
createfile()
readfile()
writefile()
getcommstatus()
setcommstatus()
2、microsoft提供了控件MSCOMM,可很方便地在VC,VB 中使用
#12
"windows.h"在哪?