yeokm1 / SwiftSerial

A Swift Linux and Mac library for reading and writing to serial ports.
MIT License
136 stars 40 forks source link

Disabled input processing #17

Closed mri-scanomat closed 4 years ago

mri-scanomat commented 4 years ago

Hi

We had an issue on Linux that some input characters were converted on the input side, like 0x0D -> 0x0A, which was a problem for us as we are working with a binary protocol. So we had to ensure to disable all input processing to avoid problem with currupt packages and wrong checksums.

So please see the suggested change and judge if it can be useful to you. Otherwise we would suggest adding a "processInput" flag and make that disable or enable this feature so that it is clear what is happening.

But thanks for a otherwise very functional library :)

yeokm1 commented 4 years ago

Hi @mri-scanomat, thanks for your PR. Give me some time to look into it and test your improvement on both Mac and Linux. If I can't replicate it, your suggestion to make it into an option is great and I'll probably do that.

I would like to clarify though, when you say the characters are changed on the input side. Was it changed on the sender or receiver? That is, when you used this library on Linux, is the Linux system the transmitting or receiving party?

mri-scanomat commented 4 years ago

Hi @yeokm1, Sound good.

In our case we look at linux as the receiver. So linux <-> embedded device. We would send some data from linux to our embedded device. The embedded device would receive the data successfully and process it. The embedded device would then send a reply, that in some cases would contain the hex value 0x0D as part of its response. like "0x11 0x22 0x33 0x0D 0x44". We could with a digital analyser verify that the data on the wire where correct, but what we received inside the swift app was ""0x11 0x22 0x33 0x0A 0x44". So the character 0x0D was changed to 0x0A. This fits with the flag ICRNL being set as it is described as

ICRNL - Translate carriage return to newline on input

To ensure this did not happen and that other input changed also did not happen, we added the three flags to the c_iflag. As minimum, we had to disable the "ICRNL" flag to not get the above behaviour on Linux. The two others were more of a precaution.

Hope it makes sense and that you can reproduce it :)

yeokm1 commented 4 years ago

Hi @mri-scanomat, apologise I'm busy this few weeks. I can only look into this issue 2 weekends from now. Any hurry to get this resolved?

mri-scanomat commented 4 years ago

Hi @yeokm1, No problem, we are not in a hurry. We have a local Fork that we are using right now so no worry. We just wanted to push back this change to help others :)

yeokm1 commented 4 years ago

@mri-scanomat Hi, I can replicate your issue and your solution works.

Updated the release code to 0.1.2.

I also included an additional test example for binary data. https://github.com/yeokm1/SwiftSerial/tree/master/Examples/SwiftSerialBinary