how to write characters to serial port
I have an ancient serial spectrometer which only runs on win9x. I want to convert to a Linux system, but the existing software is proprietary and wont work. I was able to access a console internally on the spectrometer. The console takes keyboard character commands, and sends the output to the serial port. in vb6 I can write to the com port with char values http://www.gtwiki.org/mwiki/?title=VB_Chr_Values using ComPort.Write(Chr(34)) I am interested in using Qt for the interface, how can I send something to the same effect as ComPort.Write(Chr(34)) using qt?
1 Answer 1
- Here an example from Qt official site (C++) http://qt-project.org/wiki/QtSerialPort
- If Visual Basic is your background you may prefer Gambas3 http://gambasdoc.org/help/comp/gb.net/serialport?v3 Gambas have similar syntax of Visual Basic(VB), and support Qt as GUI tool kit. See http://gambasdoc.org/help/comp?v3
- In Linux/BSD, Serial port is more accessible then windows. So you can even write to it from shell/terminal, or use system call from most programming languages. Example in shell with an Android phone as modem, it may help for debugging:
- Reading serial port (need to be root):
sudo su echo -e "AT" > /dev/ttyACM0
It shows OK on reading port window, Also you can sent hexadecimal data (use -n option to avoid sending new line at the end)
echo -e -n "\x41\x54\x0a" > /dev/ttyACM0
echo -e "\x41\x54" > /dev/ttyACM0
Sending Hexadecimal data through Serial Port Communication in Linux
I have got a task of sending hexadecimal data to my COMPORT in linux. I have written this simple C code, but it sends only a decimal number. Can anyone help me in sending an hexadecimal bit. Here is the code I have written
#include /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ int number,n; void main(void) < open_port(); >int open_port(void) < int fd; /* File descriptor for the port */ fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) < perror("open_port: Unable to open /dev/ttyACM0 - "); >else < printf("Port Opened successfully\n"); number = 1; while(number!=55)< scanf("%d",&number); n = write(fd, "ATZ\r", number); if (n < 0) fputs("write() of 4 bytes failed!\n", stderr); >> return (fd); >
A hexadecimal value is the string representation of a binary value. Each character of the string represents 4 bits(a nibble). Serial ports can send data in 5,6,7, or 8 bit units. So are you trying to send strings or numbers?
3 Answers 3
ssize_t write(int fd, const void *buf, size_t count);
That is, it sends count bytes to fd from buf . In your case, the data is always the string «AZTR\r», plus undefined data after that (if count is > 5). Your program sends neither hexadecimal nor decimal data.
Do you want to send binary data or a string of hexadecimal characters?
For option one, you can use: write(fd, somebuffer, len); , where some buffer is a pointer to any set of bytes (including ints, etc).
For option two, first convert your data to a hexadecimal string using sprintf with %02X as the format string, then proceed to write that data to the port.
Linux comand line program, serial (COM port) transmistion, Hex
So I need a program that I can run on embedded linux (OpenWRT 8.09), via command line, I need to be able to transmit 8N1, i need to transmit in HEX (not ascii,) and preferaby also in decimal. As far as i can tell microcom and picocom don’t transmit hex. Edit clarification: I want to enter data into the program in Hex, and have it send the binary (8N1) repressentaion of each 2 digit hex pair.
Not files, i want to send bytes i enter from my keyboard, like br@ys ternimal does in windows. also I can’t just send the ascci equivients as some op n the thijngs i need to send corrispongd to no printable characters
@oxinabox: files, data, whatever. to clarify, you need a BINARY transfer mode instead of an ASCII mode, and you want to be able to input the data to transfer as hex.
3 Answers 3
It is dead project yet, but it works fine and it is able to show hex representation of characters.
But it is graphical, so you can’t use it on embedded linux (if you don’t have there X support ant QT4).
If the program doesn’t allow you to transmit in hex there are workarounds. You can pipe info through od to convert it to hex before sending it to microcom or picocom.
for example, convert «some text» to hex:
echo "some text" | od -tx1 | cut -c 8- | tr -d " "
unfortunatly od is not included in opnwrt distro — this will require a recompile of hte OS. openwrt isn’t great, I think installing debian or even freebsd might be a better option, even though that will mean porting and crosscopiling
@oxinabox: what’s openwrt running on? you could crosscompile od for the openwrt environment, then if it’s on a router, scp the just binary over the network.
Can i please have more info on how to do this piping? I have made a custom program that converst hex or decimal or binery taken as a parameter to the appropriate binery stream, How do i pipe it though the com program?
I think ssterm should work for you. It is written in Python (which is probably bad) but it is console-based, and it does support hexadecimal input and output.
You can launch it like this:
ssterm /dev/ttyAMA0 -b 9600 -i hex -o split
Then it should read hexadecimal characters from stdin, convert each 2 characters to byte and send that byte to the serial.
Also it will read data from the device and show it in hexdump -like way: hex representation to the left and ascii representation to the right.
How to send data to a serial port and see any answer?
On Linux, I want to send a command string (i.e. some data) to a serial port (containing control characters), and listen to the response (which also usually might contain control characters). How can I do this as simplest as possible on Linux? An example is appreciated!
5 Answers 5
All devices on Unix are mapped to a device file, the serial ports would be /dev/ttyS0 /dev/ttyS1 . .
First have a look at the permissions on that file, lets assume you are using /dev/ttyS1 .
You will want read.write access, if this is a shared system then you should consider the security consequences of opening it up for everyone.
A very simple crude method to write to the file, would use the simple echo command.
You can have cat running in one terminal, and echo in a 2nd.
If everything is gibberish, then baud rate, bit settings might need setting before you start sending. stty will do that. !! NOTE stty will use stdin as default file descriptor to affect.
This might be enough for you to script something and log ? Not sure what you are trying to achieve.
For a more interactive, remembers your default settings approach would be to use minicom it is just a program which does everything I’ve mentioned so far. (similar to hyperterminal in Windows, you might be familiar).
An intermediate solution, would use a terminal program like screen which will work on a serial device.
man screen man minicom man stty for more information