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