Serial port read/write problem

This is a discussion on Serial port read/write problem within the C Programming forums, part of the General Programming Boards category; Hi all, I use the following code in order to read from a serial port based on the command I ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    10

    Serial port read/write problem

    Hi all,

    I use the following code in order to read from a serial port based on the command I write there first.

    Code:
    write(*dev_fd,pwise_command,PWISE_COMMAND_SIZE);
    int p_devbytes = 1;
    while(p_devbytes)
    {
    	FD_SET(*dev_fd,&fdsread);
    	fdsreaduse = fdsread;
    	q.tv_sec = 5;
    	q.tv_usec = 0;
    	sel_res = select((*dev_fd+1),&fdsreaduse,NULL,NULL,&q); 
    	if (sel_res == -1 || sel_res == 0) {
    		break;
    	}
    	else if(FD_ISSET(*dev_fd,&fdsreaduse))
    	{
    		memset(&p_devdata[0], 0, sizeof(p_devdata));
    		p_devbytes = (int)read(*dev_fd,p_devdata,128);	
    		strcat(p_devdatabuffer,p_devdata);
    	}else
    		FD_ZERO(&fdsread);
    }
    printf("DATA:%s\n",p_devdatabuffer);
    Now, everything works fine the first time around for write/read cycle, however when my program does it for second request I only get the first line of the response after which the serial port does not respond anymore.

    I run this code on an embedded controller with Midge linux. It seems to run fine when running it on my laptop, however when I cross compile it for the embedded controller I get the above problem.

    Now the question. I am not that experienced C programmer, so would someone be able to tell me if the above problem might be:

    1) issue with my code
    2) issue with the serial port configuration on the embedded controller (this is a USB port by the way if that makes any difference)
    3) issues with Midge OS


    Really stuck with this so any advice/ideas greatly appreciated.

    Regards,

    Olli

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    You say it runs fine on your laptop, is the port on your laptop a serial port or a usb? Just because both names contain serial does not mean they are anything alike at the system programming level. Serial ports are as simple as reading a port, usbs on the other hand require you to master a whole protocol to interact with the device.

    Would you mind attaching the whole program? I'm really interested in how one would go about something like this.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    10
    It is USB port on both. The differences between the systems are:

    - Laptop runs intel processor with Ubuntu while embedded controller has AMD with Midge/OpenWrt.

    - Laptop has a lot more memory

    As far as the interaction with USB ports is concerned I thought it was just like with serial ports? You open the port (for example: /dev/ttyUSB0) and then just write and read to it?

    Yes there is the protocol to handle when communicating with the device, but that is still wrapped with standard write and read.

    It is a large program, so too big to attach here as whole, but the above code is for the serial port communication part. Other than that the following code is relevant (when opening the port initially):

    Code:
    int dev_fd = open("/dev/ttyUSB0",O_RDWR);
    if(dev_fd>0){ 
    ...
    ...
    What else would you like to see?

    Olli

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    How are you setting the port parameters? If the USB is emulating a serial port you must set the port up before using
    it. Also is the embedded controller using a USB host or slave?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    10
    I am using 3rd party stty library for setting the port. That should be ok since it works on my laptop.

    Not sure what is the answer to your host/slave question, but this is what dmesg show on start up:

    Code:
    hub.c: new USB device adm5120-hcd-2, assigned address 3
    usbserial.c: FTDI FT232BM Compatible converter detected
    usbserial.c: FTDI FT232BM Compatible converter now attached to ttyUSB1 (or usb/tts/1 for devfs)
    Just noticed that dmesg displays the following when the problem occurs. This has probably something to do with it.

    Code:
    td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!! 
     td_fill_ahci: Error ~~~~~~!!

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    The error messages shown by dmseg is probably your problem. I would post a question on the Midge forum as this may be a bug in their usb drivers.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port receive Buffer help
    By Daffodils in forum C Programming
    Replies: 1
    Last Post: 07-23-2009, 08:47 AM
  2. Serial Port - Problem with binary frames
    By estratos in forum Linux Programming
    Replies: 2
    Last Post: 01-18-2006, 05:22 AM
  3. serial port communication from two Application
    By lsme in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-12-2005, 10:32 PM
  4. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 06:07 PM
  5. Weired serial port problem
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2003, 04:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21