Francesca1983

Hi,

I've just finished to migrate my project in C#for win application in a project for WIN CE 6.0.

Compile is OK but when I run it in Windows CE 6.0 Platform it make me two error:

SystemIO.Exeption in serial port

Sysrem.Exeption in event click into picture box

What's wrong

Thanks




Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

ChunSheng Tang - MSFT


Hi,

You'd better paste your snippets of code and the corresponding stack trace, the information you provided is not enough to tell the problem.






Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Francesca1983

Hi,

This is error that's shown when I run my application on WinCE OS that I did myself:

France_CE.exe
IOException
in System.IO.Ports.SerialStream.WinIOError()
in System.IO.Ports.SerialStream..ctor()
in System.IO.Ports.SerialPort.Open()
in France_CE.ModBus.openComunication()
in France_CE.Form1..ctor()
in France_CE.Bunner..ctor()
in France_CE.Program.Main()

The code to Serial port in my application is:

SerialPort PSerial1 = new SerialPort ();

...

PSerial1 = new SerialPort();
PSerial1.Parity = Parity.None;
PSerial1.BaudRate = 19200;
PSerial1.ReadTimeout = 3000;
PSerial1.WriteTimeout = 3000;
PSerial1.Handshake = Handshake.None;
PSerial1.PortName = "Com1";
PSerial1.Open();

The methods I use are:

PSerial1.ReadByte();

PSerial1.Write(bufferTx,indCom,1);

Then I have a question:

what is stack trace where I can find it

Thanks a lot.






Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Francesca1983

Hi,

I try to create a Serial Port with API...in a simple windows application for WinCE 6.0...but it generate an error...

This is the code that I use:

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

namespace SerialPortAPIwin

