基本上我使用下面的代码来设置串口的波特率:
struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); tcsetattr(fd, TCSANOW, &options);
这工作得很好。 但是,我知道我必须与使用波特率307200的设备进行通信。我该如何设置? cfsetispeed(&options, B307200);
不起作用,没有定义B307200
。
我使用MOXA Uport 1150(这实际上是一个USB转串口转换器)和一个Intel主板的标准串行端口。 我不知道后者的确切types,setserial只是报告为16550A。
Linux对于非标准的波特率使用一种很脏的方法,称为“波特率混叠”。 基本上,您告诉串行驱动程序以不同的方式解释值B38400
。 这是通过serial_struct
成员flags
的ASYNC_SPD_CUST
标志来控制的。
您需要手动计算自定义速度的除数,如下所示:
// configure port to use custom speed instead of 38400 ioctl(port, TIOCGSERIAL, &ss); ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST; ss.custom_divisor = (ss.baud_base + (speed / 2)) / speed; closestSpeed = ss.baud_base / ss.custom_divisor; if (closestSpeed < speed * 98 / 100 || closestSpeed > speed * 102 / 100) { sprintf(stderr, "Cannot set serial port speed to %d. Closest possible is %d\n", speed, closestSpeed)); } ioctl(port, TIOCSSERIAL, &ss); cfsetispeed(&tios, B38400); cfsetospeed(&tios, B38400);
当然,你需要一个具有合适的baud_base
和divisor设置的串行驱动程序。 上面的代码允许有2%的偏差,对大多数用途来说应该是可以的。
并告诉驱动程序再次将B38400
解释为38400波特:
ioctl(mHandle, TIOCGSERIAL, &ss); ss.flags &= ~ASYNC_SPD_MASK; ioctl(mHandle, TIOCSSERIAL, &ss);
作为一个谨慎的话:我不确定这种方法是否可以在其他* nix风格之间移植。
对速度的支持取决于系统。 如果B307200
没有定义,那么你的系统可能不支持它。
以下是stty.c的源代码: http : //www.koders.com/c/fid35874B30FDEAFEE83FAD9EA9A59F983C08B714D7.aspx
你可以看到所有的高速变量都是#ifdef'd,因为对这些速度的支持因系统而异。
USB谈判也有类似的问题。 我找到了你可能会用到的这个答案:
struct serial_struct ser_info; ioctl(ser_dev, TIOCGSERIAL, &ser_info); ser_info.flags = ASYNC_SPD_CUST | ASYNC_LOW_LATENCY; ser_info.custom_divisor = ser_info.baud_base / CUST_BAUD_RATE; ioctl(ser_dev, TIOCSSERIAL, &ser_info);
在许多操作系统中,枚举值在数值上等于波特率。 所以只需跳过宏/枚举并传递你想要的波特率,例如
cfsetispeed(&options, 307200);
当然,你应该检查返回代码,以确保这个技巧实际上工作,而且并非所有的波特率都支持所有的UART。
您也可以尝试使用TIOCGSERIAL
和TIOCSSERIAL
ioctl代码在struct serial_struct中设置选项。
我使用termios2
和ioctl()
命令完成了这个任务。
struct termios2 options; ioctl(fd, TCGETS2, &options); options.c_cflag &= ~CBAUD; //Remove current BAUD rate options.c_cflag |= BOTHER; //Allow custom BAUD rate using int input options.c_ispeed = 307200; //Set the input BAUD rate options.c_ospeed = 307200; //Set the output BAUD rate ioctl(fd, TCSETS2, &options);
之后,您应该能够查询端口设置,并查看您的自定义波特率,以及其他设置(可能与stty
命令)。
尝试ioctl调用 – 您可以指定一个任意的波特率。 那是,
ioctl(serialFileDescriptor,IOSSIOSPEED,&baudRate);
打开串口:
// Open the serial like POSIX C serialFileDescriptor = open( "/dev/tty.usbserial-A6008cD3", O_RDWR | O_NOCTTY | O_NONBLOCK ); // Block non-root users from using this port ioctl(serialFileDescriptor, TIOCEXCL); // Clear the O_NONBLOCK flag, so that read() will // block and wait for data. fcntl(serialFileDescriptor, F_SETFL, 0); // Grab the options for the serial port tcgetattr(serialFileDescriptor, &options); // Setting raw-mode allows the use of tcsetattr() and ioctl() cfmakeraw(&options); // Specify any arbitrary baud rate ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate);
从串口读取:
// This selector will be called as another thread - (void)incomingTextUpdateThread: (NSThread *) parentThread { char byte_buffer[100]; // Buffer for holding incoming data int numBytes=1; // Number of bytes read during read // Create a pool so we can use regular Cocoa stuff. // Child threads can't re-use the parent's autorelease pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // This will loop until the serial port closes while(numBytes>0) { // read() blocks until data is read or the port is closed numBytes = read(serialFileDescriptor, byte_buffer, 100); // You would want to do something useful here NSLog([NSString stringWithCString:byte_buffer length:numBytes]); } }
要写入串口:
uint8_t val = 'A'; write(serialFileDescriptor, val, 1);
列出可用的串行端口:
io_object_t serialPort; io_iterator_t serialPortIterator; // Ask for all the serial ports IOServiceGetMatchingServices( kIOMasterPortDefault, IOServiceMatching(kIOSerialBSDServiceValue), &serialPortIterator); // Loop through all the serial ports while (serialPort = IOIteratorNext(serialPortIterator)) { // You want to do something useful here NSLog( (NSString*)IORegistryEntryCreateCFProperty( serialPort, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0)); IOObjectRelease(serialPort); } IOObjectRelease(serialPortIterator);