Click here to Skip to main content
15,908,254 members
Home / Discussions / C#
   

C#

 
GeneralRe: serial port and data sequencing problem...? Pin
rahvyn624-Jun-07 12:25
rahvyn624-Jun-07 12:25 
GeneralRe: serial port and data sequencing problem...? Pin
Mir_As24-Jun-07 12:29
Mir_As24-Jun-07 12:29 
GeneralRe: serial port and data sequencing problem...? Pin
rahvyn624-Jun-07 12:34
rahvyn624-Jun-07 12:34 
GeneralRe: serial port and data sequencing problem...? Pin
Mir_As24-Jun-07 12:41
Mir_As24-Jun-07 12:41 
GeneralRe: serial port and data sequencing problem...? Pin
rahvyn624-Jun-07 12:46
rahvyn624-Jun-07 12:46 
GeneralRe: serial port and data sequencing problem...? Pin
Luc Pattyn24-Jun-07 13:11
sitebuilderLuc Pattyn24-Jun-07 13:11 
GeneralRe: serial port and data sequencing problem...? Pin
Mir_As24-Jun-07 13:45
Mir_As24-Jun-07 13:45 
AnswerRe: serial port and data sequencing problem...? Pin
Ed.Poore24-Jun-07 13:33
Ed.Poore24-Jun-07 13:33 
If your micro is sending indivdual characters then in most cases the DataReceived event will be fired the number of times the characters are sent, i.e. you're trying to send 6 characters but by using 6 individual putcs it will fire off 6 DataReceived events.  The best way would be to have a buffer to do something like this:
List<byte> buffer = new List<byte>();
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    buffer.Add(serialPort.ReadByte());
    if (buffer.Length == 6)
    {
        // Call your function here
        YourFunctionCall(buffer.ToArray());
        // Clear the buffer
        buffer.Clear();
    }
}
The problem you mentioned in the other thread about Invoke is basically because Windows is using a different thread to receive data from the SerialPort, this is the one which is the owner of the DataReceived event.  If you want to update for example some textboxes with teh values you receive you will have to do something like this:
private delegate void UpdateTextBoxes(byte[] values);
private void YourFunctionCall(byte[] values)
{
    this.Invoke(new UpdateTextBoxes(this.UpdateTextBoxes));
}
privaet vodi UpdateTextBoxes(byte[] values)
{
    this.txt1.Text = values[0].ToString();
    this.txt2.Text = values[1].ToString();
    // ...
}
You might want to take a look at my article[^] which looks into this problem.



GeneralRe: serial port and data sequencing problem...? Pin
Mir_As24-Jun-07 14:16
Mir_As24-Jun-07 14:16 
GeneralRe: serial port and data sequencing problem...? Pin
Ed.Poore24-Jun-07 22:38
Ed.Poore24-Jun-07 22:38 
GeneralRe: serial port and data sequencing problem...? Pin
Mir_As24-Jun-07 22:57
Mir_As24-Jun-07 22:57 
QuestionHorizontal Scrollbar on Panel Pin
rahvyn624-Jun-07 8:43
rahvyn624-Jun-07 8:43 
QuestionFormChild reference to FormChild in FormMDI Pin
hoangsamac24-Jun-07 7:34
hoangsamac24-Jun-07 7:34 
AnswerRe: FormChild reference to FormChild in FormMDI Pin
Dave Kreskowiak24-Jun-07 12:49
mveDave Kreskowiak24-Jun-07 12:49 
GeneralRe: FormChild reference to FormChild in FormMDI Pin
hoangsamac25-Jun-07 4:07
hoangsamac25-Jun-07 4:07 
GeneralRe: FormChild reference to FormChild in FormMDI Pin
Dave Kreskowiak26-Jun-07 2:39
mveDave Kreskowiak26-Jun-07 2:39 
GeneralRe: FormChild reference to FormChild in FormMDI Pin
hoangsamac27-Jun-07 5:13
hoangsamac27-Jun-07 5:13 
AnswerRe: array of bitmaps Pin
Guffa24-Jun-07 8:05
Guffa24-Jun-07 8:05 
QuestionSome question about add-in Pin
imagic24-Jun-07 5:44
imagic24-Jun-07 5:44 
AnswerRe: Some question about add-in Pin
Jasmine250124-Jun-07 8:32
Jasmine250124-Jun-07 8:32 
GeneralRe: Some question about add-in Pin
DavidNohejl24-Jun-07 8:42
DavidNohejl24-Jun-07 8:42 
GeneralRe: Some question about add-in [modified] Pin
imagic24-Jun-07 19:36
imagic24-Jun-07 19:36 
GeneralRe: Some question about add-in Pin
Jasmine250125-Jun-07 10:38
Jasmine250125-Jun-07 10:38 
GeneralRe: Some question about add-in Pin
imagic27-Jun-07 4:13
imagic27-Jun-07 4:13 
GeneralRe: Some question about add-in Pin
Jasmine250127-Jun-07 8:03
Jasmine250127-Jun-07 8:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.