LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-10-2009, 04:29 PM   #1
morty346
Member
 
Registered: Feb 2009
Posts: 52

Rep: Reputation: 15
Exclamation Well it worked in Windows..... Serial Port Communication Porting


I am working on a serial port communication project and porting over the code to run on linux from windows
Here is the Windows code that works properly(written in c#)
Porting it to c/c++

Code:
sp = new SerialPort(comPort);      
sp.BaudRate = 115200;  
sp.DataBits = 8;      
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);             
sp.Open();
Here is my version on linux
Code:
   
int fd; /* File descriptor for the port */
//set baudrate
        tcgetattr(fd, &options);
        cfsetispeed(&options, B115200);
        cfsetospeed(&options, B115200);
        options.c_cflag |= (CLOCAL | CREAD);
        options.c_cflag &= !CSIZE;
        options.c_cflag |= CS8;
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;
        tcsetattr(fd, TCSANOW, &options);
      
        fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd != -1)
fcntl(fd, F_SETFL, 0);
fd returns 3 //assuming this is good, (the board has two serial ports, I try either ttys0 or ttys1 and get the same number)

Next its time to send a command to my board and see if it responds properly

Windows Version
Code:
     buffer[0] = 0x02;
            buffer[1] = 0x00;
            buffer[2] = 0x30;
            buffer[3] = 0x00;
            buffer[5] = 0x03;

            buffer[4] = (Byte)(buffer[0] ^ buffer[1] ^ buffer[2] ^ buffer[3] ^ buffer[5] ^ 0xEA);

            if (sp != null)
            {
                if (sp.IsOpen)
                    sp.Write(buffer, 0, 6);
            }
New Linux Version
Code:
 int8_t* buffer = new int8_t[6];
    buffer[0] = 0x02;
            buffer[1] = 0x00;
            buffer[2] = 0x30;
            buffer[3] = 0x00;
            buffer[5] = 0x03;

            buffer[4] = (int8_t)(buffer[0] ^ buffer[1] ^ buffer[2] ^ buffer[3] ^ buffer[5] ^ 0xEA);

    int returnval = write(fd, buffer,6);
    return returnval;
returns 6

I am suppose to see the hardware actually respond along with receiving a ACK/NAK back yet I get nothing out of the hardware nor do I receiving a ACK/NAK

My Receive in Linux
Code:
 {
     while(true)
        {
            int val2 = ReadTest(fd);
            if(val2 != 0)
              cout << val2;
            sleep(.001);
        }
}
int ReadTest(int fd)
{
    return fcntl(fd, F_SETFL, FNDELAY);
}
I receive a 0 from ReadTest always



This leads me to beleive that I am actually not opening the correct port or somethign along with opening the port is not correct.
Before i run the program I open the terminal and
type
//Allow me to use the ports
Code:
su -
chmod o+rw /dev/ttyS0
chmod o+rw /dev/ttyS1
//list of ports (if i remove card from the computer i only recieve line one, and not the "com" ports below", so I do KNOW that these are the correct ports to be interfacing with)
Code:
dmesg | grep tty
console [tty0] enabled
0000:02:00.0: ttyS0 at I/O 0xdf00 (irq = 21) is a 16550A
0000:02:00.0: ttyS1 at I/O 0xde00 (irq = 21) is a 16550A

Last edited by morty346; 02-10-2009 at 04:36 PM.
 
Old 02-10-2009, 04:56 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
Try using stty (or copying its ioctls) to configure baudrate etc on your serial port.
 
Old 02-10-2009, 05:14 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Code:
int ReadTest(int fd)
{
    return fcntl(fd, F_SETFL, FNDELAY);
}
I don't think this actually reads anything. From man fcntl
Quote:
F_SETFL
Set the file status flags, defined in <fcntl.h>, for the file description associated with fildes from the corresponding bits in the third argument, arg, taken as type int. Bits corresponding to the file access mode and the file creation flags, as defined in <fcntl.h>, that are set in arg shall be ignored. If any bits in arg other than those mentioned here are changed by the application, the result is unspecified.
...
Upon successful completion, the value returned shall depend on cmd as follows:
...
F_SETFL
Value other than -1.
So the 0 being returned from ReadTest means it succesfully sets the com port fd to no delay mode. I think you should use read.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Porting Serial port program from windows to linux farofeiro Programming 4 01-09-2009 12:40 PM
Serial port communication.. brianbek Linux - Software 2 01-23-2006 01:43 PM
serial port communication prems Linux - Newbie 1 04-17-2005 03:31 AM
serial port communication vidyaraj Linux - Software 6 03-02-2004 07:46 PM
communication via serial port perdesiz Linux - Software 0 11-13-2003 07:23 AM


All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration