gpt4 book ai didi

c++ - Wince 6.0 串口配置

转载 作者:行者123 更新时间:2023-11-28 07:02:52 25 4
gpt4 key购买 nike

我正在使用 WinCE 6.0 设备进行串行通信。它有4个串口,我用的是2个,一个是RS232的,一个是RS485的。配置为 RS232 的串口工作正常,但 RS485 通信不工作 下面是代码,

int MbusSerialSlaveProtocol::startupServer(int slaveAddr,
const TCHAR * const portName,
long baudRate, int dataBits,
int stopBits, int parity)
{
int result;

TRACELOG2("Open port: %d \n", portName);
TRACELOG5("Parameters: %d, %d, %d, %d\n",
baudRate, dataBits, stopBits, parity);
TRACELOG2("Configuration: %d\n", timeOut);

if (isStarted())
return (FTALK_ILLEGAL_STATE_ERROR);

if ((slaveAddr <= 0) || (slaveAddr > 255))
return (FTALK_ILLEGAL_ARGUMENT_ERROR);
this->slaveAddr = slaveAddr;

if ((dataBits != SerialPort::SER_DATABITS_7) &&
(dataBits != SerialPort::SER_DATABITS_8))
return(FTALK_ILLEGAL_ARGUMENT_ERROR);

// Close an existing connection
if (serialPort.isOpen())
serialPort.closePort();

result = serialPort.openPort(portName);
if (result == SerialPort::SER_PORT_NO_ACCESS)
return(FTALK_PORT_NO_ACCESS);
if (result == SerialPort::SER_ALREADY_OPEN)
return(FTALK_PORT_ALREADY_OPEN);
if (result != SerialPort::SER_SUCCESS)
return(FTALK_OPEN_ERR);
result = serialPort.config(baudRate, dataBits,
stopBits, parity,
SerialPort::SER_HANDSHAKE_NONE);
if (result != SerialPort::SER_SUCCESS)
{
serialPort.closePort();
return(FTALK_ILLEGAL_ARGUMENT_ERROR);
}
if (serialMode == SER_RS485)
serialPort.clearRts();
return (FTALK_SUCCESS);
}
int main()
{

protocol = new ModbusSerialSlave(dataTable);
int test = atce_uart_set_interface(6, 485);
//result =

((ModbusSerialSlave*)protocol)->startupServer(01,_T("COM6:"),19200,8,1,0);
((ModbusSerialSlave*)protocol)->enableRs485Mode(10);
result = ((ModbusSerialSlave*)protocol)->startupServer(01,_T("COM6:"),port.nBaudRate,port.nDataBit,port.nStopBit,port.nParity);
while(1)
{
protocol->serverLoop();
}

}

////////////////////////////////////////////////////////////////////我将 RTS 信号更改为切换状态,但仍然无法正常工作。我将 RTS 延迟增加到 1000,尝试发送数据设备但没有得到结果。端口打开或参数设置没有错误,我通过将错误消息写入文件来监视它们。这是端口打开和配置的代码。

int SerialPort::openPort(const TCHAR * const portName)
{
ofstream config_file(TEST_FILE_NAME3);

if (isOpen())
{
config_file.write("open1",5);
closePort();
}
port = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (port == INVALID_HANDLE_VALUE)
{
switch (GetLastError())
{
case ERROR_FILE_NOT_FOUND:
{
config_file.write("open2",5);
return (SER_PORT_NOT_FOUND);
}
case ERROR_ACCESS_DENIED:
{
config_file.write("open3",5);
return (SER_ALREADY_OPEN);
}
default:
{
config_file.write("open4",5);
return (SER_API_ERROR);
}
}
}
// SetupComm(port, 256, 256); //ttt
// Save current device control block
if (!GetCommState(port, &savedDevCtrlBlock))
{
config_file.write("open5",5);
return (SER_API_ERROR);
}
this->flush(); // Needed on QNX 6 to clear a filled buffer
config_file.write("open6",5);

return (config(19200, SER_DATABITS_8, SER_STOPBITS_1,
SER_PARITY_NONE, SER_HANDSHAKE_NONE));
}



