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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:08:44+00:00 2026-06-13T01:08:44+00:00

I’ve got a C/Python setup on my machine, I’m doing some testing with serial

  • 0

I’ve got a C/Python setup on my machine, I’m doing some testing with serial communications and for some reason I’m never reading more than 1 byte back.

My set up: I have a windows 7 machine, running OpenSUSE in a virtual box. I have 2 USB-RS232 converters and an adaptor between them (so it’s a loop from one usb port to the other).

On the Windows side I was able to get them to communicate with each other via Python-to-Python, and C-to-Python. Once I use the Linux VM, I can get data from the C (Linux) to the Python (Windows), but when I do it the other way around I only get 1 byte back. I’m thinking it’s something wrong with how I open the file or execute the read on the Linux C code, but I’m not sure what could be the issue.

Python Code (using PySerial):

>>> import serial
>>> ser = serial.Serial(3)
>>> ser
Serial<id=0x2491780, open=True>(port='COM4', baudrate=9600, bytesize=8, 
parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
>>> ser.read(5)
'Hello'
>>> ser.write("hi you")
6L

The C code:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int open_port()
{
    int fd;
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd < 0)
      perror("open_port: Unable to open /dev/ttyUSB0 - ");
    else
      fcntl(fd, F_SETFL, 0);
    return fd;
}

int swrite(int fd, char * str)
{
    int n;
    n = write(fd, str, strlen(str));
    if (n<0)
        printf("write() of %d bytes failed\n", strlen(str));
    return n;
}

int main()
{
    int fd, databytes;
    char buf[100] = {0};
    struct termios options;

    fd = open_port();

    //Set the baud rate to 9600 to match
    tcgetattr(fd, &options);
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    tcsetattr(fd, TCSANOW, &options);
    tcgetattr(fd, &options);

    databytes = swrite(fd, "Hello");
    if(databytes > 0)
      printf("Wrote %d bytes\n", databytes);

    databytes = read(fd, buf, 100);
    if(databytes < 0)
      printf("Error! No bytes read\n");
    else
      printf("We read %d bytes, message: %s\n", databytes, buf);

    close(fd);

    return 0;
}

And I’m getting back:

mike@linux-4puc:~> gcc serial_com.c
mike@linux-4puc:~> ./a.out 
Wrote 5 bytes
We read 1 bytes, message: h

So the Linux->Windows write is working, python is showing the correct “Hello” string, but for some reason I’m only getting one byte back on the Windows->Linux side of things.

Anyone see anything wrong?

EDIT:
Based on the feedback that I’ve gotten, I’ve tried two tweaks to the code. Sounds like I can’t guarantee that all the data will be there, so I’ve tried:

1) a sleep

    if(databytes > 0)
      printf("Wrote %d bytes\n", databytes);
    sleep(15);                 // Hack one to get the data there in time, worked
    databytes = read(fd, buf, 100);

2) a while loop

while(1){  // Hack two to catch the data that wasn't read the first time. Failed
           // this only saw 'h' like before then sat waiting on the read()
  databytes = read(fd, buf, 100);
  if(databytes < 0)
    printf("Error! No bytes read\n");
  else
    printf("We read %d bytes, message: %s\n", databytes, buf);
}

Seems the loop doesn’t work, so does the data not read get trashed??
/EDIT

  • 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-13T01:08:45+00:00Added an answer on June 13, 2026 at 1:08 am

    Got some good answers and incite on this (+1’s all around!), while all of the answers lead to correct conclusions (about read not getting the data), none of the input actually “fixed” the problem I was having.

    Here’s how I got this to work finally:

    The settings in the termios structure were killing me. While I was setting some of the flags, I wasn’t setting all of them. So this:

    tcgetattr(fd, &options);
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    tcsetattr(fd, TCSANOW, &options); 
    

    Changed to this:

    tcgetattr(fd, &options);
    cfsetispeed(&options, B19200);
    cfsetospeed(&options, B19200);
    options.c_iflag = 0; // Disable Input flags
    options.c_lflag = 0; // Disable Local mode flags
    options.c_oflag = 0; // Disable Output flags
    options.c_cflag = (options.c_cflag & ~CSIZE) | CS8; //Data bits per character (8)
    options.c_cc[VMIN] = 50;  // Wait for 50 characters or
    options.c_cc[VTIME] = 50; // Wait for 5 seconds
    options.c_cflag |= (CLOCAL | CREAD | HUPCL);   // Ignore modem status lines, 
                                                   // enable receiver, 
                                                   // and hang up on last close
    options.c_cflag &= ~(PARENB | PARODD); //Clearing even and odd parity
    options.c_cflag &= ~CSTOPB;            //Clear double stop bits
    tcsetattr(fd, TCSANOW, &options);
    

    With these changes now I get the data in my C Linux code that I write from Python.

    I used these two sources for a lot of “facts” about what the options are for the termios structure:

    1. flag description
    2. Quick Overview
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

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.