473,320 Members | 1,958 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,320 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 13269
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.