串行端口(RS -232)使用C ++连接 [英] Serial Port (RS -232) Connection in C++

查看:243
本文介绍了串行端口(RS -232)使用C ++连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中使用16位编译器(我使用Turbo C ++ IDE)完成了串行端口RS-232连接。它包含头文件 bios.h ,其中包含从端口读取值的所有必需函数。现在我想使用C ++ 32位Mingw编译器从串口读取值。我使用Dev CPP作为我的IDE。这里我找不到 bios.h 。 Mingw中有没有特殊的头文件可用于此目的?我现在使用32位编译器,因为在我的大学项目,我得使用异常处理,我猜是在Turbo C不支持。请帮助我。

解决方案

请看这里:





1)您可以在Windows(包括MinGW)以及Linux。或者你只能使用代码作为例子。



2)在Windows上使用串行端口



3) >

这里有一些很简单的代码(没有任何错误处理或设置):

  #include< windows.h> 

/ * ... * /


//打开串口
HANDLE serialHandle;

serialHandle = CreateFile(\\\\.\\COM1,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

//做一些基本设置
DCB serialParams = {0};
serialParams.DCBlength = sizeof(serialParams);

GetCommState(serialHandle,& serialParams);
serialParams.BaudRate = baudrate;
serialParams.ByteSize = byteSize;
serialParams.StopBits = stopBits;
serialParams.Parity = parity;
SetCommState(serialHandle,& serialParams);

//设置超时
COMMTIMEOUTS timeout = {0};
timeout.ReadIntervalTimeout = 50;
timeout.ReadTotalTimeoutConstant = 50;
timeout.ReadTotalTimeoutMultiplier = 50;
timeout.WriteTotalTimeoutConstant = 50;
timeout.WriteTotalTimeoutMultiplier = 10;

SetCommTimeouts(serialHandle,& timeout);

现在可以使用 WriteFile() ReadFile()写入/读取字节。
不要忘记关闭你的连接:

  CloseHandle(serialHandle); 


I have done serial port RS-232 connection in C++ using 16-bit compiler (I was using Turbo C++ IDE). It included header file bios.h which contain all the required functions for reading values from the port. Now I want to read value from serial port using C++ 32-bit Mingw compiler. I am using Dev CPP as my IDE. Here I could not find bios.h. Are there any special header files available for this purpose in Mingw? I am using 32-bit compiler now because in my college project I got to use Exception handling which I guess is not supported in Turbo C. Please help me out.

解决方案

Please take a look here:

1) You can use this with Windows (incl. MinGW) as well as Linux. Alternative you can only use the code as an example.

2) Step-by-step tutorial how to use serial ports on windows

3) You can use this literally on MinGW

Here's some very, very simple code (without any error handling or settings):

#include <windows.h>

/* ... */


// Open serial port
HANDLE serialHandle;

serialHandle = CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

// Do some basic settings
DCB serialParams = { 0 };
serialParams.DCBlength = sizeof(serialParams);

GetCommState(serialHandle, &serialParams);
serialParams.BaudRate = baudrate;
serialParams.ByteSize = byteSize;
serialParams.StopBits = stopBits;
serialParams.Parity = parity;
SetCommState(serialHandle, &serialParams);

// Set timeouts
COMMTIMEOUTS timeout = { 0 };
timeout.ReadIntervalTimeout = 50;
timeout.ReadTotalTimeoutConstant = 50;
timeout.ReadTotalTimeoutMultiplier = 50;
timeout.WriteTotalTimeoutConstant = 50;
timeout.WriteTotalTimeoutMultiplier = 10;

SetCommTimeouts(serialHandle, &timeout);

Now you can use WriteFile() / ReadFile() to write / read bytes. Don't forget to close your connection:

CloseHandle(serialHandle);

这篇关于串行端口(RS -232)使用C ++连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