Reputation: 2542
I am trying to port a program to linux from windows.
The problem is i cannot set the baud rate successfully (code for this can be seen below)
I have set up an oscilloscope on the cable connected to the serial port. with the windows version i can see a pattern at 25micro seconds but with the linux version i can see the same pattern at 250micro seconds telling me that the information is correct but it is sending it to slow.
I have tried setting the baud rate to several different values but i still get the same thing on the oscilloscope.
What i am looking for is a program that will set up the serial port at 115200 baud, mark parity, 1 stopbit and 8 databits and send something across the line so i can see it on hyperterminal. A program in c++ would be fantastic because then i could compare it to mine if it works.
I think there is something keeping the baud rate set at a certain value somehow and if i got a program from someone else that is confirmed to work i could say it is out setup of linux. I have tried on different computers but they are all configured the same way by our sysadmin
I have been trying this for 3 weeks and have done literally hundreds of serial port tutorials and being a linux noob i am lost at what to do now.
idComDev[i] = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (idComDev[i] == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
ret = false;
}
else
{
fcntl(idComDev[i], F_SETFL, 0);
struct termios options;
tcgetattr(idComDev[i], &options); // get current settings
cfsetspeed(&options, B9600); // set baud rate
test = tcsetattr(idComDev[i], TCSANOW, &options);// save the settings
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS8; // 8 bit data
options.c_cflag &= ~PARENB; // set parity to no
options.c_cflag &= ~PARODD; // set parity to no
options.c_cflag |= CSTOPB; //set one stop bit
options.c_cflag |= (CLOCAL | CREAD);
options.c_oflag &= ~OPOST;
options.c_lflag &= 0;
options.c_iflag &= 0; //disable software flow controll
options.c_oflag &= 0;
tcsetattr(idComDev[i], TCSANOW, &options);// save the settings
printw("Seg %d = COM%hd",i,CommNo[i]);
if(test!= -1)
printw("test success");
Upvotes: 0
Views: 3280
Reputation: 11
So, I know this question was posted a long time ago, but I think you have your stop bit backwards. If you want to set one stop bit, you have to do this:
options.c_cflag &= ~CSTOPB; //set one stop bit
Not this (what you had):
options.c_cflag |= CSTOPB; //This sets stop bits to 2, since 1 is the default.
My source: http://www.easysw.com/~mike/serial/serial.html
Upvotes: 1
Reputation: 5309
If you want to port a windows serial port program to linux, I suggest you migrate first to Boost.Asio library - this includes a cross platform interface to your serial port. You can get this working under Windows and then know that the code will work on Linux.
The reason for this is that there can be other than software issues when moving from windows to linux serial ports. For example, the handshaking protocol (RTS/CTS, DTE/DSE) can vary between the two.
For a start with this, have a look at my answer here. If you want the full source code then take a look at github.
Also, this question catologues various serial sniffers, that can help determine exactly what is going on.
Upvotes: 1
Reputation: 15436
You could try :
getty ttyS0 115200 vt100
and see if a login prompt appears on your windows. Once you have this working for the default baudrate, try changing the baudrate.
getty ttyS0 9600 vt100
And see if it still works. You probably need to be root to do such a thing however. Could you also remove the fcntl call ?
Upvotes: 0
Reputation: 15526
How about the standard tool setserial
. You can set the device parameters with it (in-/output can then be done via the device file). The source code is available, so you can check out how it is implemented.
Upvotes: 0