Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8252465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:34:22+00:00 2026-06-08T00:34:22+00:00

I’m trying to establish a SerialPort connection which transfers 16 bit data packages at

  • 0

I’m trying to establish a SerialPort connection which transfers 16 bit data packages at a rate of 10-20 kHz. Im programming this in C++/CLI. The sender just enters an infinte while-loop after recieving the letter “s” and constantly sends 2 bytes with the data.

A Problem with the sending side is very unlikely, since a more simple approach works perfectly but too slow (in this approach, the reciever sends always an “a” first, and then gets 1 package consisting of 2 bytes. It leads to a speed of around 500Hz).
Here is the important part of this working but slow approach:

public: SerialPort^ port;

in main:
Parity p = (Parity)Enum::Parse(Parity::typeid, "None");
StopBits s = (StopBits)Enum::Parse(StopBits::typeid, "1");
port = gcnew SerialPort("COM16",384000,p,8,s);
port->Open();

and then doing as often as wanted:
port->Write("a");
int i = port->ReadByte();
int j = port->ReadByte();

This is now the actual approach im working with:

static int values[1000000];
static int counter = 0;

void reader(void)
    {
        SerialPort^ port;
        Parity p = (Parity)Enum::Parse(Parity::typeid, "None");
        StopBits s = (StopBits)Enum::Parse(StopBits::typeid, "1");
        port = gcnew SerialPort("COM16",384000,p,8,s);
        port->Open();
        unsigned int i = 0;
        unsigned int j = 0;
        port->Write("s"); //with this command, the sender starts to send constantly
        while(true)
        {
            i = port->ReadByte();
            j = port->ReadByte();
            values[counter] = j + (i*256);
            counter++;
        }
    }

in main:
Thread^ readThread = gcnew Thread(gcnew ThreadStart(reader));
readThread->Start();

The counter increases (much more) rapidly at a rate of 18472 packages/s, but the values are somehow wrong.
Here is an example:
The value should look like this, with the last 4 bits changing randomly (its a signal of an analogue-digital converter):

111111001100111 

Here are some values of the threaded solution given in the code:

1110011001100111 
1110011000100111 
1110011000100111 
1110011000100111

So it looks like the connection reads the data in the middle of the package (to be exact: 3 bits too late). What can i do? I want to avoid a solution where this error is fixed later in the code while reading the packages like this, because I don’t know if the the shifting error gets worse when I edit the reading code later, which I will do most likely.

Thanks in advance,

Nikolas

PS: If this helps, here is the code of the sender-side (an AtMega168), written in C.

uint8_t activate = 0;

void uart_puti16(uint16_t val) //function that writes the data to serial port
{

    while ( !( UCSR0A & (1<<UDRE0)) ) //wait until serial port is ready
            nop(); // wait 1 cycle
            UDR0 = val >> 8; //write first byte to sending register
    while ( !( UCSR0A & (1<<UDRE0)) ) //wait until serial port is ready
            nop(); // wait 1 cycle
            UDR0 = val & 0xFF; //write second byte to sending register
}

in main:
while(1)
{
        if(active == 1)
        {   
            uart_puti16(read()); //read is the function that gives a 16bit data set
        }               
}

ISR(USART_RX_vect) //interrupt-handler for a recieved byte
{
        if(UDR0 == 'a') //if only 1 single data package is requested
        {
            uart_puti16(read());
        }
        if(UDR0 == 's') //for activating constant sending
        {
            active = 1;
        }
        if(UDR0 == 'e') //for deactivating constant sending
        {
            active = 0;
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:34:23+00:00Added an answer on June 8, 2026 at 12:34 am

    At the given bit rate of 384,000 you should get 38,400 bytes of data (8 bits of real data plus 2 framing bits) per second, or 19,200 two-byte values per second.

    How fast is counter increasing in both instances? I would expect any modern computer to keep up with that rate whether using events or directly polling.

    You do not show your simpler approach which is stated to work. I suggest you post that.

    Also, set a breakpoint at the line

    values[counter] = j + (i*256);
    

    There, inspect i and j. Share the values you see for those variables on the very first iteration through the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.