{

public class Class1

{

public int PortNum;

public int BaudRate;

public byte ByteSize;

public byte Parity; // 0-4=no,odd,even,mark,space

public byte StopBits; // 0,1,2 = 1, 1.5, 2

public int ReadTimeout;

//comm port win32 file handle

private int hComm = -1;

public bool Opened = false;

//win32 api constants

private const uint GENERIC_READ = 0x80000000;

private const uint GENERIC_WRITE = 0x40000000;

private const int OPEN_EXISTING = 3;

private const int INVALID_HANDLE_VALUE = -1;

[StructLayout(LayoutKind.Sequential)]

public struct DCB

{

//taken from c struct in platform sdk

public int DCBlength; // sizeof(DCB)

public int BaudRate; // current baud rate

/* these are the c struct bit fields, bit twiddle flag to set

public int fBinary; // binary mode, no EOF check

public int fParity; // enable parity checking

public int fOutxCtsFlow; // CTS output flow control

public int fOutxDsrFlow; // DSR output flow control

public int fDtrControl; // DTR flow control type

public int fDsrSensitivity; // DSR sensitivity

public int fTXContinueOnXoff; // XOFF continues Tx

public int fOutX; // XON/XOFF out flow control

public int fInX; // XON/XOFF in flow control

public int fErrorChar; // enable error replacement

public int fNull; // enable null stripping

public int fRtsControl; // RTS flow control

public int fAbortOnError; // abort on error

public int fDummy2; // reserved

*/

public uint flags;

public ushort wReserved; // not currently used

public ushort XonLim; // transmit XON threshold

public ushort XoffLim; // transmit XOFF threshold

public byte ByteSize; // number of bits/byte, 4-8

public byte Parity; // 0-4=no,odd,even,mark,space

public byte StopBits; // 0,1,2 = 1, 1.5, 2

public char XonChar; // Tx and Rx XON character

public char XoffChar; // Tx and Rx XOFF character

public char ErrorChar; // error replacement character

public char EofChar; // end of input character

public char EvtChar; // received event character

public ushort wReserved1; // reserved; do not use

}

[StructLayout(LayoutKind.Sequential)]

private struct COMMTIMEOUTS

{

public int ReadIntervalTimeout;

public int ReadTotalTimeoutMultiplier;

public int ReadTotalTimeoutConstant;

public int WriteTotalTimeoutMultiplier;

public int WriteTotalTimeoutConstant;

}

[StructLayout(LayoutKind.Sequential)]

private struct OVERLAPPED

{

public int Internal;

public int InternalHigh;

public int Offset;

public int OffsetHigh;

public int hEvent;

}

[DllImport("kernel32.dll")]

private static extern int CreateFile(

string lpFileName, // file name

uint dwDesiredAccess, // access mode

int dwShareMode, // share mode

int lpSecurityAttributes, // SD

int dwCreationDisposition, // how to create

int dwFlagsAndAttributes, // file attributes

int hTemplateFile // handle to template file

);

[DllImport("kernel32.dll")]

private static extern bool GetCommState(

int hFile, // handle to communications device

ref DCB lpDCB // device-control block

);

[DllImport("kernel32.dll")]

private static extern bool BuildCommDCB(

string lpDef, // device-control string

ref DCB lpDCB // device-control block

);

[DllImport("kernel32.dll")]

private static extern bool SetCommState(

int hFile, // handle to communications device

ref DCB lpDCB // device-control block

);

[DllImport("kernel32.dll")]

private static extern bool GetCommTimeouts(

int hFile, // handle to comm device

ref COMMTIMEOUTS lpCommTimeouts // time-out values

);

[DllImport("kernel32.dll")]

private static extern bool SetCommTimeouts(

int hFile, // handle to comm device

ref COMMTIMEOUTS lpCommTimeouts // time-out values

);

[DllImport("kernel32.dll")]

private static extern bool ReadFile(

int hFile, // handle to file

byte[] lpBuffer, // data buffer

int nNumberOfBytesToRead, // number of bytes to read

ref int lpNumberOfBytesRead, // number of bytes read

ref OVERLAPPED lpOverlapped // overlapped buffer

);

[DllImport("kernel32.dll")]

private static extern bool WriteFile(

int hFile, // handle to file

byte[] lpBuffer, // data buffer

int nNumberOfBytesToWrite, // number of bytes to write

ref int lpNumberOfBytesWritten, // number of bytes written

ref OVERLAPPED lpOverlapped // overlapped buffer

);

[DllImport("kernel32.dll")]

private static extern bool CloseHandle(

int hObject // handle to object

);

[DllImport("kernel32.dll")]

private static extern uint GetLastError();

public void Open()

{

DCB dcbCommPort = new DCB();

COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();

// OPEN THE COMM PORT.

hComm = CreateFile("COM" + PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

// IF THE PORT CANNOT BE OPENED, BAIL OUT.

if (hComm == INVALID_HANDLE_VALUE)

{

throw (new ApplicationException("Comm Port Can Not Be Opened"));

}

// SET THE COMM TIMEOUTS.

GetCommTimeouts(hComm, ref ctoCommPort);

ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;

ctoCommPort.ReadTotalTimeoutMultiplier = 0;

ctoCommPort.WriteTotalTimeoutMultiplier = 0;

ctoCommPort.WriteTotalTimeoutConstant = 0;

SetCommTimeouts(hComm, ref ctoCommPort);

// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.

GetCommState(hComm, ref dcbCommPort);

dcbCommPort.BaudRate = BaudRate;

dcbCommPort.flags = 0;

//dcb.fBinary=1;

dcbCommPort.flags |= 1;

if (Parity > 0)

{

//dcb.fParity=1

dcbCommPort.flags |= 2;

}

dcbCommPort.Parity = Parity;

dcbCommPort.ByteSize = ByteSize;

dcbCommPort.StopBits = StopBits;

if (!SetCommState(hComm, ref dcbCommPort))

{

//uint ErrorNum=GetLastError();

throw (new ApplicationException("Comm Port Can Not Be Opened"));

}

//unremark to see if setting took correctly

//DCB dcbCommPort2 = new DCB();

//GetCommState(hComm, ref dcbCommPort2);

Opened = true;

}

public void Close()

{

if (hComm != INVALID_HANDLE_VALUE)

{

CloseHandle(hComm);

}

}

public byte[] Read(int NumBytes)

{

byte[] BufBytes;

byte[] OutBytes;

BufBytes = new byte[NumBytes];

if (hComm != INVALID_HANDLE_VALUE)

{

OVERLAPPED ovlCommPort = new OVERLAPPED();

int BytesRead = 0;

ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort);

OutBytes = new byte[BytesRead];

Array.Copy(BufBytes, OutBytes, BytesRead);

}

else

{

throw (new ApplicationException("Comm Port Not Open"));

}

return OutBytes;

}

public void Write(byte[] WriteBytes)

{

if (hComm != INVALID_HANDLE_VALUE)

{

OVERLAPPED ovlCommPort = new OVERLAPPED();

int BytesWritten = 0;

WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);

}

else

{

throw (new ApplicationException("Comm Port Not Open"));

}

}

}

