我正在尝试使用 c 中的 unistd.h Linux 函数写入 MAC OSX 上的蓝牙设备。我连接正常并成功写入了前几个字节。当我尝试向它写入其他命令时(每 15 毫秒向写入缓冲区添加一个字节),即使 write() 函数返回 1(写入成功),我也看不到任何结果。
如果您开始写入,但在您尝试开始另一次写入时它还没有完成(因为它是非阻塞的),这是否可能会搞砸初始写入? (如果是这样,有什么方法可以检查写入是否已完成?)这是我唯一能想到的事情,因为写入发生得相当频繁,并且前两个已成功发送。
qwbyte() 简单地向输出数组添加一个字节并增加其长度
开放端口函数:
BAMid = -1;
struct termios options;
struct termios originalTTYAttrs;
// Open the serial port read/write, nonblocking, with no controlling terminal, and don't wait for a connection.
BAMid = open(strPath, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (BAMid == -1)
{
printf("Error opening serial port %s - %s(%d).\n",
strPath, strerror(errno), errno);
goto error;
}
// Issue TIOCEXCL ioctl to prevent additional opens except by root-owned processes.
if (ioctl(BAMid, TIOCEXCL) == -1)
{
printf("Error setting TIOCEXCL on %s - %s(%d).\n",
strPath, strerror(errno), errno);
goto error;
}
// Get the current options and save them so we can restore the default settings later.
if (tcgetattr(BAMid, &originalTTYAttrs) == -1)
{
printf("Error getting tty attributes %s - %s(%d).\n",
strPath, strerror(errno), errno);
goto error;
}
// The serial port attributes such as timeouts and baud rate are set by modifying the termios
// structure and then calling tcsetattr() to cause the changes to take effect. Note that the
// changes will not become effective without the tcsetattr() call.
options = originalTTYAttrs;
// Set raw input (non-canonical) mode, with reads blocking until either a single character
// has been received or a one second timeout expires. [should be moot since we are leaving it as nonblocking]
cfmakeraw(&options);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 10;
cfsetspeed(&options, B57600); // Set 57600 baud
options.c_cflag |= CS8; // Use 8 bit words
// Cause the new options to take effect immediately.
if (tcsetattr(BAMid, TCSANOW, &options) == -1)
{
printf("Error setting tty attributes %s - %s(%d).\n",
strPath, strerror(errno), errno);
goto error;
}
//flush old transmissions
if (tcflush(BAMid,TCIOFLUSH) == -1) {
printf("Error flushing BAM serial port - %s(%d).\n",
strerror(errno), errno);
}
oBufLength = 0;
// Ask it to start
if (! qwbyte(CmdStart) ) {
goto error;
}
if (! qwbyte(CmdFull) ) {
goto error;
}
//this transmit works
txbytes();
printf("success opening port!");
return -1;
// Failure path
error:
if (BAMid != -1) {
close(BAMid);
}
printf("returning an error--%d",errno);
return errno;
}
写入函数(txbytes):
int i, bufSize, numBytes;
if(oBufLength != 0) { //if the output array isn't empty
//duplicating the output array and its size so it can
//be overwritten while this write is occuring
printf("about to transmit: ");
for(i = 0; i < oBufLength; i++) {
printf(" %u",oBuf[i]);
tempBuf[i] = oBuf[i];
}
printf("\n");
bufSize = oBufLength;
oBufLength = 0;
numBytes = write(BAMid, &tempBuf, bufSize);
printf("bytes written = %d\n",numBytes);
if (numBytes == -1) {
printf("Error writing to port - %s(%d).\n", strerror(errno), errno);
}
return (numBytes > 0);
}
else {
return 0;
}
最佳答案
非阻塞写入不会像您期望的那样工作。
如果不能立即完成写入,write() 会返回到您的代码 - 但它不会继续尝试在后台发送数据。它所做的只是说“我现在不能写 - 稍后再试”。它通过返回 -1 并将 errno 设置为 EAGAIN 来执行此操作。
另外,请记住,当成功时,write() 返回成功写入的字节数。因此,如果您在请求写入 2 个字节时得到的返回值为 1,则意味着它仅部分成功,您需要在某个时间再次调用 write() 以获取第二个字节。
基本上,如果您使用非阻塞 IO,您希望 txbytes() 函数在循环中调用 write() 直到缓冲区为空或返回 -1。如果它返回 -1 你需要检查 errno - 如果它是 EAGAIN,你将不得不在其他时间再次调用 write() ;其他任何事情都可能是一个真正的错误。像这样:
ssize_t written = 0;
while (oBufLength > 0 && written > -1)
{
size_t i;
printf("about to transmit %d bytes: ", oBufLength);
for(i = 0; i < oBufLength; i++) {
printf(" %u",oBuf[i]);
}
printf("\n");
written = write(BAMid, oBuf, oBufLength);
printf("Write returned %d\n", written);
if (written > 0)
{
/* The first "written" number of bytes in the buffer have now been sent, so
* we discard them and move up the remaining bytes (if any) to the start of
* the buffer */
oBufLength -= written;
memmove(oBuf, oBuf + written, oBufLength);
printf("Now have %d bytes left to send.\n", oBufLength);
}
}
if (written > -1 || errno == EAGAIN)
{
/* No fatal errors... */
return 0;
} else
/* error left in errno for caller to inspect */
return -1;
}
请注意,无需复制缓冲区 - 因为 write() 不会与您的代码并行执行任何操作。如果它说它写了字节,它就不再需要你的缓冲区了。希望对您有所帮助!
关于c - 在 c 中使用 unistd.h 在 MAC OSX 上写入串行端口的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1094288/