serial port programming in c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • claire

    #1

    serial port programming in c#

    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;
    }
  • Richy Rich

    #2
    serial port programming in c#

    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


    ?SampleGuid=b06 e30f9-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.Read Byte.

    Hope that helps :)

    Richy.

    [color=blue]
    >-----Original Message-----
    >I'm trying to open and configure a serial/comm port to[/color]
    talk to another[color=blue]
    >device. I've used the Win32 methods described on MSDN[/color]
    but I get an[color=blue]
    >unexpected error message when I try to configure the[/color]
    port -[color=blue]
    >SetCommState returns error 127 (which I think is
    >ERROR_PROC_NOT _FOUND).
    >
    >Can anyone find the mistake in the code below, or suggest[/color]
    any changes[color=blue]
    >I could make? Thanks for any help. I've only included[/color]
    the relevant[color=blue]
    >sections of code - the SetCommTimeouts and CreateFile[/color]
    methods execute[color=blue]
    >successfully , and I do check the values returned before[/color]
    proceeding to[color=blue]
    >the SetCommState method.
    >
    >************** *****
    >
    >// import declarations
    >
    >[DllImport("kern el32.dll", EntryPoint="Cre ateFile",
    >ExactSpelling= false, CharSet=CharSet .Unicode,[/color]
    SetLastError=tr ue)][color=blue]
    >private static extern IntPtr CreateFile( string[/color]
    lpFileName, uint[color=blue]
    >uintDesiredAcc ess, int iShareMode, int[/color]
    lpSecurityAttri butes, int[color=blue]
    >iCreationDispo sition, uint iFlagsAndAttrib utes, int[/color]
    hTemplateFile);[color=blue]
    >
    >[DllImport("kern el32.dll", EntryPoint="Set CommState",
    >ExactSpelling= false, CharSet=CharSet .Unicode,[/color]
    SetLastError=tr ue)][color=blue]
    >private static extern bool SetCommState(In tPtr m_hPort,[/color]
    ref DCB[color=blue]
    >m_structDCB) ;
    >
    >[DllImport("kern el32.dll", EntryPoint="Set CommTimeouts",
    >ExactSpelling= false, CharSet=CharSet .Unicode,[/color]
    SetLastError=tr ue)][color=blue]
    >private static extern bool SetCommTimeouts (IntPtr m_hPort,[/color]
    ref[color=blue]
    >CommTimeOuts lpCommTimeouts) ;
    >
    >m_hPort = CreateFile(m_sP ort, GENERIC_READ |[/color]
    GENERIC_WRITE, 0, 0,[color=blue]
    >OPEN_EXISTIN G, 0, 0);
    >
    >CommTimeOuts m_structCommTim eOuts = new CommTimeOuts();
    >m_structCommTi meOuts.iReadInt ervalTimeout =[/color]
    ReadIntervalTim eout;[color=blue]
    >
    >m_structCommTi meOuts.iReadTot alTimeoutMultip lier =
    >ReadTotalTimeo utMultiplier;
    >
    >m_structCommTi meOuts.iReadTot alTimeoutConsta nt =
    >ReadTotalTimeo utConstant;
    >
    >m_structCommTi meOuts.iWriteTo talTimeoutMulti plier =
    >WriteTotalTime outMultiplier;
    >
    >m_structCommTi meOuts.iWriteTo talTimeoutConst ant =
    >WriteTotalTime outConstant;
    >
    >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 " +[/color]
    ErrorNo);[color=blue]
    >}
    >
    >
    >// 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;
    >}
    >.
    >[/color]

    Comment

    • Noah Coad [MCP & MVP]

      #3
      Re: serial port programming in c#

      You may want to check out my tutorial on simply serial RS-232 communications
      in C# posted on DevHood at:


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


      "claire" <big_pansy@hotm ail.com> wrote in message
      news:9a8ab19b.0 309190242.784db f4c@posting.goo gle.com...[color=blue]
      > 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;
      > }[/color]


      Comment

      Working...