事实证明,汉斯走在了正确的轨道上。我有额外的代码来尝试修改连接的属性。我在最后注释掉了 SetCommState 命令(以减少我最初帖子的大小和混乱),认为这将消除任何问题,因为其余代码只修改了 DCB。在阅读了 Hans 的评论后,我在 CreateFile 命令之后注释掉了所有内容,它运行良好。这是 init_port 的全部内容。它允许 initCSACConnection 正确运行,但是当 main 尝试将返回的值分配给 csacConnection 时会导致访问冲突。
HANDLE init_port(char *port, int baud, char *control)
{
HANDLE portDescriptor;
DCB t;
int bit, stop ;
char par ;
portDescriptor = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0,
0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if ( portDescriptor == INVALID_HANDLE_VALUE )
{
perror("CreateFile") ;
exit( 9 ) ;
}
/* Strip the control parameters from the control string */
sscanf_s( control, "%1d%1c%1d", &bit, &par, &stop ) ;
/* Read in the current port attributes */
FillMemory(&t, sizeof(t), 0);
if ( GetCommState(portDescriptor, &t) == 0 )
{
perror("GetCommState") ;
printf ("opened port:(%s) descriptor:%d speed:%d c:%1d%c%1d\n",
port, portDescriptor, baud, bit, par, stop );
exit( 2 ) ;
}
/* Set the # bits, parity and stop bits */
if ( (bit < 5) || (bit > 8) )
{
fprintf(stderr, "Invalid character bit size:%d [5-8]\n",bit ) ;
exit( 2 ) ;
}
if ( par == 'E' || par == 'e' )
{
t.Parity = EVENPARITY ; /* Even parity */
t.fParity = FALSE ; /* Enable parity check */
}
else if ( par == 'O' || par == 'o' )
{
t.Parity = ODDPARITY ; /* Odd parity */
t.fParity = TRUE ; /* Enable parity check */
} else {
t.fParity = FALSE;
}
if (stop == 1)
t.StopBits = ONESTOPBIT;
else if (stop == 2)
t.StopBits = TWOSTOPBITS ; /* Two stop bits */
/* Set the baud rate */
switch(baud)
{
case 1200:
t.BaudRate = CBR_1200 ;
break;
case 2400:
t.BaudRate = CBR_2400 ;
break;
case 4800:
t.BaudRate = CBR_4800 ;
break;
case 9600:
t.BaudRate = CBR_9600 ;
break;
case 19200:
t.BaudRate = CBR_19200;
break;
case 38400:
t.BaudRate = CBR_38400;
break;
case 57600:
t.BaudRate = CBR_57600;
break;
case 115200:
t.BaudRate = CBR_115200;
break;
case 256000:
t.BaudRate = CBR_256000;
break;
}
if ( SetCommState(portDescriptor, &t) == 0 )
{
perror("SetCommState") ;
exit( 2 ) ;
}
return portDescriptor;
}