Serial Communication doesn't work sometimes.

Smokehead:
is there a way to send hex values like 0x01, 0x02, ... etc. from PC to Arduino vs serial monitor?

Why do you need to do that?

In general you can only send printable characters with the Serial Monitor.

I find that it greatly simplifies debugging if all communication between my PC program and the Arduino uses human-readable characters. That way you can do tests with the Serial Monitor acting in place of your PC program.

...R

PS. Perhaps it would be worthwhile to take a small amount of time to write a new C++ equivalent of my Python program and get that working.

if you are using Windows the RealTerm terminal emulator

the Send tab allows you to enter text such as 0x61 0x62 0x63 0x30 0x31 which is transmitted as a sequence of byte values when the Send Numbers button is hit, e.g. the Arduino program

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char state = Serial.read();
    Serial.println(state);
    Serial.println(state, HEX);
   }
}

would display

a                                                                           
61                                                                         
b                                                                            
62                                                                           
c                                                                            
63                                                                           
0                                                                            
30                                                                           
1                                                                            
3

there are a number of terminal emulators for Unix which may have similar facilities

RealTerm1.jpg

Just to clarify what you are saying...

When you say "send hex" do you mean a string representation of the value in hexadecimal format or do you really mean you want to send a byte?

acboother:
Just to clarify what you are saying...

When you say "send hex" do you mean a string representation of the value in hexadecimal format or do you really mean you want to send a byte?

I'm sure it's "send a byte". That is what is not supported in serial monitor.

If the OP can manage with sending a character or character string and doesn't actually want to write his own tool then he may want to look at my ESM version 4 recently released which also shows in the sample sketch parsing out data and converting to byte, int, char etc.

https://forum.arduino.cc/index.php?topic=500805.0

Sorry for the late answer and the poor choice of words. Actually i want to send a byte including values which can not be displayed as character properly.

But i could solve the problem. Actually it was a mistake while initializing the serial port. I forgot to reset the termios.c_lflag.

After doing this everythings fine. This would explain why it worked after starting the IDEs Serial Monitor.

Thanks for your help. Here's the code which i use to initialize the serial port:

SerialPortSettings.c_cflag &= ~PARENB; /*CLEAR Parity Bit PARENB*/
SerialPortSettings.c_cflag &= ~CSTOPB; /*Stop Bits = 1 */
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the Mask       */
SerialPortSettings.c_cflag |=  CS8;   /* Set the data bits = 8 */
SerialPortSettings.c_cflag &= ~CRTSCTS;
SerialPortSettings.c_cflag |= CREAD | CLOCAL;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_lflag |= ~(ICANON | ECHO | ECHOE | ISIG);    //This one wasnt "c_lflag" but "c_iflag" by mistake.
SerialPortSettings.c_cc[VMIN] = 0;
SerialPortSettings.c_cc[VTIME] = 0;

tcsetattr(m_Port, TCSANOW, &SerialPortSettings);

Hi
I have the same problem
can you give me more explain how to solve it please

Regards

ktyafa:
Hi
I have the same problem
can you give me more explain how to solve it please

Regards

Answer in reply #25 ?