About Com Port Question

Hello everyone. Recently I want to make a robot. This robot can use the computer to control. I know the MEGA2560 can support 4 Com Port.s. So today I try to use the USB to receive the command and the command send from the computer. There is OK. But next I want to open another Com Port that is connect the RF. When I call the Serial.begin in Loop repeat there two COM PORTs are not work well. I want to know can the Serial.begin use in the loop? I just want to know Serial.begin() only use in Setup or not. Who can give me answer?

I want to know can the Serial.begin use in the loop?

You can, but generally there is no need to.

But next I want to open another Com Port that is connect the RF.

I don't understand this. Are you unplugging the USB cable from the PC and plugging it into something else?

Now I just use the USB connect and use the RF(like the wifi)connect at same time. I send the command to USB and hope receive the reply by RF. I want to use the USB to control the RF open or close. The RF already connected in Arduino Com Port(Serial1). The hardware is OK.I use the Serial.begin in setup to test open and send or receive the RF there is OK.But I want to open or close in loop there is not work well. So I need to answer. I don't want to open the Com Port at Setup. I hope I can use the PC to control the Com Port open or close in Arduino. for example the USB port only for the testing when I developing the program and the RF will control the robot.

Now the state is when I use the USB send a command to Arduino to open the RF Com Port. The USB can received and do it. Late in PC I can received the Com Port data by USB 3 times but there are wrong. Only the all zero received and the length is different. Some times 37 bytes sometimes 64 bytes.

I have no idea what you're trying to do. Perhaps you could provide a block diagram showing what connections there are between the Arduino, PC and "RF" (whatever that is) and a description of the intended sequence of events between them?

Today I found problem. The Arduino only can receive the char type data. for example if I make a C++ struct and use memcpy copy it to a byte buffer and send to Arduino. The Arduino can't read it all. typedef struct Protocol { int ProtocolLen; //Protocol Len int eCommand; //Protocol Command;

}stProtocol;

typedef struct OpenCom2 : public stProtocol { int nSpeed; }stOpenCom2; Like this struct. In Arduino to I am new stOpenCom2 and use memcpy to a byte buffer and send it to PC(There I use the Serial.write). The PC can received and decode it . But I also new stOpenCom2 in PC and use memcpy to a byte buffer and send to Arduino it can't read it all and length is wrong. So why the always to receive the all zero in PC. I working the game company. I know many online game use this way to create protocol. In socket use this way no problem. But in Arduino maybe only can read char type. In arduino I use the readBytes to receive the Serial Port. But it only can read char type.

The Arduino only can receive the char type data.

Not true.

if I make a C++ struct and use memcpy copy it to a byte buffer and send to Arduino. The Arduino can't read it all.

ints on the Arduino are not the same size as ints on the PC.

In arduino I use the readBytes to receive the Serial Port. But it only can read char type.

Wrong. The Arduino can, in readBytes(), write to a byte array. You have to use a cast, but it's not like that's hard.

It's obvious that English is not your first language and maybe what you posted has got mixed up in the translation, but it's almost incomprehensible. All I can make out is that you're using a struct to define a message structure and then sending the underlying bytes over some sort of connection and expecting that a similar struct on the receiving side will match the same memory layout. That may work, but it relies on the two compilers using the same memory layout for the struct and you should not assume that they will do. Normally you would expect to deal explicitly with word size, alignment and network byte ordering issues. You still haven't explained what your original problem was, and I suggest you focus on getting the underlying transfer of a sequence of bytes working before you try to impose a message structure on that.

Yeah! It's cool yesterday I found the bugs and fixed it! Thank you everyone.