Serial Communication in C# Express



I had an inquiry about the use of MSComm OCX in a C# application awhile
ago and I really appreciated Nicholas Paldino's help, which is listed
below...
My code worked well in VS2003. I am now transfering the code into C#
Express, which has a new SerialPort class in it. I came across another
problem this time. My code is also listed below. I would appreciate your
comments.

/******* Your previous answer to my inquiry ********/
Haluk,

When you set the output like this:

com.Output = ((char) 34) + ((char) 14);

You are actually sending a (char) type, not a string, which is what
you really want to send. To do this, do the following:

com.Output = new string(new char[]{(char) 34, (char) 14});

This will create a new string composed of the two characters that
will be marshaled correctly to the COM object.

Also, I suspect that the output stops when it hits the null
character. To get around this, you will have to send the byte manually.

Hope this helps.

--
- Nicholas Paldino [.NET MVP]
- nicholas.paldino@xxxxxxxxxxxxxx


"Haluk" <xxxxxxxxxxxxxx> wrote in message
news:b03001c20e13$606eb230$37ef2ecf@xxxxxxxxxxxxxxx
> I am using mscommlib.ocx in my C# application to
> communicate via the serial port.
> After all the initialization code which I had already used
> in my previous VB6 applications successfully, I am using
> the following code to transmit data:
>
> com.Output = ((char) 34)+ ((char) 14);
>
> I am getting the following error code when I run this
> program:
>
> An unhandled exception of
> type 'System.Runtime.InteropServices.COMException'
> occurred in axinterop.mscommlib.dll
> Additional information: Exception from HRESULT: 0x800A017C
> (CTL_E_INVALIDPROPERTYVALUE).
>
> But also if I write the same code as follows:
>
> com.Output = "" + ((char) 34)+ ((char) 14);
>
> It runs successfully.
> This is my first problem...
>
> The second one is: instead of '34' or '14', for example if
> I decide to send '0' (zero), this character is not transmitted at all!
>
>
> com.Output = "" + ((char) 34)+ ((char) 0);
>
> I can only achieve to transmit '34', but not '0' when I
> run this code..
>
> Any explanation will be deeply appreciated..
>
> Regards,
> Haluk
>
>


/******* My new code in C# (Express) ******************/

After the usual port initialization,

if (com.IsOpen) com.Close();
com.Open();

// send char 34,14,192,51,0,0
com.Write(new string(new char[] { (char)34, (char)14, (char)192,
(char)51, (char)0, (char)0 }, 0, 6));

// for testing purposes I connected TX and RX pins of the port...

// receive routine:
private void com_DataReceived(object sender, SerialDataReceivedEventArgs
e)
{

// This method will be called when there is data waiting in the port's
buffer

// Obtain the number of bytes waiting in the port's buffer
bytes = com.BytesToRead;

// Create a byte array buffer to hold the incoming data
buffer = new char[bytes];

// Read the data from the port and store it in our buffer
com.Read(buffer, 0, bytes);

}


/****** My question about this new code *******/
After I activate the send routine, 6 bytes of data is received by pc but
with some difference to the originally sent data.

I receive : 34,14,63,51,0,0 everytime instead of 34,14,192,51,0,0

I tried to transmit 34,14,127,51,0,0 and received the same data
sequence...

As I try to tranmit anything bigger than 127 I receive 63 instead of the
original data...

Do you have any idea why this happens?
Best Regards,
Haluk


*** Sent via Developersdex http://www.developersdex.com ***
.



Relevant Pages

  • Re: Highly responsive serial port
    ... My problem consists of reading from a serial COM port as fluent as possible. ... If my reader only reads the input buffer after 50ms I'm too late to reply but I don't know it. ... Would a driver(in kernel mode) have enough priority to process such requests in time? ... a general rule of thumb in thread synchronization is to stay away from depending on "sleep" and loop designs in a vain attempt to synchronize I/O. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Serial COM port communication problems
    ... serial port. ... The receiving buffer is read by setting the mask to rx flag being the ... sub-packages arrive in succession and some sub-packages are lost. ... But this should not cause a loss of data, assuming you are reading the actual number of characters retured by WaitCommEvent. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Serial Communications - Lost Event
    ... "Completion of write" is a moment when all data from the app buffer is ... The driver can keep the data in its internal buffer ... > serial port monitoring and test applications. ... > of a serial port means when all of the data has been transfered. ...
    (microsoft.public.win32.programmer.kernel)
  • [PATCH] cpm_uart: Fix dpram allocation and non-console uarts
    ... Makes non-console UART work on both 8xx and 82xx ... static unsigned int cpm_uart_tx_empty(struct uart_port *port) ... /* Write back buffer pointer */ ... * Allocate DP-Ram and memory buffers. ...
    (Linux-Kernel)
  • Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6
    ... out all ones or all zeroes. ... of the multiple conditionals when the tx buffer is null, ... + * If the transmit buffer is null, zeroes will be shifted out while ... Only "len" bytes shift out. ...
    (Linux-Kernel)