在Linux上创建自定义服务以通过串行UART与调制解调器通信

时间:2018-07-12 14:50:47

标签: c linux service serial-port modem

我用C语言编写了一个程序,用于通过串行uart与连接到我的rapiberry pi的3G调制解调器进行通信。打开之后,除非程序崩溃,否则我不会断开连接。当我自己运行它时,它运行良好,但是当我创建一个服务来运行它时,它在启动后立即失败。

我的主要问题,这是我的程序的问题,还是我配置Linux服务运行的方式的问题?以及解决此问题的最佳做法是什么。

我的服务文件

[Unit]
Description=Firmware for sensor IOT module
Documentation=m.thompson.239@gmail.com
After=network-online.target

[Service]
Type=simple
ExecStart=/hdd/firmware
StandardOutput=syslog
RestartSec=30
Restart=always

[Install]
WantedBy=multi-user.target
Alias=firmware.service

当我检查服务状态时

Loaded: loaded (/lib/systemd/system/firmware.service; enabled; vendor preset:
   Active: activating (auto-restart) (Result: signal) since Thu 2018-07-12 14:44
  Process: 539 ExecStart=/hdd/firmware (code=killed, signal=SEGV)
 Main PID: 539 (code=killed, signal=SEGV)

Jul 12 14:44:34 raspberrypi systemd[1]: firmware.service: Failed with result 'si
lines 1-7/7 (END)...skipping...
● firmware.service - Firmware for sensor IOT module
   Loaded: loaded (/lib/systemd/system/firmware.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: signal) since Thu 2018-07-12 14:44:34 UTC; 28s ago
  Process: 539 ExecStart=/hdd/firmware (code=killed, signal=SEGV)
 Main PID: 539 (code=killed, signal=SEGV)

Jul 12 14:44:34 raspberrypi systemd[1]: firmware.service: Failed with result 'signal'.

这些是我打开串行端口时的设置。

    int set_interface_attribs(int fd, int speed)
{
  struct termios tty;

  if (tcgetattr(fd, &tty) < 0) {
    printf("Error from tcgetattr: %s\n", strerror(errno));
    return -1;
  }

  cfsetospeed(&tty, (speed_t)speed);
  cfsetispeed(&tty, (speed_t)speed);

  tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
  tty.c_cflag &= ~CSIZE;
  tty.c_cflag |= CS8;         /* 8-bit characters */
  tty.c_cflag &= ~PARENB;     /* no parity bit */
  tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
  tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

  /* setup for non-canonical mode */
  tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  tty.c_oflag &= ~OPOST;

  /* fetch bytes as they become available */
  tty.c_cc[VMIN] = 1;
  tty.c_cc[VTIME] = 1;

  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    printf("Error from tcsetattr: %s\n", strerror(errno));
    return -1;
  }
  return 0;
}

0 个答案:

没有答案