c# - 如何设置树莓派串口波特率?

标签 c# raspberry-pi serial-port .net-core libc

我有以下 .NET Core 2.0 C# 代码:

using System;
using System.Runtime.InteropServices;

namespace TestSerial
{
    class Program
    {
        [Flags]
        public enum OpenFlags
        {
            O_RDONLY = 0,
            O_WRONLY = 1,
            O_RDWR = 2,

            O_NONBLOCK = 4,
        }

        [DllImport("libc")]
        public static extern int cfsetspeed(byte[] termios_data, long speed);

        [DllImport("libc")]
        public static extern int tcgetattr(int fd, [Out] byte[] termios_data);

        [DllImport("libc")]
        public static extern int tcsetattr(int fd, int optional_actions, byte[] termios_data);

        [DllImport("libc")]
        public static extern int open(string pathname, OpenFlags flags);


        static void Main(string[] args)
        {
            const string serialDevice = "/dev/serial/by-id/usb-ELT_SENSOR_EK100_V1.0_SN000001-if00-port0";

            int fileDescriptor = open(serialDevice, OpenFlags.O_RDONLY | OpenFlags.O_NONBLOCK);
            Console.WriteLine($"Libc.open returned {fileDescriptor}");

            byte[] termiosData = new byte[256];

            int result = tcgetattr(fileDescriptor, termiosData);
            Console.WriteLine($"Libc.tcgetattr returned {result}");

            result = cfsetspeed(termiosData, 38400);
            Console.WriteLine($"Libc.cfsetspeed returned {result}");

            result = tcsetattr(fileDescriptor, 0, termiosData);
            Console.WriteLine($"Libc.tcsetattr returned {result}");
        }
    }
}

程序应该将 USB 串行端口的波特率更改为 38400。但是,程序没有按预期运行。

这是运行程序的输出:

Libc.open returned 18
Libc.tcgetattr returned 0
Libc.cfsetspeed returned -1
Libc.tcsetattr returned 0

我也仔细检查了串口设置,确实没有改变:

$ sudo stty -a -F /dev/serial/by-id/usb-ELT_SENSOR_EK100_V1.0_SN000001-if00-port0
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc

Libc.cfsetspeed 返回错误的原因可能是什么?

最佳答案

问题是 cfsetspeed 函数的速度参数声明中的数据类型(long)不正确。速度参数的数据类型应为 uint。

这是正确的代码:

using System;
using System.Runtime.InteropServices;

namespace TestSerial
{
    class Program
    {
        [Flags]
        public enum OpenFlags
        {
            O_RDONLY = 0,
            O_WRONLY = 1,
            O_RDWR = 2,

            O_NONBLOCK = 4,
        }

        [DllImport("libc")]
        public static extern int cfsetspeed(byte[] termios_data, uint speed);

        [DllImport("libc")]
        public static extern int tcgetattr(int fd, [Out] byte[] termios_data);

        [DllImport("libc")]
        public static extern int tcsetattr(int fd, int optional_actions, byte[] termios_data);

        [DllImport("libc")]
        public static extern int open(string pathname, OpenFlags flags);


        static void Main(string[] args)
        {
            const string serialDevice = "/dev/serial/by-id/usb-ELT_SENSOR_EK100_V1.0_SN000001-if00-port0";

            int fileDescriptor = open(serialDevice, OpenFlags.O_RDONLY | OpenFlags.O_NONBLOCK);
            Console.WriteLine($"Libc.open returned {fileDescriptor}");

            byte[] termiosData = new byte[256];

            int result = tcgetattr(fileDescriptor, termiosData);
            Console.WriteLine($"Libc.tcgetattr returned {result}");

            result = cfsetspeed(termiosData, 38400);
            Console.WriteLine($"Libc.cfsetspeed returned {result}");

            result = tcsetattr(fileDescriptor, 0, termiosData);
            Console.WriteLine($"Libc.tcsetattr returned {result}");
        }
    }
}

关于c# - 如何设置树莓派串口波特率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47912133/

相关文章:

Linux 从串行端口读取 - fork 解决方法

c# - LINQ:获取表列名称

python - 使 python 脚本在 x 秒不活动后退出

mysql - 在 RPI/优化查询上运行 MySQL

Python:serial.readline() - 如何定义从\n到\n\n的EOL

c - 制作文件以写入来自serialPort的所有信息

c# - 在与服务线程不同的线程上运行服务操作

c# - 发送带有附件 WinRT 的电子邮件

c# - 类继承 : recreate base class items (or instance) from a property of the inherited class

linux - 在 Raspberry Pi 上使用 oxfRPiCameraVideoGrabber 时出现问题