class HexCon

{

//converter hex string to byte and byte to hex string

public static string ByteToString(byte[] InBytes)

{

string StringOut = "";

foreach (byte InByte in InBytes)

{

StringOut = StringOut + String.Format("{0:X2} ", InByte);

}

return StringOut;

}

public static byte[] StringToByte(string InString)

{

string[] ByteStrings;

ByteStrings = InString.Split(" ".ToCharArray());

byte[] ByteOut;

ByteOut = new byte[ByteStrings.Length - 1];

for (int i = 0; i == ByteStrings.Length - 1; i++)

{

ByteOut[i] = Convert.ToByte(("0x" + ByteStrings));

}

return ByteOut;

}

}

}

And the error say me that winCE can't find the library "kernel.dll".

If you want I can post the error that it show.

How can I do for use serial port in WinCE 6.0

Thanks.






Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

ChunSheng Tang - MSFT


There is no kernel32.dll(or user32.dll or gdi32.dll). Change it to 'coredll.dll'.




Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Francesca1983

I modify kernel32.dll in coredll.dll but when I run my application in WinCE 6.0 shows me this error:

Errore
DeviceApplication1.exe
ApplicationException
Comm Port Can Not Be Opened

in DeviceApplication1.SerialPortAPI.Open()
in DeviceApplication1.Form1.Form1_Load()
in System.Windows.Forms.Form.OnLoad()
in System.Windows.Forms.Form._SetVisibleNotify()
in System.Windows.Forms.Control.set_Visible()
in System.Windows.Forms.Application.Run()
in DeviceApplication1.Program.Main()

I open serial port in Form1_Load and I've also try to open this in ButtonOpen_Clik but the error is similar.

Thanks a lot.






Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

u.noize.u

Ciao
are you sure that com1 is available on your device




Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Francesca1983

Hi,

yes com1 is available...and I just try with another port, com2, com3 and com4....but the error is the same....do you have an idea for resolve thi problem of winCE 6.0 I have also installed .NET Framework 2.0 sp2, update of winCE...but the problem ramain the same....

Thanks






Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

D'Angelo Luca

did you try your code on the device emulator or on other platform rather than wince 6.0 just to check if works, if not drop me a mail at ldangelo[at]theranet.it with the project




Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Rajiv Srinivasan-MSFT

What device are you trying this on Does this device have Serial Port support Is it a custom CEPC or a Windows Mobile based device





Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Michael Koster

Hi

One problem is that the lpFileName you pass into the CreateFile API ("COM" + PortNum) does not work under CE.

The OS expect to find the semicolon at the end.

I marked the relevant sentence in the MSDN Docs red:

Code Block

lpFileName
[in] Pointer to a null-terminated string that specifies the name of the object, such as file, COM port, disk device, or console, to create or open.

If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This limit is related to how the CreateFile function parses paths.

When lpFileName points to a COM port to open, you must include a colon after the name. For example, specify COM1: to open that port. When using IrCOMM, specify COM3:.

Is there a good reason why you do not use the System.IO.SerialPort class available in .NET CF 2.0

Thanks

Michael






Re: .NET Compact Framework [WinCE6.0 in C#] Error in Serial port and click event

Francesca1983

Hi,

I don't use SerialPort Class because Datareceived and the trasmission paket of Byte more than 4 is disturbed...the packet arrives incomplete....data arrives with mistake...etc...

I've find this article in web....

http://www.codeproject.com/useritems/NET_20_SerialPort.asp

thanks

Francesca