Serial Communication Canonical Mode Non-Blocking NL Detection - serial-port

I am sending commands over a serial port from a Linux embedded device to some serial enabled firmware. For easy debugging and simplicity we are using ascii human-readable commands terminated by newlines. Is it appropriate to use canonical mode here, or is canonical mode usually reserved for interactive terminals? The examples I find online all use raw mode.
In particular, in canonical mode, how do I check without blocking if an entire line is available for reading.

according to Linux Serial Programming documentation :
This is the normal processing mode for terminals, but can also be useful for
communicating with other dl input is processed in units of lines, which means
that a read will only return a full line of input. A line is by default
terminated by a NL (ASCII LF), an end of file, or an end of line character. A
CR (the DOS/Windows default end-of-line) will not terminate a line with the
default settings.
Canonical input processing can also handle the erase, delete word, and
reprint characters, translate CR to NL, etc..
First
using canonical mode for serial communications is the best option, because we have Linux kernel support on data transmission and system handlers that will help to better reading serial text
Second
if you want to use canonical mode , make sure that you are using the right character for end of line in your device that sending data , other way you cannot use canonical feature

Is it appropriate to use canonical mode here, or is canonical mode usually reserved for interactive terminals?
Yes, you can use canonical mode, but you will need to configure the termios interface for your situation.
The default termios configuration is for an interactive terminal, so features such as echoing the input should be disabled.
Since your device is unlikely to send the backspace and delete characters, such features can be ignored.
The examples I find online all use raw mode.
Seems like there are some "experts" that are not aware that canonical mode for terminals exists.
See the comments to reading from serial port in c breaks up lines .
For an example of (blocking) canonical mode, see this answer (note that there's another "expert comment" telling the OP that he cannot read lines).
In particular, in canonical mode, how do I check without blocking if an entire line is available for reading.
You could use select().
The man page implies that a canonical read of a terminal device is supported:
The file descriptors listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block ...)
When both fields of the timeval structure are zero, then select() will not block and returns immediately.

Related

Does sim800l has an url limit?

I want to send an information to web server with arduino via sim800l.
AT+HTTPPARA=\"URL\",\"http://api.trackers.live/v3.0.0/TRCKR02B2AF359D19/40.35715122342342322432-36.077406243242342-1469134234/1212/021/16553"
When I want to send long url to web server,the program crashes.
Does sim800l has an url limit? or is there any possible way to send an url like this?
Yes, all modems have a limit in how many characters they accept on a command line. The V.250 specification says in chapter "5.2.1 Command line general format":
A command line is made up of three elements: the prefix, the body, and the termination character. ... The DCE shall be capable of accepting at least 40 characters in the body.
The prefix is the two characters "AT" or "at" and the termination character should be '\r', thus a modem (aka DCE, Data Circuit-terminating Equipment) is allowed to only accept as low as 43 characters on a command line.
Now the was majority of modem supports more, probably that 40 character limit was due to some old Hayes modem made in the 1970s. If sim800l has decent documentation it should say how many characters it support on a command line. If it has crappy documentation it will not say.
As always with AT command syntax question, the very first source to query should always be the manufacturer specific documentation for that particular modem. Failing that you can fall back to V.250 or 27.007 or 27.005.
I dont know about the limit but you can use a link shortener to shorten the link...

Zigbee communication among 3 devices is carrying garbage along with the actual data transmitted

I am broadcasting Hello using one Xbee (say A).....Xbee (say B) and Xbee (say C) are receiving a lot of garbage values before and after Hello.
All the baudrates are 9600...where am I going wrong?
It would help if you posted an example of the data you see, perhaps the hex values of each byte.
My guess is that you've configured the modules for "API mode" which wraps payloads with a header (starting with 0x7E, the character ~) and footer. It's useful for "smart" devices because it supports multiple packet types.
Check your settings, and make sure you're using ATAP=0. You can use XCTU to change the settings, or from a terminal use the escape sequence (1 second pause, +++, 1 second pause then module should respond with OK) to enter command mode. In command mode, first set ATAP0 and then ATWR to save the changes.

Is it possible to send ASCII control codes via RS232?

I would like to receive and send bytes that have special meaning in ASCII code like End of Text, End of Transmission etc. but I am not sure if it is allowed. Can it break my communication? Sending and receiving looks like reading from file that is why I doubt I can directly use this specific values. I use Windows OS.
EDIT: I have tested it and there is no problem with any sign. All of control ASCII characters can be sent via RS 232. Both reading and writing cause no unexpected behaviour.
RS232 is a very binary protocol. It does not even assume 8-bit bytes, let alone ASCII. The fact that on Windows, you use file functions does not matter either. Those too do not assume text data, although those do assume 8-bit byets.
RS-232 nodes do not interpret the data except in software flow control mode (XOn/XOff). You use this mode only if both parties agree and agree on the values of XOn and XOff.
The values are historically based on the ASCII DC1 and DC3 characters but the only thing that matters is their values, 0x11, and 0x13.
If you haven't set up software flow control, all values are passed through as-is.

What character represent "Enter" for serial communication?

I am trying to send a serial command using my Arduino, but I can't find out how to replicate the Enter key. I've tried \r\n as well as \n and neither of those seem to do it.
I've tried the Arduino functions Serial.write() Serial.print() Serial.println() and none of those work either.
What can I use to replicate the Enter key?
Thank you
Try \r
It has the ASCII 0xD(13) and it's called Carriage Return
In general, it depends on the application that is processing the key press. From the keyboard, the typical scan code 0x1C is sent to the application for the Enter key (and 0x9C on release).
In PuTTY (related to OP's question here), it sends ASCII CR (carriage return) 0x0D only, even on Windows machines.

How can I use the Packet Structure from an RFC and apply it to my socket program?

Here's an example 'Packet Structure' image: http://freesoft.org/CIE/Course/Section3/7.htm
Lets say I had a small Python program that listened on X port and captured that packet and saved it to the variable 'data'.
How would I pull out the packet information from data? For example, say I wanted to read the 'version', is it just:
print data[0:4] ?
How would I get the Source IP Address?
I've been doing more socket coding lately and have ran into quite a few of these 'packet structure' images. I'm yet to figure out how to apply them to my code :/
Note that your example shows an IP header - if you are simply using sockets, you will not see this information (its already been digested by the system IP and TCP stacks).
If you want to capture raw data, look into using libpcap, which will allow raw packets. You can also use tcpdump to produce a file with raw packets.
As for structures, you can read the first 4 bytes if your data was a string with your command. You would likely want to encode the string as "hex" (or integers for the normal representation) or you will see "garbage" characters instead.
For more powerful unpacking, use the struct module which comes with python.

Resources