MFC-串口编程简单示例
来源:互联网 发布:tf idf算法java实现 编辑:程序博客网 时间:2024/06/14 01:14
// tcp_2_uart.cpp : 定义控制台应用程序的入口点。
//
// tcp_speed.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <Winsock2.h>
#include <sys/timeb.h>
#include <time.h>
#pragma comment(lib, "ws2_32.lib")
{
WORD wVersionRequested;
WSADATA wsaData;
int err, nRecv, nSend, nRead, nBlock=0, nRead1, nWrite;
SOCKET sockSrv=-1;
SOCKADDR_IN addrSrv;
char sendBuf[BUF_SIZE], readBuf[BUF_SIZE];
char recvBuf[32], p;
char file_name1[100] = "raw_sdkshell.bin", file_name2[100], ip_str[16] = "10.86.40.132";
FILE *fp1=NULL, *fp2=NULL;
int file_size, len, commId=0;
struct _timeb timebuffer;
long nBytes = 0, nBytes1=0, time1, time2, mtime1, mtime2;
int speed_i, speed_f;
HANDLE commHandle = INVALID_HANDLE_VALUE;
char commName[32] = {0};
DCB dcb;
COMMTIMEOUTS cmt; //create the object
printf("Please enter Serial port COM<x> (0 to skip): ");
scanf("%d", &commId);
/* open serial COM for read */
if(commId > 0)
{
sprintf(commName, "\\\\.\\COM%d", commId);
commHandle = CreateFile(commName, GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE == commHandle)
{
printf("Failed to open COM%d\n", commId);
goto err_0;
}
printf("Open COM%d, 115200, 8N1\n", commId);
//set the length of the DCB
dcb.DCBlength = sizeof(dcb);
GetCommState(commHandle, &dcb);
if(115200 != dcb.BaudRate)
{
if(!BuildCommDCB("115200,N,8,1", &dcb))
{
printf("COM error1 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
//set the state of fileHandle to be dcb returns a boolean indicating success or failure
if(!SetCommState(commHandle, &dcb))
{
printf("COM error2 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
}
//set the buffers to be size 1024 of fileHandle
if(!SetupComm(commHandle,2000,2000))
{
printf("COM error3 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
cmt.ReadIntervalTimeout = 100;
//value used to calculate the total time needed for a read operation
//which is (num bytes to read) * (timeout) in ms
cmt.ReadTotalTimeoutMultiplier = 1200;
//This value is added to the previous one to generate the timeout value
//for a single read operation (in ms)
cmt.ReadTotalTimeoutConstant = 1000;
//the next two values are the same as their read counterparts, only
//applying to write operations
cmt.WriteTotalTimeoutConstant = 1000;
cmt.WriteTotalTimeoutMultiplier = 1000;
//set the timeouts of fileHandle to be what is contained in cmt
//returns boolean success or failure
if(!SetCommTimeouts(commHandle, &cmt))
{
printf("COM error4 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
}
ReadFile(commHandle, readBuf, nSend, (LPDWORD)&nRead1, NULL);
}
//
// tcp_speed.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <Winsock2.h>
#include <sys/timeb.h>
#include <time.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
{
WORD wVersionRequested;
WSADATA wsaData;
int err, nRecv, nSend, nRead, nBlock=0, nRead1, nWrite;
SOCKET sockSrv=-1;
SOCKADDR_IN addrSrv;
char sendBuf[BUF_SIZE], readBuf[BUF_SIZE];
char recvBuf[32], p;
char file_name1[100] = "raw_sdkshell.bin", file_name2[100], ip_str[16] = "10.86.40.132";
FILE *fp1=NULL, *fp2=NULL;
int file_size, len, commId=0;
struct _timeb timebuffer;
long nBytes = 0, nBytes1=0, time1, time2, mtime1, mtime2;
int speed_i, speed_f;
HANDLE commHandle = INVALID_HANDLE_VALUE;
char commName[32] = {0};
DCB dcb;
COMMTIMEOUTS cmt; //create the object
printf("Please enter Serial port COM<x> (0 to skip): ");
scanf("%d", &commId);
/* open serial COM for read */
if(commId > 0)
{
sprintf(commName, "\\\\.\\COM%d", commId);
commHandle = CreateFile(commName, GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE == commHandle)
{
printf("Failed to open COM%d\n", commId);
goto err_0;
}
printf("Open COM%d, 115200, 8N1\n", commId);
//set the length of the DCB
dcb.DCBlength = sizeof(dcb);
GetCommState(commHandle, &dcb);
if(115200 != dcb.BaudRate)
{
if(!BuildCommDCB("115200,N,8,1", &dcb))
{
printf("COM error1 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
//set the state of fileHandle to be dcb returns a boolean indicating success or failure
if(!SetCommState(commHandle, &dcb))
{
printf("COM error2 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
}
//set the buffers to be size 1024 of fileHandle
if(!SetupComm(commHandle,2000,2000))
{
printf("COM error3 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
cmt.ReadIntervalTimeout = 100;
//value used to calculate the total time needed for a read operation
//which is (num bytes to read) * (timeout) in ms
cmt.ReadTotalTimeoutMultiplier = 1200;
//This value is added to the previous one to generate the timeout value
//for a single read operation (in ms)
cmt.ReadTotalTimeoutConstant = 1000;
//the next two values are the same as their read counterparts, only
//applying to write operations
cmt.WriteTotalTimeoutConstant = 1000;
cmt.WriteTotalTimeoutMultiplier = 1000;
//set the timeouts of fileHandle to be what is contained in cmt
//returns boolean success or failure
if(!SetCommTimeouts(commHandle, &cmt))
{
printf("COM error4 %d\n",GetLastError());
DeleteFile(commName);
goto err_0;
}
}
ReadFile(commHandle, readBuf, nSend, (LPDWORD)&nRead1, NULL);
printf("nRead1 = %d\n", nRead1);
}
0 0
- MFC-串口编程简单示例
- MFC 串口编程
- MFC串口编程
- MFC串口编程
- MFC 串口编程总结
- MFC 串口编程
- MFC 串口通信编程
- MFC 串口编程
- Windows 串口通信简单示例
- C++ MFC 简单 串口通信
- MFC简单的串口使用
- 用MFC实现串口编程
- 用MFC实现串口编程
- 用MFC实现串口编程
- 用MFC实现串口编程
- 用MFC实现串口编程
- 用MFC实现串口编程
- 用MFC实现串口编程
- Spring管理 hibernate 事务配置的五种方式
- [Leetcode] Combination Sum
- C# 中的Event EventArgs和Delegate(转载)
- C 语言 MYSQL API
- 一种简单的封装VP8/VP9视频的容器:IVF格式
- MFC-串口编程简单示例
- 理解eclipse(java工程)的工作目录
- Axis2创建WebService实例
- rapidxml,一个快速的xml库
- C/C++学习笔记20:迷途指针
- 使用ValidForm进行表单验证,结合SpringMVC检验用户名是否存在
- spring mvc 通过redirect重定向防止刷新页面重复提交又能返回数据给前台页面
- 两个数据交换的几种解法
- 预装APK的方法介绍