| |
| 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(); } } }
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
| |
| |
| 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!
|
|
|
| |
|
| |
|
|
|