Accept Solution Reject Solution. A quick scan of your code suggests that you are setting it up to 57, baud and commenting that as 9, baud - one of those is wrong! And probably the parity as well I'd suggest that you use something like Hyperterminal to check your communications and make sure that you have exactly the right port settings before you leap into code - otherwise you are trying to debug everything at the same time with no idea of what is working so far.
When you have it working via hyperterminal, manually send a "known" command to the device, and capture its response.
Then set your code up to do exactly the same thing, send the same command, and look carefully at the response before moving on to trying anything more advanced. Posted 4-Aug am OriginalGriff. Member 4-Aug pm. Yeah, i definitely got the port settings right, the is a wrong comment. I write strings to the device to change its settings and it works, so now that I am writing code to it to send back data is where the issue is. OriginalGriff 4-Aug pm. Never leave wrong comments!
They last longer than the damn code does Posted 4-Aug am CPallini. I am expecting the reply from the device to be in such format - 0. CPallini 4-Aug pm. What is the output, instead? Member 5-Aug am. If you use the zero-out method, then you will experience unexplained intermittent failures, especially on the BSDs and OS X.
Setting no blocking means that a read returns however many characters are available without waiting for more, up to the buffer limit. For most applications, it can be omitted. That definition occurs in features. It's essentially derived from the other answer, but inaccurate and misleading comments have been corrected. This demo program opens and initializes a serial terminal at baud for non-canonical mode that is as portable as possible.
The program transmits a hardcoded text string to the other terminal, and delays while the output is performed. The program then enters an infinite loop to receive and display data from the serial terminal. By default the received data is displayed as hexadecimal byte values. If the received data is ASCII text rather than binary data and you want to read it as lines terminated by the newline character, then see this answer for a sample program.
For an example of an efficient program that provides buffering of received data yet allows byte-by-byte handing of the input, then see this answer. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to open, read, and write from serial port in C? This 3rd application makes the complete serial hanling read and write. The other two applications communicate via IPC with this 3rd application and get the data read from the serial port or send the date that should be written on the serial port.
At all I have only done overlapped serial communication in serveral theads, not applications. Seems I was wrong when I said overlappes IO will work with multiple applications. Thats what is generally done. But if you have two com ports in your computer just connect them together with correct crossover cable. Should work reasonably well in your case. Since you wrote the "reading application can not be changed, you will not have the chance to add an "writing part" since the port is locked like Scott said.
What you could do is adding a serial port and "bypassing" the data over a 2nd port. There are really good USB-serial converters on the market. I use this cables and never had any issues:. And only as an additional question, are you sure your "reading application" does not send data for i.
Now I have kept program which simultes machine on one pc and program which reads machine input on other machine.
Now At least data transfer is occuring. I think there's an obvious bug in the code. You shouldn't need to call usleep to throttle the loop.
Perhaps you are failing to drain the socket or serial port completely in your input handlers, so that select always returns immediately. I would modify your serial reading function in a few ways, firstly by changing its name to reflect what it does. I would also move the accumulation buffer into the function, making it and its position counter static which means they stick around without losing their value between calls. Also define the details of the packet in define s.
More extensive changes are required to recover from a loss of synchronization to the data or on failure of the check-sum. If the check-sum fails, I don't think it is safe to discard the whole packet unless you can guarantee that the sentinel value 0xA5 or whatever never occurs within the packet or its check-sum.
As you don't mention it, I'm assuming we can't guarantee this, although it is clearly possible to arrange the data protocol to provide such a guarantee. Without the guarantee, we need to check the check-sum within the reading function and to re-sync to the next sentinel on error. I think this might be easier to achieve by making the file descriptor non-blocking and reading one byte at a time.
As your data rate is slow, this is not a great overhead. And as your baud rate is slow compared to processor speed the chances are that your select will unblock after each received character. You should also check for errors in the read call and return an error unless the read was interrupted EINTR - in which case the fd will still select available and the function will be called again - or no data was available EAGAIN.
Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Reading from a serial port Ask Question. Asked 7 years, 11 months ago.
0コメント