|
楼主 |
发表于 2022-8-1 13:47
|
显示全部楼层
本帖最后由 luoluo0775 于 2022-8-1 13:55 编辑
下面是我写的代码,因为功能很简单,所以就没有写太多注释,沟通一下应该就能行。
程序大概是这样的。
用 serialEvent 从串口侦听指令 M600 F5 N4,
然后用parseParameter 拆分指令,取得M ,F, N后面的的不同数值。
最后 processCommand根据拆分出来的指令,去设置NUMBER_OF_FEEDER 24 这24个引脚的高低电平。
#define NUMBER_OF_FEEDER 24
#define MCODE_SET_FEEDER_ENABLE 610
#define MCODE_ADVANCE 600
#define MCODE_RETRACT_POST_PICK 601
#define FEEDER_ENABLE_PIN 8
const static uint8_t feederPinMap[24] = { //feederPinMap: Map IO-pins to specific feeder. First feeder is at index 0 (N0). Last feeder is NUMBER_OF_FEEDER-1
13, // Feeder N0
11, // Feeder N1
9, //...
7,
5,
3,
14, // Feeder N6
16,
18,
22,
24,
26,
30, // Feeder N12
32,
34,
36,
38,
40,
42, //Feeder N18
44,
46,
48,
50,
52, // Feeder N23
};
String inputBuffer = ""; // a String to hold incoming data
bool BufferComplete = false; // whether the Buffer is complete
//parseParameter
float parseParameter(char code,float defaultVal) {
int codePosition = inputBuffer.indexOf(code);
if(codePosition!=-1) {
//code found in buffer
//find end of number (separated by " " (space))
int delimiterPosition = inputBuffer.indexOf(" ",codePosition+1);
float parsedNumber = inputBuffer.substring(codePosition+1,delimiterPosition).toFloat();
return parsedNumber;
} else {
return defaultVal;
}
}
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputBuffer:
inputBuffer.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (BufferComplete) {
Serial.println(inputBuffer);
// clear the Buffer:
inputBuffer = "";
BufferComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputBuffer:
inputBuffer += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
BufferComplete = true;
}
}
}
/**
* Read the input buffer and find any recognized commands. One G or M command per line.
*/
void processCommand() {
//get the command, default -1 if no command found
int cmd = parseParameter('M',-1);
switch(cmd) {
case MCODE_SET_FEEDER_ENABLE: {
int8_t _feederEnabled=parseParameter('S',-1);
if( (_feederEnabled==0 || _feederEnabled==1) ) {
if((uint8_t)_feederEnabled==1) {
digitalWrite(FEEDER_ENABLE_PIN, HIGH);
feederEnabled=ENABLED;
// executeCommandOnAllFeeder(cmdEnable);
sendAnswer(0,F("Feeder set enabled and operational"));
} else {
digitalWrite(FEEDER_ENABLE_PIN, LOW);
feederEnabled=DISABLED;
// executeCommandOnAllFeeder(cmdDisable);
sendAnswer(0,F("Feeder set disabled"));
}
} else if(_feederEnabled==-1) {
sendAnswer(0,("current powerState: ") + String(feederEnabled));
} else {
sendAnswer(1,F("Invalid parameters"));
}
break;
}
case MCODE_ADVANCE: { //1st to check: are feeder enabled?
if(feederEnabled!=ENABLED) {
sendAnswer(1,String(String("Enable feeder first! M") + String(MCODE_SET_FEEDER_ENABLE) + String(" S1")));
break;
}
int8_t signedFeederNo = (int)parseParameter('N',-1);
int8_t feederNo=feederPinMap[signedFeederNo];
feedLength = (uint8_t)parseParameter('F', -1);
pinMode( feederNo, OUTPUT);
digitalWrite(feederNo, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
|
|