wiki:Docs/Prog/Manual/DeviceSupport/SerialPorts

Serial Ports

Using Serial Ports for Linux Shell

By 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.

Generic Serial Port Use

The “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:

/dev/ttyS0 = COM1 /dev/ttyS1 = COM2

For example you may test from the serial terminal by typing:

# echo “Hello” > /dev/ttyS1

This 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:

# echo “H313233” > /dev/mnbd

This 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.

# echo “123” > /dev/com3

This will output “123” to the COM3 port.

To perform a similar operations within a C program you may use code similar to:

Line 
1
2#include <stdlib.h>
3#include <stdio.h>
4
5int main()
6{
7   FILE* pFile = fopen("/dev/ttyS1", "w");
8   if(pFile != NULL)
9   {
10      fprintf(pFile, "123\r\n");
11      fclose(pFile);
12   }
13}
14

This will write to COM2. Note fprintf also allows format specifiers such as %d to be used:

    int nID = 240;
    fprintf(pFile, "ID: %d\r\n", nID);

This would output "ID: 240".

You may want a program that reads the serial port and responds:

Line 
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <fcntl.h>
6
7#include <string.h>
8
9#include <math.h>
10#include <time.h>
11
12#include <signal.h>
13
14#include <termios.h>
15
16#include "lcd.h"
17
18#include "touch.h"
19#include "kypdbeep.h"
20#include "form.h"
21#include "mnbdcomm.h"
22#include "login.h"
23
24extern uint32 g_nRepWtIntv;
25
26extern struct mnbd_wt_struct wtdata[MAX_SCALES];
27extern uint32 g_nRepWtIntv;
28extern unsigned char g_nRepWtTst;
29
30extern int g_keycnt;
31extern uint8 g_keybuf[20];
32
33#define TIME_FREQ       100
34#define SLP_FREQ        10
35#define DSP_FREQ        10
36
37// On terminal send ACK (CTRL-F) - 825 will send back "ACK received"
38// On terminal send NACK (CTRL-U) - 825 will send back "NACK received"
39// On terminal send STX (CTRL-B) - 825 will start buffering received data
40// On termainl send ETX (CTRL-C) - 825 will send back data received and stop buffering
41
42
43// ttyS1 = COM2
44#define SERIAL_PORT "/dev/ttyS1"
45#define ACK                     0x06
46#define NACK            0x15
47#define STX                     0x02
48#define ETX                     0x03
49
50#define ACK_RESPONSE            "ACK received\r\n"
51#define ACK_RESPONSE_LEN        strlen(ACK_RESPONSE)
52
53#define NACK_RESPONSE           "NACK received\r\n"
54#define NACK_RESPONSE_LEN       strlen(NACK_RESPONSE)
55
56
57int main(int argc, char* argv[])
58{
59   int n;
60   char szMsg[41];
61   struct timespec delaytm;
62
63   int i, nFile;
64   struct termios options;
65   unsigned char rcvdata[11];
66
67   char rcv_buf[50];
68   unsigned int rcv_buf_pos = 0;
69   enum { rcv_idle, rcv_cmd };
70   int rcv_mode = rcv_idle;
71   
72   nFile = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
73   tcgetattr(nFile, &options);
74   options.c_iflag = IGNBRK | IGNPAR;
75   options.c_cflag = CS8 | CREAD | CLOCAL | CRTSCTS;
76   options.c_lflag = 0;
77   options.c_oflag = 0;
78   options.c_cc[VMIN] = 1;
79   options.c_cc[VTIME] = 0;
80   tcsetattr(nFile, TCSADRAIN, &options);
81       
82   delaytm.tv_sec = 0;
83   delaytm.tv_nsec = 100000;
84       
85   ReadTouchCal();
86
87   OpenBeeper();
88
89   OpenMnBd(0); // Mainboard settings
90       
91   OpenTouch();
92       
93   InitLCD();
94
95   OpenMnBd(REQ_SCALE(1));
96       
97   g_nRepWtIntv = REP_INTV_SAMPLE_RATE;
98   MnBdRequest(REQ_SCALE(1), MNBD_REQ_REP_WT, NO_WAIT_ACK);
99
100   int tm_cnt = TIME_FREQ;
101   int slp_cnt = SLP_FREQ;
102   int dsp_cnt = DSP_FREQ;
103
104   while(1)
105   {
106      slp_cnt--;
107      if(slp_cnt == 0)
108      {
109          nanosleep(&delaytm, NULL);
110          slp_cnt = SLP_FREQ;
111      }
112       
113      if(ind_check_for_wt_rcv(1))
114      {
115          dsp_cnt--;
116          if(dsp_cnt == 0)
117          {
118              LocateLCD(0,0);
119              sprintf(szMsg, "%6.2f", wtdata[1].grosswt);
120              PrintLCD(szMsg);
121
122              ReadKeypad();
123              if(g_keycnt > 0)
124              {
125                  g_keycnt = 0;
126                  if(g_keybuf[0] == ESC)
127                      break;
128              }
129              dsp_cnt = DSP_FREQ;
130          }
131      }
132
133      int n = read(nFile, rcvdata, sizeof(rcvdata));
134      for(i = 0; i < n; i++)
135      {
136         if(rcvdata[i] == ACK)
137         {
138            // Handle ACK
139            write(nFile, ACK_RESPONSE, ACK_RESPONSE_LEN);
140            rcv_mode = rcv_idle;
141         }
142         else if(rcvdata[i] == NACK)
143         {
144            // Handle NACK
145            write(nFile, NACK_RESPONSE, NACK_RESPONSE_LEN);
146            rcv_mode = rcv_idle;
147         }
148         else if(rcv_mode == rcv_idle)
149         {
150            if(rcvdata[i] == STX)
151            {
152               rcv_mode = rcv_cmd;
153               rcv_buf_pos = 0;
154            }
155         }
156         else if(rcv_mode == rcv_cmd)
157         {
158            if(rcvdata[i] == ETX)
159            {
160               // Command received - write it
161               write(nFile, rcv_buf, rcv_buf_pos);
162               rcv_mode = rcv_idle;
163            }
164            else
165            {
166               if(rcv_buf_pos < sizeof(rcv_buf) - 1)
167               {
168                  rcv_buf[rcv_buf_pos++] = rcvdata[i];
169               }
170            }
171         }
172       }
173    }
174    g_nRepWtIntv = REP_INTV_STOP;
175    MnBdRequest(REQ_SCALE(0), MNBD_REQ_REP_WT, NO_WAIT_ACK);
176
177    CloseMnBd(0);
178    CloseMnBd(REQ_SCALE(1));
179    ClearLCD();
180    return 0;
181}
182

This 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.

[^]

Last modified 8 years ago Last modified on Nov 30, 2009, 2:35:21 PM