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 6706501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:31:40+00:00 2026-05-26T07:31:40+00:00

I have an Arduino hooked up via usb to my computer. From it I

  • 0

I have an Arduino hooked up via usb to my computer. From it I try to read the from the COM port.

Can anyone see if theres anything blatantly wrong with this???

void main()
{   
    int exitStatus;
    unsigned int bytesToRead = 1;
    unsigned char *buffer = (unsigned char*) malloc(sizeof(unsigned char) + 1);
    Serial *connection = new Serial("COM3");

    if(connection->IsConnected()){
        exitStatus=connection->ReadData(buffer, bytesToRead);
        if( *buffer > 0)
            <statement I'm trying to hit>
    }

}    

Right now it always hits the ‘statement I’m trying to hit’ even when it’s no supposed to. Debugging it always shows that the contents of buffer is a lot of junk. I know what’s coming in from the serial input should be right because from what I’m seeing in the serial monitor, it all looks good.

Thoughts?

  • 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-05-26T07:31:41+00:00Added an answer on May 26, 2026 at 7:31 am

    You are not evaluating the exitStatus variable, which might indicate it the read was successful.

    Also you don’t need to malloc memory if you want to read just one byte, you can simply pass the pointer to a local char variable.

    And while I’m at it, the type for main is either int main() or int main(int argc, char** argv)

    int main()
    {   
        int exitStatus;
        unsigned int bytesToRead = 1;
        unsigned char buffer;
        Serial *connection = new Serial("COM3");
    
        if(connection->IsConnected()){
            exitStatus=connection->ReadData(&buffer, 1);
            if((exitStatus == <Insert the value for a correct read status>) && (buffer != '0'))
                <statement I'm trying to hit>
        }
        return 0;
    }
    

    update
    Changed the != 0 check to != ‘0’, since I suspect that there is a ‘0’ character (=0x30) comming from the serial interface.

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

Sidebar

Related Questions

I have an Arduino which I have coded to read from a USB serial
I have an Arduino Uno connected to a PC via USB and I am
I'm trying to read serial data from my Arduino using Java. I have followed
I have an Arduino project where I read data from a webserver. I have
I have an Arduino board connected to my PC's serial port. The board sends
I have a problem automagically detecting my Arduino's serial port in Python, using Mac/Linux.
I am fetching data from the following database: I have arduino-box that send that
I have few projects ideas that involve plugging a computer or an arduino to
I would love to upload my arduino sketches via bluetooth, but I have no
I have an Arduino connected to my computer running a loop, sending a value

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.