← Back to dashboard

File view

plc_232_print.lib

1
//$Id: $
2
/*
3
  2013/01/20 Charles Hand
4
  New Library
5
*/
6
7
/*** BeginHeader */
8
9
#ifndef __ISM_232_PRINT_
10
#define __ISM_232_PRINT_
11
12
#define BINBUFSIZE  63
13
#define BOUTBUFSIZE 63
14
15
#define PRINT_ERROR_NONE 0
16
#define PRINT_ERROR_PRINTER_NOT_READY 1
17
#define PRINT_ERROR_ENQUEUE_TOO_LONG 2
18
#define PRINT_ERROR_ENQUEUE_BUSY 3
19
20
/*** EndHeader */
21
22
/*** BeginHeader
23
24
     Initialize232,
25
	  PrintTick,
26
     EnquePrintLine
27
*/
28
29
//Global function prototypes **************************************************
30
void Initialize232();
31
int PrintTick();
32
int EnquePrintLine(char *aLine);
33
34
/*** EndHeader */
35
36
//Local function prototypes *********************************************
37
int printerOnline();
38
int dynamicCBugWorkaroundSendMessage(char *aMessage);
39
40
//Constants *******************************************************
41
#define PRINT_BAUD_RATE 9600L
42
#define PRINT_TX_ENABLE_BIT  3
43
#define PRINT_ESC "\1B"
44
45
#define SERB_CTS_PORT PEDR
46
#if BOARDREV == 'B'
47
	#define SERB_CTS_BIT 7
48
#endif
49
#if BOARDREV == 'C'
50
	#define SERB_CTS_BIT 4
51
#endif
52
53
#define PRINT_STATE_IDLE 0
54
55
/*** Local variables ***/
56
char dynamicCBugWorkaroundCurrentLine[BOUTBUFSIZE];
57
char statusRequestLine[3];
58
int printState;
59
60
61
void Initialize232 ()
62
{
63
	printf("Init 232 ...\n");
64
   printState = PRINT_STATE_IDLE;
65
   memset(dynamicCBugWorkaroundCurrentLine, 0, sizeof(dynamicCBugWorkaroundCurrentLine));
66
   strcpy(statusRequestLine, PRINT_ESC);
67
   strcat(statusRequestLine, "v");
68
69
   BitWrPortI (PDFR, &PDFRShadow, 1, 4); 
70
   WrPortI (SBCR, &SBCRShadow, 0x10);
71
   BitWrPortI (PEFR, &PEFRShadow, 0, SERB_CTS_BIT);
72
	BitWrPortI (PEDDR, &PEDDRShadow, 0, SERB_CTS_BIT);
73
   serBopen(PRINT_BAUD_RATE);
74
}
75
76
int PrintTick()
77
{
78
	int result;
79
   result = PRINT_ERROR_NONE;
80
   if (!printerOnline())
81
		result = PRINT_ERROR_PRINTER_NOT_READY;
82
   return result;
83
}
84
85
86
int EnquePrintLine(char *aLine)
87
{
88
	int result;
89
   int sendResult;
90
   result = 0;
91
   sendResult = 0;
92
	if (strlen(aLine) > BOUTBUFSIZE - 2)
93
   {
94
   	result = PRINT_ERROR_ENQUEUE_TOO_LONG;
95
   }
96
   else
97
   {
98
   	strcpy(dynamicCBugWorkaroundCurrentLine, aLine);
99
      strcat(dynamicCBugWorkaroundCurrentLine, "\0d");
100
      sendResult = dynamicCBugWorkaroundSendMessage(dynamicCBugWorkaroundCurrentLine);
101
      printState = PRINT_STATE_IDLE;
102
   }
103
}
104
105
int printerOnline()
106
{
107
  int result;
108
109
  result = 0;
110
  if (!BitRdPortI (SERB_CTS_PORT, SERB_CTS_BIT))
111
  	 result = 1;
112
  return (result);
113
}
114
115
int dynamicCBugWorkaroundSendMessage(char *message) {
116
	 int result;
117
    long timesUp;
118
    int charsSent;
119
120
    result = 0;
121
    charsSent = 0;
122
    charsSent = serBwrite(message, strlen(message));
123
    if (charsSent != strlen(message))
124
    {
125
    	result = 1;
126
    }
127
    while (serBwrUsed() > 0) {}
128
    timesUp = MS_TIMER + 2;
129
    while ((long)MS_TIMER - timesUp < 0) {}
130
    return result;
131
}
132
133
/*** BeginHeader */
134
135
#endif
136
137
/*** EndHeader */