4

I'm trying to write data to a serial port via P/Invoke (explicitly not with the SerialPort-class). Here's what I have so far:

[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(string filename,       // file name
                                uint desiredAccess,    // read? write?
                                uint shareMode,        // sharing
                                uint attributes,       // SecurityAttributes pointer
                                uint creationDisposition,
                                uint flagsAndAttributes,
                                uint templateFile);

[DllImport("kernel32", SetLastError = true)]
static extern bool WriteFile(IntPtr hFile,               // handle to file
                             byte[] lpBuffer,            // data buffer
                             uint nBytesToWrite,         // number of bytes to write
                             out uint nBytesWritten,     // number of bytes written
                             [In] ref NativeOverlapped overlapped); // overlapped buffer

const uint OPEN_EXISTING = 3;

_portHandle = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
uint bytesWritten;
var buffer = Encoding.ASCII.GetBytes("foo\r");
var overlapped = new NativeOverlapped();
WriteFile(_portHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);

I get a _portHandle != -1 and can call WriteFile. But after the call, it does nothing. I can wait forever in VS-debug mode and wait for the call to return. Also, no exception is thrown.

Any kernel32.dll-masters out there who can help me?

Papa Mufflon
  • 17,558
  • 5
  • 27
  • 35
  • Does it also hang when calling the functions from native C code? – ThiefMaster Dec 04 '10 at 11:57
  • 3
    Is there a reason why you're using P/Invoke instead of the managed framework library to do that? – Frédéric Hamidi Dec 04 '10 at 11:58
  • Yes, Silverlight 5-preparation :p – Papa Mufflon Dec 04 '10 at 11:59
  • Don't know if it works with native C, but it works with the .net-SerialPort class. – Papa Mufflon Dec 04 '10 at 12:02
  • @Papa, your code doesn't show the type of `_portHandle`. That's an `IntPtr`, right? Also, what happens if you call `WriteFile()` synchronously instead of using an `OVERLAPPED`? – Frédéric Hamidi Dec 04 '10 at 13:06
  • Yes, it's an IntPtr. What should I do to call WriteFile() synchronously? Isn't the NativeOverlapped struct just for the layout of the array? – Papa Mufflon Dec 04 '10 at 13:18
  • @Papa, yup, you're right, the `NativeOverlapped` confused me. Now, I'm not sure about the stray `\r` at the end of your output, especially with a serial interface... can you try with `\n` or `Environment.NewLine` and see what happens? – Frédéric Hamidi Dec 04 '10 at 13:36
  • This should make no problems. With the class mentioned in my answer, there is no problem with "\r". Probably, I missed some initialization / configuration (baudrate, stopbits, etc.)!? Anyway, I work now with John Hind's class.However - thanks for the help. – Papa Mufflon Dec 06 '10 at 12:19

2 Answers2

2

Thanks from Papa Mufflon for introducing that usable project, I derived that for myself and made very simple opensource project from it https://github.com/ebraminio/PInvokeSerialPort that you can also download it from https://nuget.org/packages/PInvokeSerialPort.

Of-course this project is not complete so any suggestion any feature requesting on it is valuable for me :)

Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
0

Taken Serial Comm from John Hind (http://msdn.microsoft.com/en-us/magazine/cc301786.aspx) instead of developing my own :)

Works great!

Papa Mufflon
  • 17,558
  • 5
  • 27
  • 35
  • I'm trying to communicate with a serial device with Silverlight 5 as well, were you able to use this code to do this ? – Andy M Jan 27 '12 at 09:02
  • Sorry for late answer, but yes, I managed to get it to work. Unfortunately, I've done it for my former company and therefore doesn't have the code anymore. Btw, it was with the Silverlight beta - maybe something changed in the meantime!? – Papa Mufflon Feb 01 '12 at 06:10
  • No problem, I've found how to make it work ! You can find how I did this here : http://stackoverflow.com/questions/8989538/serial-communication-with-silverlight-5-com-port/9092519#9092519 – Andy M Feb 01 '12 at 07:57