[英]write() returns error:invalid argument when using serial port
I am trying to send data using the serial port but the write command always returns -1. 我正在尝试使用串行端口发送数据,但写入命令始终返回-1。
This is the code for the write command. 这是write命令的代码。
int WriteComm( int Comid, void *buf, int nobtw )
{
unsigned long nobw;
nobw = write(Comid, buf, nobtw);
move(10,5);
perror("");
sleep(10);
return nobw;
}
and this is the code that calls it 这是调用它的代码
gnobw = WriteComm(theApp.idComDev[Seg],&head[1],1); //send network address
I am getting invalid argument as the error but after looking on google I cant find anything explaning what this means or how to fix it. 我得到了无效的争论作为错误,但在谷歌上看后,我找不到任何解释这意味着什么或如何解决它。 the closes thing I found was this but it uses st0 not ttyS0 so im not sure if its even the same thing.
我发现关闭的东西是这个,但它使用st0而不是ttyS0所以我不确定它是否甚至相同的东西。
can anyone explain what i am doing wrong to get this error and how to fix it ? 任何人都可以解释我做错了什么来得到这个错误以及如何解决它?
You should only be examining errno
(this includes calling perror()
) if the write
call failed, which it indicates by returning -1. 你应该只检查
errno
(这包括调用perror()
)如果write
调用失败,它通过返回-1表示。 If the write succeeds, it leaves errno
unchanged. 如果写入成功,则会使
errno
保持不变。
In order to test for this you should really be assigning the return value to a variable with a signed type - preferably ssize_t
- not an unsigned long
. 为了对此进行测试,您应该将返回值分配给具有签名类型的变量 - 最好是
ssize_t
- 而不是unsigned long
。
You're getting EINVAL back from write( ). 你从write()获得了EINVAL。 That means one of your arguments to the function is invalid: EINVAL = * E *rror, * INVAL *id argument.
这意味着你对函数的一个参数是无效的:EINVAL = * E * rror ,* INVAL * id参数。 There are three arguments to the function:
该函数有三个参数:
arg your variable
---------------------- -------------
int file descriptor: Comid
void *buf: buf
size_t size: nobtw
write( ) puked when it saw one of those three. 当看到这三个中的一个时,写道()。 So one of those three is wrong.
所以这三个中的一个是错的。
So put a printf( ) before the call to write( ) and see which one (or two; or three) is wrong. 所以在调用write()之前放一个printf()并查看哪一个(或两个;或三个)是错误的。
Where is the actual code (not your memory of the code) that does the open( )? 执行open()的实际代码(不是代码的内存)在哪里? Is the file descriptor returned by open( ) the same one (Comid) you are trying to write( ) onto?
open()返回的文件描述符是否与您尝试写入()的同一个(Comid)? If not, there's your problem.
如果没有,那就是你的问题。
That is the likely error in this mashup. 这是mashup中可能出现的错误。
EINVAL from write(3) means: 来自写(3)的EINVAL意味着:
The STREAM or multiplexer referenced by fildes is linked (directly or indirectly) downstream from a multiplexer. 由fildes引用的STREAM或多路复用器在多路复用器的下游(直接或间接)链接。
What this basically means is that something else has your serial port open for writing at the same time -- at least intermittently. 这基本上意味着还有其他东西让你的串口同时打开 - 至少是间歇性的。 USB to serial converters seem to be particularly vulnerable to this.
USB转串口转换器似乎特别容易受此影响。 Other serial drivers will generally only allow you to open them once.
其他串行驱动程序通常只允许您打开一次。
Source: 资源:
http://linux.die.net/man/3/write http://linux.die.net/man/3/write
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.