//
// 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);
}