I have the following method that handles the data received event:
private void _port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
string data = _port.ReadExisting();
_log.LogToFile(data, true);
}
The LogToFile function logs data to a file. The second parameter
indicate if the file should timestamp the message.
On the initial test of this method I got the following result:
[9/19/2008 4:40:04 PM]10??I 1
[9/19/2008 4:40:04 PM] 1909081
[9/19/2008 4:40:04 PM]640?
This is not what I expected. I converting an old VB 6.0 program to
C#. If I add Thread.Sleep(500) before the ReadExisting() I get the
string I was expecting:
[9/19/2008 4:57:47 PM]10??I 1 1909081657?
Notice in the first try it is sending 8 bytes at a time. Is there
anyway to avoid using Thread.Sleep to achieve this? 8 6425
hi Kevin, Ke***********@gmail.com wrote:
On the initial test of this method I got the following result:
[9/19/2008 4:40:04 PM]10??I 1
[9/19/2008 4:40:04 PM] 1909081
[9/19/2008 4:40:04 PM]640?
This is not what I expected. I converting an old VB 6.0 program to
C#. If I add Thread.Sleep(500) before the ReadExisting() I get the
string I was expecting:
[9/19/2008 4:57:47 PM]10??I 1 1909081657?
The first result looks good. I don't know, what device you are querying,
but your string has obviously an explicit start and end symbol. Just
read it in a loop as long as your string is not complete.
mfG
--stefan <--
hi,
Stefan Hoffmann wrote:
The first result looks good. I don't know, what device you are querying,
but your string has obviously an explicit start and end symbol. Just
read it in a loop as long as your string is not complete.
Better than a loop (i'm using <as start/end tags):
private string _Data = null;
private void _port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
string data = _port.ReadExisting();
if (_Data == null && data.StartsWith("<"))
{
_Data = data;
data = null;
}
if (_Data != null && data != null)
_Data += data;
if (_Data != null)
if (_Data.EndsWith(">"))
{
_log.LogToFile(_Data, true);
_Data = null;
}
}
Depending on the kind of device, you must consider that you get strings
like "789><123897"
mfG
--stefan <--
I tried that. There is still only 8 bytes available at a time. The
only solution I have found so far is to add the sleep. I don't want
to do that because then I have to make the sleep long enough to get
the larger messages but not so large any timeouts will be triggered.
On Sep 25, 9:51*am, Stefan Hoffmann <stefan.hoffm...@explido.de>
wrote:
hi,
Stefan Hoffmann wrote:
The first result looks good. I don't know, what device you are querying,
but your string has obviously an explicit start and end symbol. Just
read it in a loop as long as your string is not complete.
Better than a loop (i'm using <as start/end tags):
private string _Data = null;
private void _port_DataReceived(object sender,
* *SerialDataReceivedEventArgs e)
{
* *string data = _port.ReadExisting();
* *if (_Data == null && data.StartsWith("<"))
* *{
* * *_Data = data;
* * *data = null;
* *}
* *if (_Data != null && data != null)
* * *_Data += data;
* *if (_Data != null)
* * *if (_Data.EndsWith(">"))
* * *{
* * * *_log.LogToFile(_Data, true);
* * * *_Data = null;
* * *}
}
Depending on the kind of device, you must consider that you get strings
like "789><123897"
mfG
--stefan <--
That's interesting. What isn't being displayed in my post is the
start and end character - 0x02 and 0x03.
hi Kevin, Ke***********@gmail.com wrote:
I tried that. There is still only 8 bytes available at a time.
Try setting another bit (baud) rate for your port.
mfG
--stefan <-- Ke***********@gmail.com wrote:
I have the following method that handles the data received event:
private void _port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
string data = _port.ReadExisting();
_log.LogToFile(data, true);
}
The LogToFile function logs data to a file. The second parameter
indicate if the file should timestamp the message.
On the initial test of this method I got the following result:
[9/19/2008 4:40:04 PM]10??I 1
[9/19/2008 4:40:04 PM] 1909081
[9/19/2008 4:40:04 PM]640?
This is not what I expected. I converting an old VB 6.0 program to
C#. If I add Thread.Sleep(500) before the ReadExisting() I get the
string I was expecting:
[9/19/2008 4:57:47 PM]10??I 1 1909081657?
Notice in the first try it is sending 8 bytes at a time. Is there
anyway to avoid using Thread.Sleep to achieve this?
If you know the minimum length of an incoming message, then you could change the
ReceivedBytesThreshold property of the SerialPort. Or, if your incoming message
contains a known end byte that you can synch on, you could use the ReadTo()
method.
HTH,
-rick-
I cannot change the baud rate, I have to match the machine, right?
The minimum number of characters could be one. The machine sends an
ACK once and a while to see if anything is listening. This is
receiving data from a medical device. There is a comments field that
can be filled out and that will be in the data stream
I said VB but that was my mistake. It's a service that uses Vb to
manage it. The code that monitors the serial port is C++. here's the
C++ code:
if(ReadFile(hComm,buf,4096,&dwBytesTransferred,NUL L) == TRUE)
That will read the entire stream into the buffer.
Someone suggested to me to use SerialPort.ReadLine. None of these
make a difference. First, there may be new line characters in the
data stream that is part of the data. I've tried all of the read
functions, the program is only getting 8 bytes at a time unless I put
a sleep in there.
I have even tried something like
while (_port.BytesToRead 0)
{
iRead = _port.Read(buffer, iRead, _port.BytesToRead);
iTotal += iRead;
}
Only the first 8 bytes unless I put a sleep in the loop. A sleep
probably isn't bad, it just has to be large enough that on slower
machines it will still get all the data into the buffer but not so
long that it causes any timeouts.
I think I have to do what Stefan suggested if I want to be safe.
BTW, thanks for the responses. I tried to get a response on the msdn
forum but apparently my question was off topic for the general C#
forum. Ke***********@gmail.com wrote:
On Sep 25, 9:51 am, Stefan Hoffmann <stefan.hoffm...@explido.de>
wrote:
>hi,
Stefan Hoffmann wrote:
>>The first result looks good. I don't know, what device you are querying, but your string has obviously an explicit start and end symbol. Just read it in a loop as long as your string is not complete.
Better than a loop (i'm using <as start/end tags):
private string _Data = null; private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { string data = _port.ReadExisting();
if (_Data == null && data.StartsWith("<")) { _Data = data; data = null; } if (_Data != null && data != null) _Data += data; if (_Data != null) if (_Data.EndsWith(">")) { _log.LogToFile(_Data, true); _Data = null; }
}
Depending on the kind of device, you must consider that you get strings like "789><123897"
mfG --stefan <--
That's interesting. What isn't being displayed in my post is the
start and end character - 0x02 and 0x03.
Then, again I will suggest ReadTo(). It looks as if your data may consist of a
fixed length ASCII string bracketed by start and end framing values. You could
use ReadTo() to read up to the next start byte, as per
string startPattern = Encoding.ASCII.GetString(new byte[]{2});
string input, junk;
while (1)
{
/*
Read previous previous input through end byte
(should be 20 bytes as per your example). Then
read and discard the start byte you just found.
*/
input = myPort.ReadTo(startPattern);
junk = myPort.ReadByte(); //
}
If your data pattern is as simple as you indicate then this simple loop or
something very like it may work for you. You would probably want to add
validation of the start and end bytes plus code to recover your framing if you
happen, e.g., to get a short input string.
HTH,
-rick- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nathan Ie |
last post by:
Hi there!
Not sure how many of you out there are familiar with the Serialport Class,
but I'm trying to add an event handler for every time I receive new incoming
data from the serial port by...
|
by: Nathan Ie |
last post by:
Hello all!
I was wondering if any of you could shed some light on this problem I cant
seem to figure out...
I try to add an event handler to my SerialPort variable by doing:
...
|
by: LordHog |
last post by:
Hello all,
I would like to use the new SerialPort class in Visual C++ 2005
Express edition, but I am having problems adding my event handler to
the DataReceived event. In the header file I have...
|
by: Andrea Judge |
last post by:
Hi there,
I'm developing an application to catch OBD sensors' data from my car.
I connect it via a RS232 interface.
Now.
with HyperTerminal I can connect and send/receive data, but I'd like to...
|
by: Jean Paul Mertens |
last post by:
Hello,
Someone can tell me why I dont get serial port events in a Service, I
created a separate Thread to open the port but no events are coming up (the
same happens when I use the timer...
|
by: Simon |
last post by:
Hi all,
I'm writing a PocketPC / CE application which communicates with a
Bluetooth GPS reciever that is paired to a serial port but I'm
experiencing odd behaviour from the CF 2.0 SerialPort...
|
by: Adriano |
last post by:
Hello,
I'm developing an application in VB.NET 2005 that communicates with a device
through RS232,
and need to send the following sequence of hexadecimal data to the device:
0xFF, 0x01, 0xC3,...
|
by: Lars Siden |
last post by:
Hi,
I've really struggled with this. Using Hyperterminal everything works fine.
But if I write my own program I can't get "OnDataReceived" to be fired.
MS samples from the SDK gives me the...
|
by: cronusf |
last post by:
I set up two virtual COM ports 3 and 4 using com0com. I tried to test
it with the following program. However, the DataReceived event
handlers never get called. Can anyone with SerialPort class...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |