⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rs232.cpp

📁 提供串行端口通讯范例于Vaisual C++环境下
💻 CPP
字号:
/*****************************************************************************************/
/*      This project is designed to detect human skin hue and saturation value.          */
/*  Using mouse select a region in window "det_skin_color" first.                        */ 
/*  Hot key: "z" to detect selection region                                              */
/*																						 */
/*           "r" to do repaint window                                                    */
/*																						 */
/*           "c" to confirm the result and keep it for next time compare				 */
/*																						 */
/*           "n" to select next picture													 */
/*																						 */
/*           "ESC" to exit the program and save the result into "hue_range.txt"		     */
/*																						 */
/*   Author : Yuan Long Lo  Date : 2008/05/23                                            */
/*****************************************************************************************/
//#include <StdAfx.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iomanip.h>
#include <windows.h>
#include "fstream.h"

#define MAXSTRINGLENGTH 1024
HANDLE HCOM1;
DCB PortDCB;
COMMTIMEOUTS  time_out_control;
char errStr[MAXSTRINGLENGTH];
DWORD dwDefaultDword;
unsigned char WrBuffer[]={"1,EXEC,MOV P1:200,200,200"}; // change it for your need 

bool main()
{
	char fullname[200];
	char *portname;
	portname = "COM1";
	wsprintf(fullname, "\\\\.\\%s", portname);
	HCOM1 = CreateFile (fullname, // Pointer to the name of the port
                GENERIC_READ | GENERIC_WRITE,
                              // Access (read-write) mode
                0,            // Share mode
                NULL,         // Pointer to the security attribute
                OPEN_EXISTING,// How to open the serial port
                0,            // Port attributes
                NULL);        // Handle to port with attribute
                              // to copy

	//DCB PortDCB;	// Initialize the DCBlength member. 
	PortDCB.DCBlength = sizeof (DCB);	// Get the default port setting information.
	// Change the DCB structure settings.
	PortDCB.BaudRate = 19200;             // Current baud ;ORIGINAL 9600
	PortDCB.fBinary = TRUE;               // Binary mode; no EOF check 
	PortDCB.fParity = TRUE;               // Enable parity checking 
	PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control 
	PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control 
	PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
										  // DTR flow control type 
	PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity 
	PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx 
	PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control 
	PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control 
	PortDCB.fErrorChar = FALSE;           // Disable error replacement 
	PortDCB.fNull = FALSE;                // Disable null stripping 
	PortDCB.fRtsControl = RTS_CONTROL_ENABLE; 
										  // RTS flow control 
	PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on 
										  // error
	PortDCB.ByteSize = 8;                 // Number of bits/byte, 4-8 
	PortDCB.Parity = NOPARITY;			  // 0-4=no,odd,even,mark,space
	PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2 

	if (HCOM1 == INVALID_HANDLE_VALUE) 
	{
		//AfxMessageBox("Open Port failed");
		printf("Open Port failed");
		return(FALSE);
	}

	if(!GetCommState(HCOM1, &PortDCB))
	{
		//AfxMessageBox("!= Getcommonstate");
		printf("!= Getcommonstate");
		return(FALSE);
	}
	
	if (!BuildCommDCB("19200,n,8,1",&PortDCB)) 
	{
		//AfxMessageBox("!buildcommDCB 19200,n,8,1");
		printf("!buildcommDCB 19200,n,8,1");
		CloseHandle(HCOM1);
		HCOM1 = INVALID_HANDLE_VALUE;
		return(FALSE);
	}
	
	if (!SetCommState(HCOM1, &PortDCB))
	{
		//AfxMessageBox("!setcommmstate");
		printf("!setcommmstate");
		CloseHandle(HCOM1);
		HCOM1 = INVALID_HANDLE_VALUE;
		return(FALSE);
	}
	time_out_control.ReadIntervalTimeout = 20; 
    time_out_control.ReadTotalTimeoutMultiplier = 10; 
	time_out_control.ReadTotalTimeoutConstant = 100; 
    time_out_control.WriteTotalTimeoutMultiplier = 10; 
    time_out_control.WriteTotalTimeoutConstant = 100;

	SetCommTimeouts(HCOM1, &time_out_control);

	char byte0;
	byte0=0;
	DWORD* charOutCnt;
	charOutCnt=&dwDefaultDword;

	for (int i = 0; i < sizeof(WrBuffer); i++)
	{
	  byte0 = WrBuffer[i];	

				  WriteFile(
								HCOM1,        //HANDLE
								&byte0,		  //lpBuffer
								1,	          //NumberOfBytesWritten
								charOutCnt,	  //lpNumberOfBytesWritten
								NULL		  //lpOverlappede
						   );
	}


	CloseHandle(HCOM1);     //release com1 port resource important!
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -