如何用c程序进行串口通信?

发布于 2024-07-19 08:05:43 字数 50 浏览 9 评论 0原文

使用vc++编译器如何访问串口。 Bioscom()函数可以在Turbo C中使用。

Using vc++ compiler how one can access serial port. Bioscom() function can be used in Turbo C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囍孤女 2024-07-26 08:05:43

您必须像这样使用 CreateFile 打开适当的 com 设备。 适应您的需求。

// Handle of the communication connection 
void *comHandle;

// Port parameters, set to your own needs
unsigned portIndex;
unsigned baudRate;
unsigned dataBits;
Parity   parity;
unsigned stopBits;
bool     handShake;
int      readIntervalTimeout;
int      readTotalTimeoutMultiplier;
int      readTotalTimeoutConstant;
int      writeTotalTimeoutMultiplier;
int      writeTotalTimeoutConstant;
DCB dcb;
COMMTIMEOUTS ct;

// Create COM-device name string 
char comDevice[20];
sprintf(comDevice, "\\\\.\\COM%d", portIndex);

// Open serial port
_comHandle = CreateFile(comDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (comHandle == INVALID_HANDLE_VALUE)
{
  return false;
}


ct.ReadIntervalTimeout         = readIntervalTimeout;        
ct.ReadTotalTimeoutMultiplier  = readTotalTimeoutMultiplier; 
ct.ReadTotalTimeoutConstant    = readTotalTimeoutConstant;   
ct.WriteTotalTimeoutMultiplier = writeTotalTimeoutMultiplier;
ct.WriteTotalTimeoutConstant   = writeTotalTimeoutConstant;  

if (!GetCommState(_comHandle,&dcb))
{
  disconnect();
  return false;
}

dcb.BaudRate        = baudRate;  
dcb.ByteSize        = (BYTE)dataBits;
dcb.Parity          = (parity == None) ? NOPARITY : ((parity == Even) ? EVENPARITY : ODDPARITY);
dcb.StopBits        = (stopBits > 1) ? TWOSTOPBITS : ONESTOPBIT;
dcb.fRtsControl     = handShake ? RTS_CONTROL_HANDSHAKE : RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow    = handShake;
dcb.fOutxDsrFlow    = handShake;
dcb.fDtrControl     = handShake ? DTR_CONTROL_HANDSHAKE : DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = handShake;
dcb.fOutX           = FALSE;
dcb.fInX            = FALSE;
dcb.fErrorChar      = FALSE;
dcb.fNull           = FALSE;
dcb.fAbortOnError   = TRUE;

// Set port state
if( !SetCommState(_omHandle, &dcb) ||
    !SetCommTimeouts(comHandle, &ct) ||
    !SetupComm(comHandle, 64, 64) ||
    !PurgeComm(comHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
{
  disconnect();
  return false;
} 

阅读相应的 MSDN 条目以了解所调用的各种函数。 另外,为了简洁起见,我省略了断开连接方法。

you have to open the appropriate com-device with CreateFile like so. Adapt to your needs.

// Handle of the communication connection 
void *comHandle;

// Port parameters, set to your own needs
unsigned portIndex;
unsigned baudRate;
unsigned dataBits;
Parity   parity;
unsigned stopBits;
bool     handShake;
int      readIntervalTimeout;
int      readTotalTimeoutMultiplier;
int      readTotalTimeoutConstant;
int      writeTotalTimeoutMultiplier;
int      writeTotalTimeoutConstant;
DCB dcb;
COMMTIMEOUTS ct;

// Create COM-device name string 
char comDevice[20];
sprintf(comDevice, "\\\\.\\COM%d", portIndex);

// Open serial port
_comHandle = CreateFile(comDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (comHandle == INVALID_HANDLE_VALUE)
{
  return false;
}


ct.ReadIntervalTimeout         = readIntervalTimeout;        
ct.ReadTotalTimeoutMultiplier  = readTotalTimeoutMultiplier; 
ct.ReadTotalTimeoutConstant    = readTotalTimeoutConstant;   
ct.WriteTotalTimeoutMultiplier = writeTotalTimeoutMultiplier;
ct.WriteTotalTimeoutConstant   = writeTotalTimeoutConstant;  

if (!GetCommState(_comHandle,&dcb))
{
  disconnect();
  return false;
}

dcb.BaudRate        = baudRate;  
dcb.ByteSize        = (BYTE)dataBits;
dcb.Parity          = (parity == None) ? NOPARITY : ((parity == Even) ? EVENPARITY : ODDPARITY);
dcb.StopBits        = (stopBits > 1) ? TWOSTOPBITS : ONESTOPBIT;
dcb.fRtsControl     = handShake ? RTS_CONTROL_HANDSHAKE : RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow    = handShake;
dcb.fOutxDsrFlow    = handShake;
dcb.fDtrControl     = handShake ? DTR_CONTROL_HANDSHAKE : DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = handShake;
dcb.fOutX           = FALSE;
dcb.fInX            = FALSE;
dcb.fErrorChar      = FALSE;
dcb.fNull           = FALSE;
dcb.fAbortOnError   = TRUE;

// Set port state
if( !SetCommState(_omHandle, &dcb) ||
    !SetCommTimeouts(comHandle, &ct) ||
    !SetupComm(comHandle, 64, 64) ||
    !PurgeComm(comHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
{
  disconnect();
  return false;
} 

Read the appropriate MSDN entries for the various functions called. Also, I've left out the disconnect method for brevity reasons.

↘紸啶 2024-07-26 08:05:43

代码项目中有很多关于与 C++ 进行串行通信的文章。 这是返回的第一篇文章。 您基本上通过文件 I/O 操作来访问端口。 这有点复杂,我建议为这项任务找到合适的库。

They are many articles in Code Project regarding serial communication with C++. This is the first article returned. You basically access the port with file I/O operations. It is a bit complicated and I recommend finding an appropriate library for this task.

久伴你 2024-07-26 08:05:43

Microsoft Developer Network 上的此页面介绍了如何使用串行端口在 Windows 中,根据您选择的编译器,我认为这是您想要的目标环境。

This page on the Microsoft Developer Network introduces how to work with serial ports in Windows, which I assume is the environment you want to target, based on your choice of compiler.

凉城 2024-07-26 08:05:43

BIOS 功能仅在您使用 MSDOS 或非常旧版本的 Windows(并且特定于 Turbo C)时才可用。 对于现代版本的 Windows,您将需要使用操作系统 API 来执行串行 I/O。

The bios functions are only available if you are using MSDOS or very old versions of Windows (and are specific to Turbo C). For modern versions of Windows, you will need to use the OS APIs to perform serial I/O.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文