Changes between Version 1 and Version 2 of Docs/Prog/Manual/DeviceSupport/SerialPorts


Ignore:
Timestamp:
11/30/09 10:21:43 (15 years ago)
Author:
Don Wilson
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Docs/Prog/Manual/DeviceSupport/SerialPorts

    v1 v2  
    11== Serial Ports ==
     2
     3=== Using Serial Ports for Linux Shell ===
     4By default, the Cardinal 825 ships with the Linux console disabled. To enable it, start the indicator and press 3. Setup Menu. Next, press 4. Update Software Menu and then 3. Settings. If you see '''console=null''', remove that line. Save the settings and reboot your indicator. You will now see Linux output from COM1.
     5
     6=== Generic Serial Port Use ===
     7The “COM1” and “COM2” serial ports are routed through the main board to the MCF5329 processor on the operator interface board. These ports are controlled by the appropriate uCLinux device driver for the built in Coldfire serial ports. These ports may be accessed as:
     8
     9/dev/ttyS0  = COM1
     10/dev/ttyS1  = COM2
     11
     12For example you may test from the serial terminal by typing:
     13{{{
     14# echo “Hello” > /dev/ttyS1
     15}}}
     16
     17This will send the message “Hello” out the COM2 serial port. The 825 COM3 port is connected to the main board MCF5213 processor. If the 825 is configured for OIML operation this port is dedicated to sending OIML format verification weight and may not be used by applications. If OIML is not enabled applications may access the COM3 port by means of the mnbd-comm device driver. For example:
     18
     19{{{
     20# echo “H313233” > /dev/mnbd
     21}}}
     22
     23This will output “123” to the COM3 port. Note 31 hexadecimal for the ASCII character “1”, 32 is hexadecimal for the ASCII character “2”, etc. As of kernel 2008-11-20 an additional device file /dev/com3 is provided to simplify COM3 application communications. This additional device file is also controlled by the mnbd-comm device driver.
     24{{{
     25# echo “123” > /dev/com3
     26}}}
     27
     28This will output “123” to the COM3 port.
     29
     30To perform a similar operations within a C program you may use code similar to:
     31
     32{{{
     33#!Lineno
     34#!c
     35
     36#include <stdlib.h>
     37#include <stdio.h>
     38
     39int main()
     40{
     41   FILE* pFile = fopen("/dev/ttyS1", "w");
     42   if(pFile != NULL)
     43   {
     44      fprintf(pFile, "123\r\n");
     45      fclose(pFile);
     46   }
     47}
     48
     49}}}
     50
     51This will write to COM2. Note fprintf also allows format specifiers such as %d to be used:
     52
     53{{{
     54    int nID = 240;
     55    fprintf(pFile, "ID: %d\r\n", nID);
     56}}}
     57
     58This would output "ID: 240".
     59
     60You may want a program that reads the serial port and responds:
     61
     62{{{
     63#!Lineno
     64#!c
     65
     66#include <stdio.h>
     67#include <stdlib.h>
     68#include <unistd.h>
     69#include <fcntl.h>
     70
     71#include <string.h>
     72
     73#include <math.h>
     74#include <time.h>
     75
     76#include <signal.h>
     77
     78#include <termios.h>
     79
     80#include "lcd.h"
     81
     82#include "touch.h"
     83#include "kypdbeep.h"
     84#include "form.h"
     85#include "mnbdcomm.h"
     86#include "login.h"
     87
     88extern uint32 g_nRepWtIntv;
     89
     90extern struct mnbd_wt_struct wtdata[MAX_SCALES];
     91extern uint32 g_nRepWtIntv;
     92extern unsigned char g_nRepWtTst;
     93
     94extern int g_keycnt;
     95extern uint8 g_keybuf[20];
     96
     97#define TIME_FREQ       100
     98#define SLP_FREQ        10
     99#define DSP_FREQ        10
     100
     101// On terminal send ACK (CTRL-F) - 825 will send back "ACK received"
     102// On terminal send NACK (CTRL-U) - 825 will send back "NACK received"
     103// On terminal send STX (CTRL-B) - 825 will start buffering received data
     104// On termainl send ETX (CTRL-C) - 825 will send back data received and stop buffering
     105
     106
     107// ttyS1 = COM2
     108#define SERIAL_PORT "/dev/ttyS1"
     109#define ACK                     0x06
     110#define NACK            0x15
     111#define STX                     0x02
     112#define ETX                     0x03
     113
     114#define ACK_RESPONSE            "ACK received\r\n"
     115#define ACK_RESPONSE_LEN        strlen(ACK_RESPONSE)
     116
     117#define NACK_RESPONSE           "NACK received\r\n"
     118#define NACK_RESPONSE_LEN       strlen(NACK_RESPONSE)
     119
     120
     121int main(int argc, char* argv[])
     122{
     123   int n;
     124   char szMsg[41];
     125   struct timespec delaytm;
     126
     127   int i, nFile;
     128   struct termios options;
     129   unsigned char rcvdata[11];
     130
     131   char rcv_buf[50];
     132   unsigned int rcv_buf_pos = 0;
     133   enum { rcv_idle, rcv_cmd };
     134   int rcv_mode = rcv_idle;
     135   
     136   nFile = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
     137   tcgetattr(nFile, &options);
     138   options.c_iflag = IGNBRK | IGNPAR;
     139   options.c_cflag = CS8 | CREAD | CLOCAL | CRTSCTS;
     140   options.c_lflag = 0;
     141   options.c_oflag = 0;
     142   options.c_cc[VMIN] = 1;
     143   options.c_cc[VTIME] = 0;
     144   tcsetattr(nFile, TCSADRAIN, &options);
     145       
     146   delaytm.tv_sec = 0;
     147   delaytm.tv_nsec = 100000;
     148       
     149   ReadTouchCal();
     150
     151   OpenBeeper();
     152
     153   OpenMnBd(0); // Mainboard settings
     154       
     155   OpenTouch();
     156       
     157   InitLCD();
     158
     159   OpenMnBd(REQ_SCALE(1));
     160       
     161   g_nRepWtIntv = REP_INTV_SAMPLE_RATE;
     162   MnBdRequest(REQ_SCALE(1), MNBD_REQ_REP_WT, NO_WAIT_ACK);
     163
     164   int tm_cnt = TIME_FREQ;
     165   int slp_cnt = SLP_FREQ;
     166   int dsp_cnt = DSP_FREQ;
     167
     168   while(1)
     169   {
     170      slp_cnt--;
     171      if(slp_cnt == 0)
     172      {
     173          nanosleep(&delaytm, NULL);
     174          slp_cnt = SLP_FREQ;
     175      }
     176       
     177      if(ind_check_for_wt_rcv(1))
     178      {
     179          dsp_cnt--;
     180          if(dsp_cnt == 0)
     181          {
     182              LocateLCD(0,0);
     183              sprintf(szMsg, "%6.2f", wtdata[1].grosswt);
     184              PrintLCD(szMsg);
     185
     186              ReadKeypad();
     187              if(g_keycnt > 0)
     188              {
     189                  g_keycnt = 0;
     190                  if(g_keybuf[0] == ESC)
     191                      break;
     192              }
     193              dsp_cnt = DSP_FREQ;
     194          }
     195      }
     196
     197      int n = read(nFile, rcvdata, sizeof(rcvdata));
     198      for(i = 0; i < n; i++)
     199      {
     200         if(rcvdata[i] == ACK)
     201         {
     202            // Handle ACK
     203            write(nFile, ACK_RESPONSE, ACK_RESPONSE_LEN);
     204            rcv_mode = rcv_idle;
     205         }
     206         else if(rcvdata[i] == NACK)
     207         {
     208            // Handle NACK
     209            write(nFile, NACK_RESPONSE, NACK_RESPONSE_LEN);
     210            rcv_mode = rcv_idle;
     211         }
     212         else if(rcv_mode == rcv_idle)
     213         {
     214            if(rcvdata[i] == STX)
     215            {
     216               rcv_mode = rcv_cmd;
     217               rcv_buf_pos = 0;
     218            }
     219         }
     220         else if(rcv_mode == rcv_cmd)
     221         {
     222            if(rcvdata[i] == ETX)
     223            {
     224               // Command received - write it
     225               write(nFile, rcv_buf, rcv_buf_pos);
     226               rcv_mode = rcv_idle;
     227            }
     228            else
     229            {
     230               if(rcv_buf_pos < sizeof(rcv_buf) - 1)
     231               {
     232                  rcv_buf[rcv_buf_pos++] = rcvdata[i];
     233               }
     234            }
     235         }
     236       }
     237    }
     238    g_nRepWtIntv = REP_INTV_STOP;
     239    MnBdRequest(REQ_SCALE(0), MNBD_REQ_REP_WT, NO_WAIT_ACK);
     240
     241    CloseMnBd(0);
     242    CloseMnBd(REQ_SCALE(1));
     243    ClearLCD();
     244    return 0;
     245}
     246
     247}}}
     248
     249This sample displays updating weight on the LCD display and will exit when the Shift-ESC key on the keypad is pressed. If CTRL-F is received on the COM2 port this application will send "ACK received<CR><LF>" out the serial port in response. If CTRL-U is received on the COM2 port this application will send "NACK received<CR><LF>" out the serial port in response. If CTRL-B is received the application will start buffering subsequent received characters. When CTRL-C is received the application will respond back with the buffered characters and stop buffering.
     250
     251[[Top]]