Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to communicate serial port using c#.net

Status
Not open for further replies.

volkan

Newbie level 6
Joined
Dec 29, 2004
Messages
14
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
151
I am a bit new in C# and havent seen any class that communicates with serial port. Do you know how can i make it or do you have any library codes for it.
 

hi

there is an activex control called mscomm32 that u can use easily in your application

or if u are using visual studio.net 2005 u will find a library called System.io.ports as what i think
 

I have the same problem. I try to control modem trough RS232 with a software running in windows. I have planned to use C++ how can I do it? Also I need an user interface.
 

mertkan65 said:
I have the same problem. I try to control modem trough RS232 with a software running in windows. I have planned to use C++ how can I do it? Also I need an user interface.

Here's some code that will help you get started using the Win32 API. Even though this is C code, it will work in a C++ program too.

Code:
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   char *pcCommPort = "COM2";

   hCom = CreateFile( pcCommPort,
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // no security attributes
                    OPEN_EXISTING, // must use OPEN_EXISTING
                    0,    // not overlapped I/O
                    NULL  // hTemplate must be NULL for comm devices
                    );

   if (hCom == INVALID_HANDLE_VALUE) 
   {
       // Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }

   // Build on the current configuration, and skip setting the size
   // of the input and output buffers with SetupComm.

   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      // Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

   dcb.BaudRate = CBR_57600;     // set the baud rate
   dcb.ByteSize = 8;             // data size, xmit, and rcv
   dcb.Parity = NOPARITY;        // no parity bit
   dcb.StopBits = ONESTOPBIT;    // one stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      // Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }

   printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
   return (0);
}

just remember that before your program exits use the
Code:
BOOL CloseHandle(
  HANDLE hObject
);
otherwise you might block access to the serial port until you reboot your computer.

- Jayson
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top