int SerialPort::config(long baudRate, int dataBits,
int stopBits, int parity, int flowControl)
{
DCB devCtrlBlock;
ofstream config_file(TEST_FILE_NAME);
if (!isOpen())
{
config_file.write("Line1",5);
return (SER_NOT_OPEN);
}

//
// Retrieve current device control block
//
if (!GetCommState(port, &devCtrlBlock))
{
config_file.write("Line2",5);
return (SER_API_ERROR);
}

//
// Modify device control block
//
devCtrlBlock.BaudRate = baudRate;
switch (dataBits)
{
case SER_DATABITS_7:
devCtrlBlock.ByteSize = 7;
break;
case SER_DATABITS_8:
devCtrlBlock.ByteSize = 8;
break;
default:
{
config_file.write("Line3",5);
return (SER_INVALID_PARAMETER);
}
}
switch (stopBits)
{
case SER_STOPBITS_1:
devCtrlBlock.StopBits = ONESTOPBIT;
break;
case SER_STOPBITS_2:
devCtrlBlock.StopBits = TWOSTOPBITS;
break;
default:
{
config_file.write("Line4",5);
return (SER_INVALID_PARAMETER);
}
}
switch (parity)
{
case SER_PARITY_NONE:
devCtrlBlock.fParity = FALSE;
devCtrlBlock.Parity = NOPARITY;
break;
case SER_PARITY_EVEN:
devCtrlBlock.fParity = TRUE;
devCtrlBlock.Parity = EVENPARITY;
break;
case SER_PARITY_ODD:
devCtrlBlock.fParity = TRUE;
devCtrlBlock.Parity = ODDPARITY;
break;
default:
{
config_file.write("Line5",5);
return (SER_INVALID_PARAMETER);
}
}
switch (flowControl)
{
case SER_HANDSHAKE_RTS_CTS:
devCtrlBlock.fOutX = FALSE; // Disable output X-ON/X-OFF
devCtrlBlock.fInX = FALSE; // Disable input X-ON/X-OFF
devCtrlBlock.fOutxCtsFlow = TRUE;
devCtrlBlock.fOutxDsrFlow = FALSE;
devCtrlBlock.fRtsControl = RTS_CONTROL_HANDSHAKE;
devCtrlBlock.fDtrControl = DTR_CONTROL_ENABLE;
devCtrlBlock.fDsrSensitivity = FALSE;
break;
case SER_HANDSHAKE_NONE:
devCtrlBlock.fOutX = FALSE; // Disable output X-ON/X-OFF
devCtrlBlock.fInX = FALSE; // Disable input X-ON/X-OFF
devCtrlBlock.fOutxCtsFlow = FALSE;
devCtrlBlock.fOutxDsrFlow = FALSE;
devCtrlBlock.fRtsControl = RTS_CONTROL_TOGGLE;
devCtrlBlock.fDtrControl = DTR_CONTROL_ENABLE;
devCtrlBlock.fDsrSensitivity = FALSE;
break;
default:
{
config_file.write("Line6",5);
return (SER_INVALID_PARAMETER);
}
}
devCtrlBlock.fBinary = TRUE;
devCtrlBlock.fErrorChar = FALSE;
devCtrlBlock.fNull = FALSE;
devCtrlBlock.fAbortOnError = FALSE;

//
// Store device control block
//
if (!SetCommState(port, &devCtrlBlock))
{
config_file.write("Line7",5);
return (SER_INVALID_PARAMETER);
}
this->baudRate = baudRate;
this->flowControl = flowControl;
config_file.write("Line8",5);

return (SER_SUCCESS);
}

最佳答案

我看到你清除了RTS信号,我想这是用来启用RS485收发器的。 RS485 通常是半双工的,每次发送数据都需要开启/关闭传输。如果不这样做,则无法传输,或者始终处于传输状态,因此无法从同一线路上的其他设备接收任何数据。您可能需要切换此信号才能使通信正常进行。

关于c++ - Wince 6.0 串口配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22178606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com