Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial communication between linux and windows

Tags:

c++

c

linux

windows

I am sending data bytes from linux to windows in serial RS232 then everything is ok, only i have to handle 0xa send from linux, because windows read it as 0xd + 0xa. but when i am sending data bytes from windows to linux some bytes are replaced as - windows send - 0xd linux receive 0xa windows send - 0x11 linux receive garbage tyte value in integer something 8200

plese explain what goes wrong when I send data from windows to Linux. thanks in advance

Windows serial port initialize

char *pcCommPort = "COM1";
    hCom = CreateFile( TEXT("COM1"),
                       GENERIC_READ | GENERIC_WRITE,
                       0,    // must be opened with exclusive-access
                       NULL, // no security attributes
                       OPEN_EXISTING, // must use OPEN_EXISTING
                       0,    // not overlapped I/O
                       NULL  // hTemplate must be NULL for comm devices
                       );
fSuccess = GetCommState(hCom, &dcb);
 FillMemory(&dcb, sizeof(dcb),0);


    dcb.DCBlength = sizeof(dcb);
    dcb.BaudRate = CBR_115200;     // set the baud rate
    dcb.ByteSize = 8;             // data size, xmit, and rcv
    dcb.Parity = NOPARITY;        // no parity bit
    dcb.StopBits = ONESTOPBIT;    // one stop bit
    dcb.fOutxCtsFlow = false;

    fSuccess = SetCommState(hCom, &dcb);
 buff_success = SetupComm(hCom, 1024, 1024);
COMMTIMEOUTS cmt;
    // ReadIntervalTimeout in ms
    cmt.ReadIntervalTimeout = 1000;
    cmt.ReadTotalTimeoutMultiplier = 1000;
    cmt.ReadTotalTimeoutConstant=1000;
    timeout_flag = SetCommTimeouts(hCom, &cmt);

windows write serial-

WriteFile(hCom, buffer, len, &write, NULL);

Linux serial initialize-

_fd_port_no = open("//dev//ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
tcgetattr(_fd_port_no, &options);
        cfsetispeed(&options, B115200);
        cfsetospeed(&options, B115200);
        options.c_cflag |= (CS8);
        options.c_cflag|=(CLOCAL|CREAD);
        options.c_cflag &=~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        options.c_iflag |= (IXON | IXOFF | IXANY);
        options.c_cflag &= ~ CRTSCTS;
        tcsetattr(_fd_port_no, TCSANOW, &options);

read serial linux-

while(read(_fd_port_no,buffer+_buffer_len,sizeof(buffer))>0)
    {
        _buffer_len = _buffer_len+sizeof(buffer);

    }

Yes, as i told from Linux to windows only NL/CR problem detected but i solved it by byte replacing, but do you have any idea about serila data send from windows to Linux (byte replacement policy). Actually I have to send 200 KB file in 200 bytes blocks over serial so which byte could be replaced if send from Windows to Linux

like image 200
pritesh Avatar asked Sep 11 '26 21:09

pritesh


1 Answers

If you are using the ReadFile and WrietFile on Windows and read and write in Linux, it shouldn't really matter what the line-endings are, other than "you have to translate it at some point after receiving it.

This doesn't look right:

while(read(_fd_port_no,buffer+_buffer_len,sizeof(buffer))>0)
{
    _buffer_len = _buffer_len+sizeof(buffer);

}

You should take into account the size of the read returned by read.

And if sizeof(buffer) is the actual buffer you are reading into, adding +_buffer_len, when _buffer_len >= sizeof(buffer) will write outside the buffer.

Also slightly worried about this:

    options.c_iflag |= (IXON | IXOFF | IXANY);
    options.c_cflag &= ~ CRTSCTS;

Are you SURE you want a XOFF/CTRL-S (0x13) to stop flow? Usually that means that data with CTRL-S in it won't be allowed - which may not be an issue when sending text data, but if you ever need to send binary data it certainly will be. IXOFF also means that the other end will have to respond to XOFF and XON (CTRL-Q, 0x11) to stop/start the flow of data. Typically, we don't want this in modern systems....

Using RTS/CTS should be safe if you have the wiring correct between the two ends.

like image 188
Mats Petersson Avatar answered Sep 13 '26 11:09

Mats Petersson