Topaz Filer - Email filing software
Serial Communications
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


Ted Momtgomery
Does anyone have a working example of reading and writing
to a serial port using c#?
Reply to this message...
Vote that this is a GOOD answer...
 
Double-click to open VS solution with elevated permissions on Windows 7
New BootFX DBGet build available
 
    
Hun Boon Teo
http://www.gotdotnet.com/userfiles/JustinHarrell/JustinIO.zip

"Ted Momtgomery" <Click here to reveal e-mail address> wrote in message news:15f701c1e4df$ed4f6d50$95e62ecf@tkmsftngxs02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Twitter and Snow… simple #uksnow
Windows 7 compatible tool for mounting ISO images
 
    
Ronald Putz
HY!

Try this. But you got to connect the Com1 and Com2 Port with a Cross Link
Cable.
When you start ths, the Programm will start to write to a Port and after a
small delay it will read from the other Port.

using System;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;

namespace Serial
{
unsafe class ReadSerialStream
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int CreateFile(string filename, uint access, uint sharemode,
uint security_attributes, uint creation, uint flags, uint template);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(int handle);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadFile(int hFile, byte* lpBuffer, int
nNumberOfBytesToRead, int* lpNumberOfBytesRead, NativeOverlapped*
lpOverlapped);

private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
const uint OPEN_EXISTING = 3;

public ReadSerialStream()
{
byte[] buffer = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
byte[] Data = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
int i = 0;
bool Error = false;

int Port = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);

//fixed( byte* ptrBuffer = &buffer[0]) ReadFile(ptrBuffer); DAS GLEICHE
WIE DIE NÄCHSTE ZEILE.
//fixed( byte* ptrBuffer = buffer) ReadFile(ptrBuffer);
try
{
fixed( byte* ptrBuffer = buffer)
{
int read = 0;

while(true)
{
string Answer = "";

Error = ReadFile(Port, ptrBuffer, 10, &read,
(System.Threading.NativeOverlapped*)0);

Console.WriteLine("Error: {0}", Error);
Console.WriteLine("Read: {0}", read);

byte* ptrTemp = ptrBuffer;

for(i = 0; i < 10; i++)
{
Data[i] = *ptrTemp;

Answer = Answer + ((char)Data[i]).ToString();

if(i != 9)
{
ptrTemp++;
}

Console.Write("{0}", Data[i]);
}

Console.WriteLine("");
Console.WriteLine("Antwort: {0}", Answer);
Console.WriteLine("");

Thread.Sleep(500);
}
}
}

finally
{
CloseHandle(Port);
}
}
}

unsafe class WriteSerialStream
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int CreateFile(string filename, uint access, uint sharemode,
uint security_attributes, uint creation, uint flags, uint template);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(int handle);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteFile(int hFile, byte* lpBuffer, int
nNumberOfBytesToWrite, int* lpNumberOfBytesWritten, NativeOverlapped*
lpOverlapped);

private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
const uint OPEN_EXISTING = 3;

public WriteSerialStream()
{
string SendString = "HelloWorld";
int SendBufferLength = SendString.Length;
int i = 0;
byte[] SendBuffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bool Error = false;

char[] charArray = SendString.ToCharArray();

for(i = 0; i < charArray.Length; i++)
{
SendBuffer[i] = (byte)charArray[i];
}

int Port = CreateFile("COM2",
GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);

//Lesen in eigenem Thread starten da sonst die whiles sich gegenseitig
blockieren:
Thread Reading = new Thread(new ThreadStart(StartReading) );
Reading.Start();

try
{
fixed( byte* ptrBuffer = SendBuffer)

while(true)
{
int Written = 0;
Error = WriteFile(Port, ptrBuffer, SendBufferLength, &Written,
(System.Threading.NativeOverlapped*)0);

Console.WriteLine("Error: {0}", Error);
Console.WriteLine("Bytes Written: {0}", Written);
Console.WriteLine("String Written: {0}", SendString);
Console.WriteLine("");

Thread.Sleep(200);
}
}
}

finally
{
CloseHandle(Port);
}
}

private void StartReading()
{
Thread.Sleep(200);
ReadSerialStream Read = new ReadSerialStream();
}
}

class Serial
{
[STAThread]
static void Main()
{
WriteSerialStream Write = new WriteSerialStream();
        }
    }
}

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Hun Boon Teo
I do not have any sample on how to use the code.
This is the description from the author of the code on gotdotnet site
"C# source code file I wrote to access the serial ports using the win32api. In order to use it, you would create a
instance of the CommPort class, set the various properties such as BaudRate, Parity etc. then call the Open() method,
the do Read() and Write() methods which give and take byte arrays, then when finished do a Close(). "

I am not sure whether this will help, you may want to take a look at this article in C++
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp

Or this article that use MSComm control using C#
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320

"Ronald Putz" <Click here to reveal e-mail address> wrote in message news:e#LLlZR5BHA.2216@tkmsftngp04...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Introduction to BootFX’s Object Relational Mapping Functionality article now live on CodeProject
Xenu Link Sleuth
 
    
Ted Montgomery
Thank you so much for all your help. The article on using MSComm control
in C# was exactly what I was looking for.

Thanks again,

Ted Montgomery

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Reply to this message...
Vote that this is a GOOD answer...
 
Chromium OS – really?
Invocation Software – the new name for the makers of Topaz Filer
 
    
Ted Montgomery
Thank you for the quick reply!

I downloaded JustinIO.zip but I am new to the world of C programing and
I am wondering if you have a read and write example using JustinIO?

Thanks again,

Ted Montgomery

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Console
System.Runtime.InteropServices.CharSet
System.Threading.NativeOverlapped
System.Threading.Thread
System.Threading.ThreadStart




Ad
BootFX
Reliable and powerful .NET application framework.
Looking to invest in a major software project? Technical and commercial advice available here.
Other Helpful Sites
MBR 247
Topaz Filer
SharePoint Email Filing
Software Advisory Services
 
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734