473,379 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Serial Port

How do I send some data to serial port (for example COM1:) in C#?
It will be nice if you will show me some example.
Nov 16 '05 #1
4 13277
Dave,
To my knowledge there is no support in the .net framework for legacy
ports. It looks like you may have to look into using api's/PInvoke. It looks
like they will support it in the future.

PInvoke samples C#:
http://www.gotdotnet.com/community/u...ery=SerialPort
http://msdn.microsoft.com/msdnmag/is...NETSerialComm/

HTH

--
Lateralus [MCAD]
"Dave" <da*********@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
How do I send some data to serial port (for example COM1:) in C#?
It will be nice if you will show me some example.

Nov 16 '05 #2
But is there exists any free library like OpenNETCF for Compact Framework?

"Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Dave,
To my knowledge there is no support in the .net framework for legacy
ports. It looks like you may have to look into using api's/PInvoke. It
looks like they will support it in the future.

PInvoke samples C#:

http://www.gotdotnet.com/community/u...ery=SerialPort
http://msdn.microsoft.com/msdnmag/is...NETSerialComm/

HTH

--
Lateralus [MCAD]
"Dave" <da*********@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
How do I send some data to serial port (for example COM1:) in C#?
It will be nice if you will show me some example.


Nov 16 '05 #3
It is supported in .NET 2.0. In current versions you'll have to use 3rd
party libraries or p/invoke.

Etienne Boucher
Nov 16 '05 #4
Copy the classes to a file and run, this should do.
(don't forget to copmile with /unsafe)

[StructLayout(LayoutKind.Sequential)]

public struct COMMTIMEOUTS

{

public uint ReadIntervalTimeout;

public uint ReadTotalTimeoutMultiplier;

public uint ReadTotalTimeoutConstant;

public uint WriteTotalTimeoutMultiplier;

public uint WriteTotalTimeoutConstant;

}



[StructLayout(LayoutKind.Sequential)]

public struct DCB

{

public uint DCBlength;

public uint BaudRate;

public uint fBinary;

public uint fParity;

public uint fOutxCtsFlow;

public uint fOutxDsrFlow;

public uint fDtrControl;

public uint fDsrSensitivit;

public uint fTXContinueOnXoff;

public uint fOutX;

public uint fInX;

public uint fErrorChar;

public uint fNull;

public uint fRtsControl;

public uint fAbortOnError;

public uint fDummy2;

public ushort wReserved;

public ushort XonLim;

public ushort XoffLim;

public byte ByteSize;

public byte Parity;

public byte StopBits;

public char XonChar;

public char XoffChar;

public char ErrorChar;

public char EofChar;

public char EvtChar;

public ushort wReserved1;

};



public class Rs232

{

#region Native Methos and Declaration

const uint GENERIC_READ = 0x80000000;

const uint GENERIC_WRITE = 0x40000000;

const uint GENERIC_EXECUTE = 0x20000000;

const uint GENERIC_ALL = 0x10000000;

const uint CREATE_NEW = 1;

const uint CREATE_ALWAYS = 2;

const uint OPEN_EXISTING = 3;

const uint OPEN_ALWAYS = 4;

const uint TRUNCATE_EXISTING = 5;

[DllImport("kernel32", SetLastError=true)]

static extern unsafe IntPtr CreateFile(

string FileName, // file name

uint DesiredAccess, // access mode

uint ShareMode, // share mode

uint SecurityAttributes, // Security Attributes

uint CreationDisposition, // how to create

uint FlagsAndAttributes, // file attributes

int hTemplateFile // handle to template file

);
[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool ReadFile(

IntPtr hFile, // handle to file

void* pBuffer, // data buffer

int NumberOfBytesToRead, // number of bytes to read

int* pNumberOfBytesRead, // number of bytes read

int Overlapped // overlapped buffer

);

[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool WriteFile(

IntPtr hFile, // handle to file

void* pBuffer, // data buffer

int nNumberOfBytesToWrite, // number of bytes to be written to the file

int* lpNumberOfBytesWritten, // number of bytes written

int Overlapped // overlapped buffer

);
[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool CloseHandle(

IntPtr hObject // handle to object

);

[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool GetCommState(

IntPtr hFile,

ref DCB lpDCB);

[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool SetCommState(

IntPtr hFile,

ref DCB lpDCB);

[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool SetCommTimeouts(

IntPtr hFile,

ref COMMTIMEOUTS lpCommTimeouts);

[DllImport("kernel32", SetLastError=true)]

static extern unsafe bool GetCommTimeouts(

IntPtr hFile,

ref COMMTIMEOUTS lpCommTimeouts);

#endregion

IntPtr handle;
public Rs232()

{

}

public bool Open(string comPort)

{

handle = CreateFile(

comPort,

GENERIC_READ | GENERIC_WRITE,

0,

0,

OPEN_EXISTING,

0,

0);
if (handle != IntPtr.Zero)

return true;

else

return false;

}

public unsafe int Read(byte[] buffer, int index, int count)

{

if (handle == IntPtr.Zero)

return 0;

int n = 0;

fixed (byte* p = buffer)

{

if (!ReadFile(handle, p + index, count, &n, 0))

return 0;

}

return n;

}

public unsafe int Write(byte[] buffer, int index, int count)

{

if (handle == IntPtr.Zero)

return 0;

int n = 0;

fixed (byte* p = buffer)

{

if (!WriteFile(handle, p + index, count, &n, 0))

return 0;

}

return n;

}
public unsafe bool Init(uint BaudRate, byte ByteSize, byte Parity, byte
StopBits)

{

if (handle == IntPtr.Zero)

return false;

// Init the com state

DCB dcb = new DCB();

if (!GetCommState(handle, ref dcb))

return false;

dcb.BaudRate = BaudRate;

dcb.ByteSize = ByteSize;

dcb.Parity = Parity;

dcb.StopBits = StopBits;

if (!SetCommState(handle, ref dcb))

return false;

// Init the com timeouts

COMMTIMEOUTS Commtimeouts = new COMMTIMEOUTS();

if (!GetCommTimeouts(handle, ref Commtimeouts))

return false;

Commtimeouts.ReadIntervalTimeout = 600;

if (!SetCommTimeouts(handle, ref Commtimeouts))

return false;

return true;

}

public bool Close()

{

// close file handle

return CloseHandle(handle);

}

}

"Dave" <da*********@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
How do I send some data to serial port (for example COM1:) in C#?
It will be nice if you will show me some example.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
2
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. ...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
4
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
6
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.