我正在使用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);
}
答案 0 :(得分:1)
我看到你清除RTS信号,我想这用于启用RS485收发器。 RS485通常是半双工,每次发送数据时都需要启用/禁用传输。如果不这样做,则无法传输或始终处于传输状态,您无法从同一线路上的其他设备接收任何数据。您可能需要切换此信号以使通信正常工作。