简体   繁体   English

寻找linux串口的测试程序

[英]Looking for test program for linux serial port

I am trying to port a program to linux from windows. 我试图从Windows将程序移植到linux。

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. 使用Windows版本我可以在25微秒的时间看到一个模式但是使用linux版本我可以在250微秒的时间看到相同的模式,告诉我信息是正确的,但它发送的速度很慢。

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. 我正在寻找的是一个程序,它将设置115200波特的串行端口,标记奇偶校验,1个停止位和8个数据位,并在线路上发送一些内容,以便我可以在超级终端上看到它。 A program in c++ would be fantastic because then i could compare it to mine if it works. 用c ++编写的程序会非常棒,因为如果可行,我可以将它与我的程序进行比较。

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. 我认为有一些东西保持波特率以某种方式设置在某个值,如果我从其他人那里得到一个确认可以工作的程序,我可以说它已经完成了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. 我已经尝试了3个星期,已经完成了数百个串口教程,并且是一个Linux noob,我现在在做什么都迷失了。

    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");

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 我的来源: http//www.easysw.com/~mike/serial/serial.html

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. 如果你想将一个Windows串口程序移植到linux,我建议你先迁移到Boost.Asio库 - 这包括一个串口的跨平台接口。 You can get this working under Windows and then know that the code will work on Linux. 你可以在Windows下使用它,然后知道代码可以在Linux上运行。

The reason for this is that there can be other than software issues when moving from windows to linux serial ports. 原因是从Windows移动到Linux串行端口时可能存在软件问题。 For example, the handshaking protocol (RTS/CTS, DTE/DSE) can vary between the two. 例如,握手协议(RTS / CTS,DTE / DSE)可以在两者之间变化。

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 . 如果你想要完整的源代码,那么看看github

Also, this question catologues various serial sniffers, that can help determine exactly what is going on. 此外,这个问题列出了各种串口嗅探器,可以帮助确定究竟发生了什么。

How about the standard tool setserial . 标准工具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. 源代码可用,因此您可以查看它是如何实现的。

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. 但是你可能需要做root才能做这样的事情。 Could you also remove the fcntl call ? 你还可以删除fcntl电话吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM