2017-12-20 28 views
3

나는 다음과 같은 .NET 코어 2.0 C# 코드가 :이 프로그램은 38400에 USB 직렬 포트의 전송 속도를 변경해야하지만 예상대로 프로그램이 작동하지 않습니다Raspberry PI의 직렬 포트에서 전송 속도를 설정하는 방법은 무엇입니까?

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

합니다.

는 프로그램 실행의 출력입니다 : 나는 또한 이중 직렬 포트 설정을 확인했다

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 에서요?

답변

0

문제는 cfsetspeed 함수의 speed 매개 변수를 선언 할 때 잘못된 데이터 유형 (long)입니다. speed 매개 변수의 데이터 유형은 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}"); 
     } 
    } 
} 
:

이 올바른 코드