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("kern el32.dll", EntryPoint="Cre ateFile",
ExactSpelling=f alse, CharSet=CharSet .Unicode, SetLastError=tr ue)]
private static extern IntPtr CreateFile( string lpFileName, uint
uintDesiredAcce ss, int iShareMode, int lpSecurityAttri butes, int
iCreationDispos ition, uint iFlagsAndAttrib utes, int hTemplateFile);
[DllImport("kern el32.dll", EntryPoint="Set CommState",
ExactSpelling=f alse, CharSet=CharSet .Unicode, SetLastError=tr ue)]
private static extern bool SetCommState(In tPtr m_hPort, ref DCB
m_structDCB);
[DllImport("kern el32.dll", EntryPoint="Set CommTimeouts",
ExactSpelling=f alse, CharSet=CharSet .Unicode, SetLastError=tr ue)]
private static extern bool SetCommTimeouts (IntPtr m_hPort, ref
CommTimeOuts lpCommTimeouts) ;
m_hPort = CreateFile(m_sP ort, GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
CommTimeOuts m_structCommTim eOuts = new CommTimeOuts();
m_structCommTim eOuts.iReadInte rvalTimeout = ReadIntervalTim eout;
m_structCommTim eOuts.iReadTota lTimeoutMultipl ier =
ReadTotalTimeou tMultiplier;
m_structCommTim eOuts.iReadTota lTimeoutConstan t =
ReadTotalTimeou tConstant;
m_structCommTim eOuts.iWriteTot alTimeoutMultip lier =
WriteTotalTimeo utMultiplier;
m_structCommTim eOuts.iWriteTot alTimeoutConsta nt =
WriteTotalTimeo utConstant;
unsafe
{
SetCommTimeouts (m_hPort, ref m_structCommTim eOuts);
DCB m_structDCB = new DCB();
m_structDCB.DCB length = sizeof(DCB);
m_structDCB.Bau dRate = (uint)9600;
m_structDCB.Byt eSize = (byte)8;
m_structDCB.Sto pBits = (byte)1;
m_structDCB.Par ity = (byte)0;
m_structDCB.Xon Lim = 2048;
m_structDCB.Xof fLim = 512;
m_structDCB.Xon Char = 0x11; // Ctrl-Q
m_structDCB.Xof fChar = 0x13; // Ctrl-S
if (!SetCommState( m_hPort, ref m_structDCB))
{
int ErrorCode = GetLastError();
throw new ApplicationExce ption("Error Code " + ErrorNo);
}
// dcb structure
[StructLayout(La youtKind.Sequen tial)]
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 fTXContinueOnXo ff;
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("kern el32.dll", EntryPoint="Cre ateFile",
ExactSpelling=f alse, CharSet=CharSet .Unicode, SetLastError=tr ue)]
private static extern IntPtr CreateFile( string lpFileName, uint
uintDesiredAcce ss, int iShareMode, int lpSecurityAttri butes, int
iCreationDispos ition, uint iFlagsAndAttrib utes, int hTemplateFile);
[DllImport("kern el32.dll", EntryPoint="Set CommState",
ExactSpelling=f alse, CharSet=CharSet .Unicode, SetLastError=tr ue)]
private static extern bool SetCommState(In tPtr m_hPort, ref DCB
m_structDCB);
[DllImport("kern el32.dll", EntryPoint="Set CommTimeouts",
ExactSpelling=f alse, CharSet=CharSet .Unicode, SetLastError=tr ue)]
private static extern bool SetCommTimeouts (IntPtr m_hPort, ref
CommTimeOuts lpCommTimeouts) ;
m_hPort = CreateFile(m_sP ort, GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
CommTimeOuts m_structCommTim eOuts = new CommTimeOuts();
m_structCommTim eOuts.iReadInte rvalTimeout = ReadIntervalTim eout;
m_structCommTim eOuts.iReadTota lTimeoutMultipl ier =
ReadTotalTimeou tMultiplier;
m_structCommTim eOuts.iReadTota lTimeoutConstan t =
ReadTotalTimeou tConstant;
m_structCommTim eOuts.iWriteTot alTimeoutMultip lier =
WriteTotalTimeo utMultiplier;
m_structCommTim eOuts.iWriteTot alTimeoutConsta nt =
WriteTotalTimeo utConstant;
unsafe
{
SetCommTimeouts (m_hPort, ref m_structCommTim eOuts);
DCB m_structDCB = new DCB();
m_structDCB.DCB length = sizeof(DCB);
m_structDCB.Bau dRate = (uint)9600;
m_structDCB.Byt eSize = (byte)8;
m_structDCB.Sto pBits = (byte)1;
m_structDCB.Par ity = (byte)0;
m_structDCB.Xon Lim = 2048;
m_structDCB.Xof fLim = 512;
m_structDCB.Xon Char = 0x11; // Ctrl-Q
m_structDCB.Xof fChar = 0x13; // Ctrl-S
if (!SetCommState( m_hPort, ref m_structDCB))
{
int ErrorCode = GetLastError();
throw new ApplicationExce ption("Error Code " + ErrorNo);
}
// dcb structure
[StructLayout(La youtKind.Sequen tial)]
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 fTXContinueOnXo ff;
public uint fOutX;
public uint fInX;
public uint fErrorChar;
public uint fNull;
public uint fRtsControl;
public uint fAbortOnError;
}
Comment