12

In Windows command prompt to configure a serial port, I can simple use:

mode com1: 9600,n,8,1

or to read the configuration:

mode com1:

Are there similar commands in Linux? What would be the easiest way to find and configure the serial port in Linux?

  • Normally you can edit it right in the applications you use for accessing the serial like, e.g. screen /dev/ttyS0 <baud-rate> (the default would be 9600 no matter what system-wide settings, not even sure such exist) – phk Nov 13 '15 at 10:28
  • I need to configure it to use a POS printer. – Hrvoje T Nov 13 '15 at 10:38
  • OK, in that case try the stty commands mentioned at: wiki.openwrt.org/doc/recipes/serialbaudratespeed – phk Nov 13 '15 at 10:40
24

You can use the stty command to set such parameters.

This will show all settings on the first serial port (replace ttyS0 with ttyUSB0 if using an USB serial port):

stty -F /dev/ttyS0 -a

This will set the baud rate to 9600, 8 bits, 1 stop bit, no parity:

stty -F /dev/ttyS0 9600 cs8 -cstopb -parenb

One thing that generally confuses people is that most serial drivers will reset the settings to the defaults once the device is closed (i.e. no process has the device open anymore). So the above stty command will set the settings, then when it's done the driver resets them again. If you first have your POS software open the device and then perform the stty settings, they should stick around until your POS software closes the device again (e.g. upon exiting).

I'd have thought that the POS software should have some way of configuring these settings on its own. If you're writing your own software to drive the printer, make sure you first open the device, and then perform the stty command.

You may also need to play around with other settings, e.g. opost means that output postprocessing will be performed. If opost and onlcr are both set, the onlcr will cause an extra carriage return (0xd) to be added when a newline (0xa) byte is output, typically to prevent staircase printing such as

This is a line
              This is the next line

This may or may not be what you want. If you want a raw one-to-one output to the printer turn opost off (add -opost to the stty parameters).

Handshaking (flow control) is also controlled with stty, without knowing more about the printer I can't tell whether you need to set anything.

Check the stty manpage for lots more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.