C
claire
I'm trying to open and configure a serial/comm port to talk to another
device. I've used the Win32 methods described on MSDN but I get an
unexpected error message when I try to configure the port -
SetCommState returns error 127 (which I think is
ERROR_PROC_NOT_FOUND).
Can anyone find the mistake in the code below, or suggest any changes
I could make? Thanks for any help. I've only included the relevant
sections of code - the SetCommTimeouts and CreateFile methods execute
successfully, and I do check the values returned before proceeding to
the SetCommState method.
*******************
// import declarations
[DllImport("kernel32.dll", EntryPoint="CreateFile",
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)]
private static extern IntPtr CreateFile( string lpFileName, uint
uintDesiredAccess, int iShareMode, int lpSecurityAttributes, int
iCreationDisposition, uint iFlagsAndAttributes, int hTemplateFile);
[DllImport("kernel32.dll", EntryPoint="SetCommState",
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)]
private static extern bool SetCommState(IntPtr m_hPort, ref DCB
m_structDCB);
[DllImport("kernel32.dll", EntryPoint="SetCommTimeouts",
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)]
private static extern bool SetCommTimeouts(IntPtr m_hPort, ref
CommTimeOuts lpCommTimeouts);
m_hPort = CreateFile(m_sPort, GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
CommTimeOuts m_structCommTimeOuts = new CommTimeOuts();
m_structCommTimeOuts.iReadIntervalTimeout = ReadIntervalTimeout;
m_structCommTimeOuts.iReadTotalTimeoutMultiplier =
ReadTotalTimeoutMultiplier;
m_structCommTimeOuts.iReadTotalTimeoutConstant =
ReadTotalTimeoutConstant;
m_structCommTimeOuts.iWriteTotalTimeoutMultiplier =
WriteTotalTimeoutMultiplier;
m_structCommTimeOuts.iWriteTotalTimeoutConstant =
WriteTotalTimeoutConstant;
unsafe
{
SetCommTimeouts(m_hPort, ref m_structCommTimeOuts);
DCB m_structDCB = new DCB();
m_structDCB.DCBlength = sizeof(DCB);
m_structDCB.BaudRate = (uint)9600;
m_structDCB.ByteSize = (byte)8;
m_structDCB.StopBits = (byte)1;
m_structDCB.Parity = (byte)0;
m_structDCB.XonLim = 2048;
m_structDCB.XoffLim = 512;
m_structDCB.XonChar = 0x11; // Ctrl-Q
m_structDCB.XoffChar = 0x13; // Ctrl-S
if (!SetCommState(m_hPort, ref m_structDCB))
{
int ErrorCode = GetLastError();
throw new ApplicationException("Error Code " + ErrorNo);
}
// dcb structure
[StructLayout(LayoutKind.Sequential)]
internal struct DCB
{
public int DCBlength;
public uint BaudRate;
public uint Flags;
public ushort wReserved;
public ushort XonLim;
public ushort XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public sbyte XonChar;
public sbyte XoffChar;
public sbyte ErrorChar;
public sbyte EofChar;
public sbyte EvtChar;
public ushort wReserved1;
public uint fBinary;
public uint fParity;
public uint fOutxCtsFlow;
public uint fOutxDsrFlow;
public uint fDtrControl;
public uint fDsrSensitivity;
public uint fTXContinueOnXoff;
public uint fOutX;
public uint fInX;
public uint fErrorChar;
public uint fNull;
public uint fRtsControl;
public uint fAbortOnError;
}
device. I've used the Win32 methods described on MSDN but I get an
unexpected error message when I try to configure the port -
SetCommState returns error 127 (which I think is
ERROR_PROC_NOT_FOUND).
Can anyone find the mistake in the code below, or suggest any changes
I could make? Thanks for any help. I've only included the relevant
sections of code - the SetCommTimeouts and CreateFile methods execute
successfully, and I do check the values returned before proceeding to
the SetCommState method.
*******************
// import declarations
[DllImport("kernel32.dll", EntryPoint="CreateFile",
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)]
private static extern IntPtr CreateFile( string lpFileName, uint
uintDesiredAccess, int iShareMode, int lpSecurityAttributes, int
iCreationDisposition, uint iFlagsAndAttributes, int hTemplateFile);
[DllImport("kernel32.dll", EntryPoint="SetCommState",
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)]
private static extern bool SetCommState(IntPtr m_hPort, ref DCB
m_structDCB);
[DllImport("kernel32.dll", EntryPoint="SetCommTimeouts",
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)]
private static extern bool SetCommTimeouts(IntPtr m_hPort, ref
CommTimeOuts lpCommTimeouts);
m_hPort = CreateFile(m_sPort, GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
CommTimeOuts m_structCommTimeOuts = new CommTimeOuts();
m_structCommTimeOuts.iReadIntervalTimeout = ReadIntervalTimeout;
m_structCommTimeOuts.iReadTotalTimeoutMultiplier =
ReadTotalTimeoutMultiplier;
m_structCommTimeOuts.iReadTotalTimeoutConstant =
ReadTotalTimeoutConstant;
m_structCommTimeOuts.iWriteTotalTimeoutMultiplier =
WriteTotalTimeoutMultiplier;
m_structCommTimeOuts.iWriteTotalTimeoutConstant =
WriteTotalTimeoutConstant;
unsafe
{
SetCommTimeouts(m_hPort, ref m_structCommTimeOuts);
DCB m_structDCB = new DCB();
m_structDCB.DCBlength = sizeof(DCB);
m_structDCB.BaudRate = (uint)9600;
m_structDCB.ByteSize = (byte)8;
m_structDCB.StopBits = (byte)1;
m_structDCB.Parity = (byte)0;
m_structDCB.XonLim = 2048;
m_structDCB.XoffLim = 512;
m_structDCB.XonChar = 0x11; // Ctrl-Q
m_structDCB.XoffChar = 0x13; // Ctrl-S
if (!SetCommState(m_hPort, ref m_structDCB))
{
int ErrorCode = GetLastError();
throw new ApplicationException("Error Code " + ErrorNo);
}
// dcb structure
[StructLayout(LayoutKind.Sequential)]
internal struct DCB
{
public int DCBlength;
public uint BaudRate;
public uint Flags;
public ushort wReserved;
public ushort XonLim;
public ushort XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public sbyte XonChar;
public sbyte XoffChar;
public sbyte ErrorChar;
public sbyte EofChar;
public sbyte EvtChar;
public ushort wReserved1;
public uint fBinary;
public uint fParity;
public uint fOutxCtsFlow;
public uint fOutxDsrFlow;
public uint fDtrControl;
public uint fDsrSensitivity;
public uint fTXContinueOnXoff;
public uint fOutX;
public uint fInX;
public uint fErrorChar;
public uint fNull;
public uint fRtsControl;
public uint fAbortOnError;
}