serial port programming in c#

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;
}
 
R

Richy Rich

Hi Claire,

I once had problems in C# trying to receive binary data on
the serial port.

I ended up using a C# implementation. This can be found
at

http://www.gotdotnet.com/Community/UserSamples/Details.aspx
?SampleGuid=b06e30f9-1301-4cc6-ac14-dfe325097c69

Make sure you put in the bug fixes though described on the
web page above (scroll down to see).

This is problem receiving on the port one byte at the
time. I simply used serialPort.Read instead of
serialPort.ReadByte.

Hope that helps :)

Richy.

-----Original Message-----
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;
}
.
 
N

Noah Coad [MCP & MVP]

You may want to check out my tutorial on simply serial RS-232 communications
in C# posted on DevHood at:
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320

Good luck!
-Noah Coad
Microsoft MVP & MCP [.NET/C#]


claire said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top