基本上我使用以下代码设置串口的波特率:
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转串口转换器)和英特尔主板的标准串口。我不知道后者的确切类型,setserial只是将其报告为16550A。
答案 0 :(得分:24)
Linux对非标准波特率使用脏方法,称为“波特率别名”。基本上,您告诉串行驱动程序以不同方式解释值B38400
。这是通过ASYNC_SPD_CUST
成员serial_struct
中的flags
标记来控制的。
您需要手动计算自定义速度的除数,如下所示:
// 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
和除数设置的串行驱动程序。前面的代码段允许2%的偏差,这对于大多数用途应该是可以的。
告诉司机再次将B38400
解释为38400波特:
ioctl(mHandle, TIOCGSERIAL, &ss);
ss.flags &= ~ASYNC_SPD_MASK;
ioctl(mHandle, TIOCSSERIAL, &ss);
谨慎之处:我不确定这种方法是否可以在其他* nix风格之间移植。
答案 1 :(得分:2)
支持该速度取决于系统。如果未定义B307200
,那么您的系统可能不支持它。
以下是stty.c的源代码:http://www.koders.com/c/fid35874B30FDEAFEE83FAD9EA9A59F983C08B714D7.aspx
您可以看到所有高速变量都是#ifdef'd,因为对这些速度的支持因系统而异。
答案 2 :(得分:2)
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);
答案 3 :(得分:1)
在许多操作系统上,枚举值在数值上等于波特率。 所以只需跳过宏/枚举并传递你想要的波特率,例如
cfsetispeed(&options, 307200);
当然你应该检查返回代码以确保这个技巧真正起作用,而且并非所有UART都支持所有波特率。
您还可以尝试使用TIOCGSERIAL
和TIOCSSERIAL
ioctl代码在struct serial_struct中设置选项。
答案 4 :(得分:1)
我使用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);
之后,您应该能够查询端口设置并查看自定义BAUD速率以及其他设置(可能使用stty
命令)。
答案 5 :(得分:-2)
尝试ioctl调用 - 您可以指定任意波特率。也就是说,
ioctl(serialFileDescriptor,IOSSIOSPEED,&amp; 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);