473,387 Members | 2,436 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,387 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 13283
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.